summaryrefslogtreecommitdiffstats
path: root/aconf/model/root.lua
blob: 301e493982cb01635eb26510fe97c6f6e4616eac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
--[[
Copyright (c) 2012-2016 Kaarle Ritvanen
See LICENSE file for license details
--]]

--- @module aconf.model
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(context)
   object.super(self, M.RootModel):init(
      util.update(context, {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 order = 0
M.topology = setmetatable(
   {},
   {
      __call=function(self, addr, create)
	 util.setdefaults(self, {members={}, paths={}, referrers={}})

	 if type(addr) == 'table' then addr = util.copy(addr)
	 else addr = pth.split(addr) end
	 if not addr[1] then return self end

	 local comp = pth.escape(addr[1])
	 local top
	 if create then
	    top = setdefault(self.members, comp, {order=order})
	    order = order + 1
	 else
	    top = self.members[comp] or self.members['*']
	    if not top then return end
	 end
	 table.remove(addr, 1)

	 return setmetatable(top, getmetatable(self))(addr, create)
      end
   }
)

--- inject a new field to the root model.
-- @tparam string name name of the field
-- @param field (<i>[&lt;Field&gt;](#Overview_of_field_classes)</i> or
-- <i>[&lt;Model&gt;](#new)</i>) type of the field, specified by a
-- field class or instance. Can also be specified by a model.
-- @tparam ?{[string]=any,...} params field parameters. Applicable
-- when the type of the field is specified by a model or a field
-- class.
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, t)
	 if not t then t = top end
	 setdefault(t, k, v)
	 assert(t[k] == v)
      end

      set('type', record.type)
      set('subtype', record.subtype)
      table.insert(top.paths, record.path)

      local mode = record.be_mode
      if mode then
	 if type(mode) == 'string' then mode = {['.']=mode} end
	 for addr, m in pairs(mode) do
	    set('mode', m, addr == '.' and top or top(addr, true))
	 end
      end

      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(pth.rawjoin(scope, record.search), true).referrers,
	    record.path
	 )
      end
   end
end


return M