2
-- This is a simple OOP single-inheritance implementation based
3
-- on the one suggested by <Programming in Lua at http://www.lua.org/pil/16.html>.
4
-- One big difference from what you may be expecting is that there are no
5
-- constructors per se; all subclasses share the same constructor method,
6
-- which takes a table of properties that are mixed into the instance.
7
-- e.g. Code like this:
10
-- MyClass = Class:extend{ color = 'red', name = 'robin' }
11
-- myObject = MyClass:new{ color = 'blue' }
14
-- Would set myObject to have a color of 'blue' and a name of 'robin'. There is a
15
-- <onNew> event handler you can use to perform initialization based on these values.
18
-- Called once, when a new object is created via <new()>.
22
-- Creates a subclass of a class object, replacing any class properties
23
-- with those in the object passed. It also sets the subclass's prototype
24
-- property to this class. This alters the passed table in-place.
27
-- obj - table of properties
32
extend = function (self, obj)
34
assert(type(obj) == 'table', 'must extend a table, received a ' .. type(obj))
36
-- copy any table properties into the subclass, so that
37
-- it does not accidentally overwrite them
39
for key, value in pairs(self) do
40
if key ~= '__index' and not obj[key] and type(value) == 'table' then
41
obj[key] = copyTable(self[key])
45
-- __index work to set up inheritance and getters/setters
48
setmetatable(obj, self)
55
-- Extends an object and calls its onNew() handler if it is defined.
56
-- This handler is meant for object-specific initialization,
57
-- not class-wide work.
60
-- obj - table of properties that the new object starts with,
61
-- overriding anything set in the class
66
new = function (self, obj)
67
obj = self:extend(obj)
68
if obj.onNew then obj:onNew() end
73
-- Mixes all properties passed into the current object, overwriting any
74
-- pre-existing values.
77
-- obj - table of properties to mix into the object
82
mixin = function (self, obj)
83
assert(type(obj) == 'table', 'must mix in a table, received a ' .. type(obj))
84
for key, value in pairs(obj) do
89
-- Function: instanceOf
90
-- Checks whether a certain object is anywhere in its inheritance chain.
93
-- class - table to check against
98
instanceOf = function (self, class)
99
local proto = self.prototype
102
if proto == class then
106
proto = proto.prototype