Modul:File link: Unterschied zwischen den Versionen
seperate treatment of private fields and read-only fields, and add a __pairs metamethod
(enable call-chaining and tweak the error messages to use "image:method" rather than just "method") |
(seperate treatment of private fields and read-only fields, and add a __pairs metamethod) |
||
Zeile 228: | Zeile 228: | ||
end | end | ||
local | local privateFields = { | ||
theName = true, | theName = true, | ||
theFormat = true, | theFormat = true, | ||
Zeile 243: | Zeile 243: | ||
theCaption = true | theCaption = true | ||
} | } | ||
local readOnlyFields = {} | |||
for field in pairs(data) do | for field in pairs(data) do | ||
readOnlyFields[field] = true | readOnlyFields[field] = true | ||
Zeile 248: | Zeile 250: | ||
setmetatable(obj, { | setmetatable(obj, { | ||
__index = data, | __index = function (t, key) | ||
if privateFields[key] then | |||
error(string.format( | |||
"image object field '%s' is private", | |||
tostring(key) | |||
), 2) | |||
else | |||
return data[key] | |||
end | |||
end, | |||
__newindex = function (t, key, value) | __newindex = function (t, key, value) | ||
if readOnlyFields[key] then | if privateFields[key] then | ||
error(string.format( | |||
"image object field '%s' is private", | |||
tostring(key) | |||
), 2) | |||
elseif readOnlyFields[key] then | |||
error(string.format( | error(string.format( | ||
"field '%s' is read-only", | "image object field '%s' is read-only", | ||
tostring(key) | tostring(key) | ||
), 2) | ), 2) | ||
Zeile 261: | Zeile 277: | ||
__tostring = function (t) | __tostring = function (t) | ||
return t:render() | return t:render() | ||
end, | |||
__pairs = function () | |||
local temp = {} | |||
for k, v in pairs(data) do | |||
if not privateFields[k] then | |||
temp[k] = v | |||
end | |||
end | |||
return pairs(temp) | |||
end | end | ||
}) | }) |