Changes

Module:Arguments

429 bytes added, 04:03, 9 December 2013
use an array to hold argument tables rather than firstArgs and secondArgs variables
-- Get the arguments from the frame object if available. If the frame object is not available, we are being called
-- from another Lua module or from the debug console, so assume arguments are passed directly put the args ina special table so we can differentiate them. local fargs, pargs, luaArgs
if frame == mw.getCurrentFrame() then
fargs = frame.args
pargs = frame:getParent().args
else
fargs luaArgs = type(frame) == 'table' and frame or {} pargs = {}
end
-- Set up the args and metaArgs tables. args will be the one accessed from functions, and metaArgs will hold the actual arguments.
-- The metatable connects the two together.
local args, metaArgs, metatable = {}, {}, {}
setmetatable(args, metatable)
end
-- Use a user-generated functions function to tidy the values if specified.
local valueFunc = options.valueFunc
if valueFunc then
end
local function mergeArgs(iterator, ...tables)
-- Accepts multiple tables as input and merges their keys and values into one table using the specified iterator.
-- If a value is already present it is not overwritten; tables listed earlier have precendence.
local tables = {...}
for _, t in ipairs(tables) do
for key, val in iterator(t) do
end
-- Set the order of precedence of the argument tables. If the variables are nil, nothing will be added to the table, -- which is how we avoid clashes between the frame /parent args and parent the Lua args. local firstArgs, secondArgs argTables = fargs, pargs{}
if options.parentFirst then
firstArgstable.insert(argTables, secondArgs = pargs) table.insert(argTables, fargs) else table.insert(argTables, fargs) table.insert(argTables, pargs)
end
table.insert(argTables, luaArgs)
--[[
-- Define metatable behaviour. Arguments are stored in the metaArgs table, and are only fetched from the
-- fargs and pargs argument tables once. Also, we keep a record in the metatable of when pairs and ipairs havebeen called, -- been called, so we do not run pairs and ipairs on fargs and pargs more than once. We also do not runipairs on fargs -- ipairs on fargs and pargs if pairs has already been run, as all the arguments will already have been -- copied over.
--]]
metatable.__index = function (t, key)
return val
else
for i, argTable in ipairs(argTables) do local firstVal argTableVal = tidyVal(key, firstArgsargTable[key]) if firstVal argTableVal ~= nil then metaArgs[key] = argTableVal return firstVal elseargTableVal return tidyVal(key, secondArgs[key])end
end
end
metatable.__pairs = function ()
if not metatable.donePairs then
mergeArgs(pairs, firstArgs, secondArgsargTables)
metatable.donePairs = true
metatable.doneIpairs = true
metatable.__ipairs = function ()
if not metatable.doneIpairs then
mergeArgs(ipairs, firstArgs, secondArgsargTables)
metatable.doneIpairs = true
end
Anonymous user