Module:File link: Difference between revisions

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)
Line 228:
end
local readOnlyFieldsprivateFields = {
theName = true,
theFormat = true,
Line 243:
theCaption = true
}
local readOnlyFields = {}
for field in pairs(data) do
readOnlyFields[field] = true
Line 248 ⟶ 250:
setmetatable(obj, {
__index = datafunction (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 readOnlyFieldsprivateFields[key] then
error(string.format(
"image object field '%s' is read-onlyprivate",
tostring(key)
), 2)
elseif readOnlyFields[key] then
error(string.format(
"image object field '%s' is read-only",
tostring(key)
), 2)
Line 261 ⟶ 277:
__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
})