Jump to content

Module:File link: Difference between revisions

521 bytes added ,  10 years ago
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: Line 228:
end
end
local readOnlyFields = {
local privateFields = {
theName = true,
theName = true,
theFormat = true,
theFormat = true,
Line 243: Line 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
Line 248: Line 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)
Line 261: Line 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
})
})
Cookies help us deliver our services. By using our services, you agree to our use of cookies.