lua-Coat
Yet a Another Lua Object-Oriented Model

References

Global Functions

class( modname )

Create a Coat class as a standard Lua module.

type( obj )

This standard function is overloaded.

Functions in the built Class

after( name, func )

Method modifier.

around( name, func )

Method modifier. The new method receives the old one as first parameter.

before( name, func )

Method modifier.

extends( class [, ...] )

Extends the current class with the list of class in parameter.

has( name, options )

Adds a attribute name in the current class. The prefix + in name allows overriding.

The following options are available :

  • clearer : function name
  • coerce : boolean
  • default : value or function
  • handles : table
  • is : string ro|rw
  • isa : type name
  • lazy : boolean
  • required : boolean
  • trigger : function

isa( obj, t )

Checks if a object is an instance of t which is a class or classname.

overload( name, func )

Registers a metamethod.

override( name, func )

Method modifier.

method( name, func )

Registers a method.

new( args )

Instanciates a object.

If exist, the user method BUILD is called at the end.

Functions in Coat.Meta

Coat.Meta.class( modname )

Returns the Coat class modname or nil.

Coat.Meta.classes()

Returns a table with all Coat classes.

Coat.Meta.has( class, name )

Returns the table of options of the attribute name in the class class or nil.

Examples

Point

require 'Coat'

class 'Point'

has( 'x', { is = 'rw', isa = 'number', default = 0 } )
has( 'y', { is = 'rw', isa = 'number', default = 0 } )

overload( '__tostring', function (self)
    return '(' .. self:x() .. ', ' .. self:y() .. ')'
end )

method( 'draw', function (self)
    return "drawing " .. self._CLASS .. tostring(self)
end )
local p1 = Point{ x = 1, y = 2 }
p1:draw()

Point3D

require 'Coat'

class 'Point3D'
extends 'Point'

has( 'z', {is = 'rw', isa = 'number', default = 0 } );

overload( '__tostring', function(self)
    return '(' .. self:x() .. ', ' .. self:y() .. ', ' .. self:z() .. ')'
end )
local p1 = Point3D{ x = 1, y = 2, z = 3 }
p1:draw()