mixins now support static methods

This commit is contained in:
Enrique García Cota 2011-09-18 17:40:50 +02:00
parent 2271813323
commit c2e4fce043
2 changed files with 15 additions and 2 deletions

View File

@ -63,7 +63,12 @@ end
local function _includeMixin(klass, mixin) local function _includeMixin(klass, mixin)
assert(type(mixin)=='table', "mixin must be a table") assert(type(mixin)=='table', "mixin must be a table")
for name,method in pairs(mixin) do for name,method in pairs(mixin) do
if name ~="included" then klass[name] = method end if name ~= "included" and name ~= "static" then klass[name] = method end
end
if mixin.static then
for name,method in pairs(mixin.static) do
klass.static[name] = method
end
end end
if type(mixin.included)=="function" then mixin:included(klass) end if type(mixin.included)=="function" then mixin:included(klass) end
klass.__mixins[mixin] = true klass.__mixins[mixin] = true

View File

@ -10,6 +10,9 @@ context('A Mixin', function()
function Mixin1:included(theClass) theClass.includesMixin1 = true end function Mixin1:included(theClass) theClass.includesMixin1 = true end
function Mixin1:foo() return 'foo' end function Mixin1:foo() return 'foo' end
function Mixin1:bar() return 'bar' end function Mixin1:bar() return 'bar' end
Mixin1.static = {}
Mixin1.static.bazzz = function() return 'bazzz' end
function Mixin2:baz() return 'baz' end function Mixin2:baz() return 'baz' end
@ -26,7 +29,7 @@ context('A Mixin', function()
test('has all its functions (except "included") copied to its target class', function() test('has all its functions (except "included") copied to its target class', function()
assert_equal(Class1:bar(), 'bar') assert_equal(Class1:bar(), 'bar')
assert_equal(Class1.included, nil) assert_nil(Class1.included)
end) end)
test('makes its functions available to subclasses', function() test('makes its functions available to subclasses', function()
@ -41,5 +44,10 @@ context('A Mixin', function()
assert_equal(Class2:bar2(), 'bar2') assert_equal(Class2:bar2(), 'bar2')
end) end)
test('makes new static methods available in classes', function()
assert_equal(Class1:bazzz(), 'bazzz')
assert_equal(Class2:bazzz(), 'bazzz')
end)
end) end)