bug fixes: orgs load properly (metatable set), orgs can't have same name, orgs now saved on modifying operations

This commit is contained in:
Luke Miller 2021-04-16 20:27:31 -04:00
parent 6bc5f4077e
commit cff1d393c0
3 changed files with 28 additions and 13 deletions

View File

@ -8,10 +8,6 @@
-- Main API table
if not modpol then modpol = {} end
-- Table for modpol data
modpol.orgs = {
}
-- Record of every state change should appear here
modpol.ledger = {
}
@ -77,10 +73,8 @@ dofile (topdir .. "/api.lua")
-- Final checks
-- create instance if not present
if not modpol.orgs.get_org('instance') then
modpol.orgs.init_instance()
modpol.instance = modpol.orgs.get_org('instance')
end
modpol.instance = modpol.orgs.array[1] or modpol.orgs.init_instance()
modpol.ocutil.log ("modpol loaded")

View File

@ -1,4 +1,4 @@
modpol.orgs =
modpol.orgs = modpol.orgs or
{
count = 1,
array = {}
@ -46,6 +46,8 @@ function modpol.orgs.reset()
modpol.orgs.array[id] = nil
end
end
modpol.orgs:record('Resetting all orgs', 'org_reset')
end
-- ===================================================
@ -74,6 +76,9 @@ function modpol.orgs.init_instance()
-- adding instance to org list
modpol.orgs.array[1] = instance
modpol.orgs:record('Initialized the instance org', 'create_instance')
return instance
end
@ -129,6 +134,12 @@ function modpol.orgs:add_org(name)
return false, error_msg
end
if modpol.orgs.get_org(name) then
error_msg = 'Error: org name is already being used'
modpol.ocutil.log(error_msg)
return false, error_msg
end
-- creating the child sub org
modpol.orgs.count = modpol.orgs.count + 1
local child_org = {
@ -147,6 +158,7 @@ function modpol.orgs:add_org(name)
-- adding child to org list
modpol.orgs.array[child_org.id] = child_org
self:record('created sub org ' .. name, 'add_org')
return child_org
end
@ -171,6 +183,8 @@ function modpol.orgs:delete()
modpol.orgs.array[self.id] = 'removed'
print('Removed ' .. self.name .. ': ' .. self.id)
self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org')
end
@ -195,6 +209,7 @@ function modpol.orgs:add_member(user)
-- adds to end if no empty spots
table.insert(self.members, user)
end
self.record('Added member ' .. user, 'add_member')
end
-- =======================================
@ -206,6 +221,7 @@ function modpol.orgs:remove_member(user)
if user_index then
self.members[user_index] = ''
end
self.record('Removed member ' .. user, 'del_member')
end
-- ===========================================

View File

@ -49,7 +49,7 @@ local store_orgs = function()
modpol.ocutil.fatal_error ("store_data: orgs")
end
local nn = modpol.ocutil.table_length (modpol.orgs)
local nn = modpol.ocutil.table_length (modpol.orgs.array)
local str = "entries"
if nn == 1 then str = "entry" end
modpol.ocutil.log (nn .. " orgs stored to disk")
@ -80,7 +80,7 @@ end
modpol.store_data = function()
store_ledger()
store_orgs()
store_old_ledgers()
-- store_old_ledgers()
end
-- ===================================================================
@ -116,7 +116,12 @@ local load_orgs = function()
end
modpol.orgs = func()
local nn = modpol.ocutil.table_length (modpol.orgs)
-- this block resets the metatable after being loaded in so that the class functions work
for id, org in ipairs(modpol.orgs.array) do
setmetatable(org, modpol.orgs)
end
local nn = modpol.ocutil.table_length (modpol.orgs.array)
local str = "entries"
if nn == 1 then str = "entry" end
modpol.ocutil.log (nn .. " orgs loaded from disk")
@ -152,7 +157,7 @@ end
modpol.load_storage = function()
load_ledger()
load_orgs()
load_old_ledgers()
-- load_old_ledgers()
end
-- ===================================================================