Module:CompoundEntityAbstract

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(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(opts) end return util_table.concat(tbl, opts.sep) end

function p:get(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:get(length) end return util_table.concat(tbl, opts.sep) end

function p:links(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:link(opts) end return util_table.concat(tbl, opts.sep) end

function p:flairs(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(opts) end return util_table.concat(tbl, opts.sep) end

function p:flairslinked(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(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 or '') 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

function p:has(len, str) for _, v in ipairs(self) do		if v:get(len) == str then return true end end return false end

function p:_or(entity) if self.is_nil then return entity end return self end

return p