changed modpol.orgs.list to modpol.orgs.array, added support for id or org name in get_org function
This commit is contained in:
parent
08122f7a6a
commit
bc3aba4ff0
@ -1,23 +1,30 @@
|
|||||||
modpol.orgs =
|
modpol.orgs =
|
||||||
{
|
{
|
||||||
count = 1,
|
count = 1,
|
||||||
list = {}
|
array = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- sets modpol.orgs as it's own fallback
|
-- sets modpol.orgs as it's own fallback
|
||||||
modpol.orgs.__index = modpol.orgs
|
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)
|
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
|
end
|
||||||
|
|
||||||
-- ===============================================
|
-- ===============================================
|
||||||
-- returns a string list of all orgs
|
-- returns a string list of all orgs
|
||||||
function modpol.orgs.list_orgs()
|
function modpol.orgs.list_all()
|
||||||
local str
|
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 type(v) == 'table' then
|
||||||
if str then
|
if str then
|
||||||
str = str .. '\n' .. v.name
|
str = str .. '\n' .. v.name
|
||||||
@ -32,9 +39,9 @@ end
|
|||||||
-- ===========================================
|
-- ===========================================
|
||||||
-- deletes all orgs except for the instance
|
-- deletes all orgs except for the instance
|
||||||
function modpol.orgs.reset()
|
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
|
if k > 1 then
|
||||||
modpol.orgs.list[k] = nil
|
modpol.orgs.array[k] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -44,7 +51,7 @@ end
|
|||||||
-- can only be run once, as only one instance can exist
|
-- can only be run once, as only one instance can exist
|
||||||
function modpol.orgs.init_instance()
|
function modpol.orgs.init_instance()
|
||||||
local error_msg
|
local error_msg
|
||||||
if modpol.orgs.list[1] then
|
if modpol.orgs.array[1] then
|
||||||
error_msg = 'Error: instance has already been initialized'
|
error_msg = 'Error: instance has already been initialized'
|
||||||
modpol.ocutil.log(error_msg)
|
modpol.ocutil.log(error_msg)
|
||||||
return false, error_msg
|
return false, error_msg
|
||||||
@ -64,7 +71,7 @@ function modpol.orgs.init_instance()
|
|||||||
setmetatable(instance, modpol.orgs)
|
setmetatable(instance, modpol.orgs)
|
||||||
|
|
||||||
-- adding instance to org list
|
-- adding instance to org list
|
||||||
modpol.orgs.list[1] = instance
|
modpol.orgs.array[1] = instance
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -136,7 +143,7 @@ function modpol.orgs:add_org(name)
|
|||||||
table.insert(self.children, child_org.id)
|
table.insert(self.children, child_org.id)
|
||||||
|
|
||||||
-- adding child to org list
|
-- adding child to org list
|
||||||
modpol.orgs.list[child_org.id] = child_org
|
modpol.orgs.array[child_org.id] = child_org
|
||||||
|
|
||||||
|
|
||||||
return child_org
|
return child_org
|
||||||
@ -144,7 +151,7 @@ end
|
|||||||
|
|
||||||
-- ========================================
|
-- ========================================
|
||||||
-- recursively deletes an org and its suborgs
|
-- 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
|
-- note: "reason" param was removed, can be added back
|
||||||
function modpol.orgs:delete()
|
function modpol.orgs:delete()
|
||||||
if self.id == 1 then
|
if self.id == 1 then
|
||||||
@ -159,7 +166,7 @@ function modpol.orgs:delete()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
modpol.orgs.list[self.id] = 'removed'
|
modpol.orgs.array[self.id] = 'removed'
|
||||||
print('Removed ' .. self.name .. ': ' .. self.id)
|
print('Removed ' .. self.name .. ': ' .. self.id)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user