From c2e4fce04360fb0e19fdbd4e66e45d73bd3e1526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sun, 18 Sep 2011 17:40:50 +0200 Subject: [PATCH] mixins now support static methods --- middleclass.lua | 7 ++++++- spec/mixins_spec.lua | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/middleclass.lua b/middleclass.lua index cb0c9fc..78fd162 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -63,7 +63,12 @@ end local function _includeMixin(klass, mixin) assert(type(mixin)=='table', "mixin must be a table") 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 if type(mixin.included)=="function" then mixin:included(klass) end klass.__mixins[mixin] = true diff --git a/spec/mixins_spec.lua b/spec/mixins_spec.lua index 1028dac..45ff47d 100644 --- a/spec/mixins_spec.lua +++ b/spec/mixins_spec.lua @@ -10,6 +10,9 @@ context('A Mixin', function() function Mixin1:included(theClass) theClass.includesMixin1 = true end function Mixin1:foo() return 'foo' end function Mixin1:bar() return 'bar' end + Mixin1.static = {} + Mixin1.static.bazzz = function() return 'bazzz' 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() assert_equal(Class1:bar(), 'bar') - assert_equal(Class1.included, nil) + assert_nil(Class1.included) end) test('makes its functions available to subclasses', function() @@ -41,5 +44,10 @@ context('A Mixin', function() assert_equal(Class2:bar2(), 'bar2') end) + test('makes new static methods available in classes', function() + assert_equal(Class1:bazzz(), 'bazzz') + assert_equal(Class2:bazzz(), 'bazzz') + end) + end)