added module template, added support for module parameters and default values through the config table
This commit is contained in:
parent
826f9600b5
commit
6e02e254aa
29
modpol/modules/template.lua
Normal file
29
modpol/modules/template.lua
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
|
||||
Module = {}
|
||||
|
||||
Module.setup = {
|
||||
name = "Module Name",
|
||||
desc = "Description of the module"
|
||||
}
|
||||
|
||||
-- defines the input parameters to the module initiate function
|
||||
-- when calling a module from within another module
|
||||
-- variables not defined in config will be ignored
|
||||
-- default values set in config can be overridden
|
||||
Module.config = {
|
||||
votes_required = 5
|
||||
voting_type = "majority"
|
||||
}
|
||||
|
||||
-- Modules have access to the following instance variables
|
||||
-- self.org (the org the module was called in)
|
||||
-- self.initiator (the user that callced the module)
|
||||
-- self.id (the process id of the module instance)
|
||||
|
||||
function Module:initiate(config, result)
|
||||
-- call interaction functions here!
|
||||
|
||||
-- call result function
|
||||
if result then result() end
|
||||
end
|
@ -1,4 +1,4 @@
|
||||
function modpol.orgs:call_module(module_name, initiator)
|
||||
function modpol.orgs:call_module(module_name, initiator, config, result)
|
||||
if not modpol.modules[module_name] then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. name .. '" not found')
|
||||
return
|
||||
@ -23,13 +23,15 @@ function modpol.orgs:call_module(module_name, initiator)
|
||||
|
||||
local module = modpol.modules[module_name]
|
||||
|
||||
-- setting default params
|
||||
local new_process = {
|
||||
metatable = {__index = module},
|
||||
initiator = initiator,
|
||||
org = self,
|
||||
id = index
|
||||
}
|
||||
|
||||
|
||||
-- copying default fields from setup
|
||||
for k, v in pairs(module.setup) do
|
||||
new_process[k] = v
|
||||
end
|
||||
@ -38,7 +40,14 @@ function modpol.orgs:call_module(module_name, initiator)
|
||||
|
||||
self.processes[index] = new_process
|
||||
|
||||
self.processes[index]:initiate()
|
||||
-- sets default values for undeclared config variables
|
||||
for k, v in pairs(module.config) do
|
||||
if config[k] == nil then
|
||||
config[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
self.processes[index]:initiate(config, result))
|
||||
|
||||
return index
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user