summaryrefslogtreecommitdiffstats
path: root/aconf/model/root.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-03-10 22:45:18 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-03-24 01:18:13 +0200
commit7d9c43916b0600ac4879dfe9793eab807a83ab2b (patch)
treeec54ed64c9a557b6ea4ad88d31138a02d3e0cd04 /aconf/model/root.lua
parentcb6c243dc356ef1d46d7ddb96e6ea6ae007c6cca (diff)
downloadaconf-7d9c43916b0600ac4879dfe9793eab807a83ab2b.tar.bz2
aconf-7d9c43916b0600ac4879dfe9793eab807a83ab2b.tar.xz
rename ACF2 to Alpine Configurator (aconf)
Diffstat (limited to 'aconf/model/root.lua')
-rw-r--r--aconf/model/root.lua89
1 files changed, 89 insertions, 0 deletions
diff --git a/aconf/model/root.lua b/aconf/model/root.lua
new file mode 100644
index 0000000..027b2e2
--- /dev/null
+++ b/aconf/model/root.lua
@@ -0,0 +1,89 @@
+--[[
+Copyright (c) 2012-2014 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+local M = {}
+
+local model = require('aconf.model.model')
+local node = require('aconf.model.node')
+local object = require('aconf.object')
+local pth = require('aconf.path')
+
+local util = require('aconf.util')
+local setdefault = util.setdefault
+
+
+M.RootModel = model.new()
+
+function M.RootModel:init(txn)
+ object.super(self, M.RootModel):init{txn=txn, path='/', addr='/null'}
+end
+
+function M.RootModel:has_permission(user, permission)
+ return permission == 'read'
+end
+
+function M.RootModel:meta(path)
+ local obj = self:fetch(path, true)
+ if object.isinstance(obj, node.TreeNode) then return node.meta(obj) end
+ return node.mmeta(self:fetch(pth.parent(path), true), pth.name(path))
+end
+
+
+local _topology = {}
+local order = 0
+
+function M.topology(addr, create)
+ local top = _topology
+ if type(addr) == 'table' then addr = util.copy(addr)
+ else addr = pth.split(addr) end
+
+ local function defaults(top)
+ return util.setdefaults(top, {members={}, paths={}, referrers={}})
+ end
+
+ while #addr > 0 do
+ if create then
+ top = setdefault(defaults(top).members, addr[1], {order=order})
+ order = order + 1
+ else
+ top = top.members[addr[1]] or top.members[pth.wildcard]
+ if not top then return end
+ end
+ table.remove(addr, 1)
+ end
+
+ return defaults(top)
+end
+
+function M.register(name, field, params)
+ if not params then params = {} end
+ params.create = true
+ M.RootModel[name] = model.to_field(field, params)
+
+ local root = M.RootModel()
+
+ for _, record in ipairs(node.topology(root:fetch(name))) do
+ local top = M.topology(record.addr, true)
+
+ local function set(k, v)
+ setdefault(top, k, v)
+ assert(top[k] == v)
+ end
+
+ set('type', record.type)
+ table.insert(top.paths, record.path)
+
+ if record.scope then
+ local scope = node.addr(
+ root:fetch(pth.to_absolute(record.scope, pth.parent(record.path)))
+ )
+ set('scope', scope)
+ table.insert(M.topology(scope, true).referrers, record.path)
+ end
+ end
+end
+
+
+return M