lua-Coat
Yet 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.

can( obj, methname ) or obj:can( methname )

Checks if a object has a method methname.

does( obj, r ) or obj:does( r )

Checks if a object consumes r which is a role or rolename.

extends( class [, ...] )

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

has.name = { ['+', ] option1 = value1, option2 = value2, ... }

Adds a attribute name in the current class. A item '+' allows overriding.

The following options are available :

  • builder : function name
  • clearer : function name
  • coerce : boolean
  • default : value or function
  • does : role name
  • handles : table for method delegation
  • is : string (ro|rw)
  • isa : type name
  • lazy : boolean
  • lazy_build : boolean
  • reader : function name
  • required : boolean
  • trigger : function
  • writer : function name

isa( obj, t ) or obj:isa( 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.

with( role [, alias-excludes ] [, role, ... ] )

Composes one or more role, a optional table alias-excludes allows to prevent name collision.

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()