From b9eb2813a510dbfa86070a23c78c7d24fee9e1cc Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 15 Jan 2010 23:42:20 +0000 Subject: [PATCH] Added setterFor & getterFor methods --- MiddleClass.lua | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/MiddleClass.lua b/MiddleClass.lua index 53e2b96..01da160 100644 --- a/MiddleClass.lua +++ b/MiddleClass.lua @@ -72,25 +72,6 @@ Object = { name = "Object", } -do - local capitalize = function(s) - return string.gsub (s, "(%w)([%w]*)", function (first, rest) return string.upper(first) .. rest end, 1) - end - - Object.getter = function(class, attributeName, defaultValue) - class['get' .. capitalize(attributeName)] = function(self) return self[attributeName] or defaultValue end - end - - Object.setter = function(class, attributeName) - class['set' .. capitalize(attributeName)] = function(self, value) self[attributeName] = value end - end - - Object.getterSetter = function(class, attributeName, defaultValue) - class:getter(attributeName, defaultValue) - class:setter(attributeName) - end -end - classes[Object]=Object -- adds Object to the list of classes Object.__classDict = { @@ -103,6 +84,28 @@ setmetatable(Object, { __index = Object.__classDict, __newindex = Object.__class __call = Object.new }) +do --internal capitalization stuff for getters and setters + local capitalize = function(s) + return string.gsub(s, "(%w)([%w]*)", function (first, rest) return string.upper(first) .. rest end, 1) + end + + Object.getterFor = function(class, attributeName) return 'get' .. capitalize(attributeName) end + Object.setterFor = function(class, attributeName) return 'set' .. capitalize(attributeName) end +end + +Object.getter = function(class, attributeName, defaultValue) + class[class:getterFor(attributeName)] = function(self) return self[attributeName] or defaultValue end +end + +Object.setter = function(class, attributeName) + class[class:setterFor(attributeName)] = function(self, value) self[attributeName] = value end +end + +Object.getterSetter = function(class, attributeName, defaultValue) + class:getter(attributeName, defaultValue) + class:setter(attributeName) +end + -- Returns true if class is a subclass of other, false otherwise function subclassOf(other, class) if class.superclass==nil then return false end --class is Object, or a non-class