[checked revision] | [checked revision] |
([ST] + comment) |
([ST] fix is_nil for alreadyCast) |
||
Line 18: | Line 18: | ||
if not opts then opts = {} end |
if not opts then opts = {} end |
||
self.sep = opts.sep or ',' |
self.sep = opts.sep or ',' |
||
− | if not strs then |
+ | if not strs or (opts.alreadyCast and #strs == 0) then |
self.is_nil = true |
self.is_nil = true |
||
self.objs = {} |
self.objs = {} |
Revision as of 10:12, 18 June 2020
Documentation for this module may be created at Module:EntityListAbstract/doc
local util_args = require('Module:ArgsUtil')
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')
local LCS = require('Module:LuaClassSystem')
local p = LCS.class()
local h = {}
p.Entity = nil
function p:init(strs, opts)
if not opts then opts = {} end
self.sep = opts.sep or ','
if not strs or (opts.alreadyCast and #strs == 0) then
self.is_nil = true
self.objs = {}
return
end
if type(strs) == 'string' then
strs = util_text.split(strs, self.sep)
end
-- so we can build a compound role from already-created objects
-- instead of first having to cast back as string
if opts.alreadyCast then
self.objs = strs
else
self.objs = self:castEntities(strs, opts)
end
end
function p:castEntities(strs, opts)
if not self.Entity then
error('Missing self.Entity, cannot cast')
end
local ret = {}
for _, str in ipairs(strs) do
ret[#ret+1] = self.Entity(str, opts)
end
return ret
end
function p.__ipairs(tbl)
local function stateless_iter(tbl, i)
-- Implement your own index, value selection logic
i = i + 1
local val = tbl.objs[i]
if val ~= nil then return i, val end
end
-- return iterator function, table, and starting point
return stateless_iter, tbl, 0
end
function p:exists()
return not self.is_nil
end
function p:tostring()
if self.is_nil then return end
if self.objs then
return util_table.concat(self, self.sep, tostring)
end
return 'Attempting to tostring something we cannot'
end
function p:names(length, opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:name(length, opts)
end
return util_table.concat(tbl, opts.sep)
end
function p:linked(length, opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:linked(length, opts)
end
return util_table.concat(tbl, opts.sep)
end
function p:flairs(length, opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:flair(length, opts)
end
return util_table.concat(tbl, opts.sep)
end
function p:flairslinked(length, opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:flairlinked(length, opts)
end
return util_table.concat(tbl, opts.sep)
end
function p:images(opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:image(opts)
end
return util_table.concat(tbl, opts.sep)
end
function p:imageslinked(opts)
if self.is_nil then return end
if not opts then opts = {} end
local tbl = {}
for _, obj in ipairs(self) do
tbl[#tbl+1] = obj:imagelinked(opts)
end
return util_table.concat(tbl, opts.sep)
end
return p