changed modpol.orgs.list to modpol.orgs.array, added support for id or org name in get_org function

This commit is contained in:
Luke Miller 2021-04-12 16:55:24 -04:00
parent 08122f7a6a
commit bc3aba4ff0

View File

@ -1,23 +1,30 @@
modpol.orgs =
{
count = 1,
list = {}
array = {}
}
-- sets modpol.orgs as it's own fallback
modpol.orgs.__index = modpol.orgs
-- ==================================================
-- returns org when given its id
-- returns org when given its id or name
function modpol.orgs.get_org(id)
return modpol.orgs.list[id]
if type(id) == 'string' then
for id, org in ipairs(modpol.orgs.array) do
if org.name == id then
return org
end
end
elseif type(id) == 'number' then
return modpol.orgs.array[id]
end
-- ===============================================
-- returns a string list of all orgs
function modpol.orgs.list_orgs()
function modpol.orgs.list_all()
local str
for k, v in ipairs(modpol.orgs.list) do
for k, v in ipairs(modpol.orgs.array) do
if type(v) == 'table' then
if str then
str = str .. '\n' .. v.name
@ -32,9 +39,9 @@ end
-- ===========================================
-- deletes all orgs except for the instance
function modpol.orgs.reset()
for k, v in ipairs(modpol.orgs.list) do
for k, v in ipairs(modpol.orgs.array) do
if k > 1 then
modpol.orgs.list[k] = nil
modpol.orgs.array[k] = nil
end
end
end
@ -44,7 +51,7 @@ end
-- can only be run once, as only one instance can exist
function modpol.orgs.init_instance()
local error_msg
if modpol.orgs.list[1] then
if modpol.orgs.array[1] then
error_msg = 'Error: instance has already been initialized'
modpol.ocutil.log(error_msg)
return false, error_msg
@ -64,7 +71,7 @@ function modpol.orgs.init_instance()
setmetatable(instance, modpol.orgs)
-- adding instance to org list
modpol.orgs.list[1] = instance
modpol.orgs.array[1] = instance
return instance
end
@ -136,7 +143,7 @@ function modpol.orgs:add_org(name)
table.insert(self.children, child_org.id)
-- adding child to org list
modpol.orgs.list[child_org.id] = child_org
modpol.orgs.array[child_org.id] = child_org
return child_org
@ -144,7 +151,7 @@ end
-- ========================================
-- recursively deletes an org and its suborgs
-- leaves entry in modpol.orgs.list as a string "removed"
-- leaves entry in modpol.orgs.array as a string "removed"
-- note: "reason" param was removed, can be added back
function modpol.orgs:delete()
if self.id == 1 then
@ -159,7 +166,7 @@ function modpol.orgs:delete()
end
end
modpol.orgs.list[self.id] = 'removed'
modpol.orgs.array[self.id] = 'removed'
print('Removed ' .. self.name .. ': ' .. self.id)
end