Added setterFor & getterFor methods

This commit is contained in:
kikito 2010-01-15 23:42:20 +00:00
parent 395e2d7498
commit b9eb2813a5

View File

@ -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