mirror of
https://github.com/kikito/middleclass.git
synced 2024-10-05 23:24:17 +00:00
Added setterFor & getterFor methods
This commit is contained in:
parent
395e2d7498
commit
b9eb2813a5
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user