From 1df9ccf178df16f3e5b159ef85bba4c1ae071fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sun, 14 Aug 2011 18:39:57 +0200 Subject: [PATCH] added list of subclasses per class --- middleclass.lua | 2 ++ spec/Object_spec.lua | 4 ++++ spec/classes_spec.lua | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/middleclass.lua b/middleclass.lua index 5fcc4b3..cb0c9fc 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -31,6 +31,7 @@ end local function _createClass(name, super) local klass = { name = name, super = super, static = {}, __mixins = {}, __instanceDict={} } + klass.subclasses = setmetatable({}, {__mode = "k"}) _setClassDictionariesMetatables(klass) _setClassMetatable(klass) @@ -91,6 +92,7 @@ function Object.static:subclass(name) local subclass = _createClass(name, self) _setClassMetamethods(subclass) _setDefaultInitializeMethod(subclass, self) + self.subclasses[subclass] = true self:subclassed(subclass) return subclass diff --git a/spec/Object_spec.lua b/spec/Object_spec.lua index 4232aa2..40cb01a 100644 --- a/spec/Object_spec.lua +++ b/spec/Object_spec.lua @@ -48,6 +48,10 @@ context('Object', function() assert_equal(SubClass.super, Object) end) + test('it includes the subclass in the list of subclasses', function() + assert_true(Object.subclasses[SubClass]) + end) + end) end) diff --git a/spec/classes_spec.lua b/spec/classes_spec.lua index 1c526cc..51a6274 100644 --- a/spec/classes_spec.lua +++ b/spec/classes_spec.lua @@ -69,6 +69,10 @@ context('A Class', function() assert_equal(SubClass, AClass.child) end) + test('it includes the subclass in the list of subclasses', function() + assert_true(AClass.subclasses[SubClass]) + end) + end) end)