Changes
Jump to navigation
Jump to search
seperate treatment of private fields and read-only fields, and add a __pairs metamethod
end
local readOnlyFields privateFields = {
theName = true,
theFormat = true,
theCaption = true
}
local readOnlyFields = {}
for field in pairs(data) do
readOnlyFields[field] = true
setmetatable(obj, {
__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)
if privateFields[key] then error(string.format( "image object field '%s' is private", tostring(key) ), 2) elseif readOnlyFields[key] then
error(string.format(
"image object field '%s' is read-only",
tostring(key)
), 2)
__tostring = function (t)
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
})