summaryrefslogtreecommitdiffstats
path: root/acf/model/node.lua
blob: d038e6e6e9f6de5fcfbfdf190c87b6cf96929b21 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]

module(..., package.seeall)

local raise = require('acf.error').raise
local object = require('acf.object')
local class = object.class
local super = object.super

local pth = require('acf.path')
local util = require('acf.util')


BoundMember = class()

function BoundMember:init(parent, field)
   local pmt = getmetatable(parent)
   local mt = {}

   function mt.__index(t, k)
      local member = field[k]
      if type(member) ~= 'function' then return member end
      return function(self, ...)
		local name
		if field.name then name = field.name
		else
		   name = arg[1]
		   table.remove(arg, 1)
		end
		return member(
		   field,
		   {
		      txn=pmt.txn,
		      parent=parent,
		      path=pth.join(pmt.path, name),
		      addr=pth.to_absolute(
			 field.addr or pth.escape(name), pmt.addr
		      )
		   },
		   unpack(arg)
		)
	     end
   end

   setmetatable(self, mt)
end


TreeNode = class()

function TreeNode:init(context)
   local mt = getmetatable(self)
   util.update(mt, context)

   mt.meta = {}
   function mt.get(k, create) return mt.load(k, create) end

   if not mt.txn then return end

   if mt.parent then
      mt.meta['ui-name'] = getmetatable(mt.parent).mmeta(
	 pth.name(mt.path)
      )['ui-name']
   end

   function mt.save(k, v) rawset(self, k, v) end
   function mt.__index(t, k) return mt.get(k) end
   function mt.__newindex(t, k, v) mt.save(k, v) end

   function mt.has_permission(user, permission)
      local p = permission..mt.path
      if mt.txn:fetch('/auth/permissions')[p] then
	 return user:check_permission(p)
      end

      if ({create=true, delete=true})[permission] then
	 permission = 'modify'
      end
      return has_permission(mt.parent, user, permission)
   end

   mt.txn.validable[mt.path] = mt.addr
end

function TreeNode:fetch(path, create)
   if type(path) == 'string' then path = pth.split(path) end

   if #path == 0 then return self end

   local mt = getmetatable(self)
   local name = path[1]
   if not mt.valid_member(name) then
      raise(mt.path, 'Member does not exist: '..name)
   end

   local next = mt.get(name, create)
   if next == nil and (not create or #path > 1) then
      raise(mt.path, 'Subordinate does not exist: '..name)
   end

   table.remove(path, 1)
   if #path > 0 and type(next) ~= 'table' then
      raise(pth.join(mt.path, name), 'Is a primitive value')
   end

   return TreeNode.fetch(next, path, create)
end


Collection = class(TreeNode)

function Collection:init(context, params)
   super(self, Collection):init(context)

   self.init = nil
   self.fetch = nil

   local field = BoundMember(self, params.field)

   local mt = getmetatable(self)
   function mt.topology() return field:topology(pth.wildcard) end
   function mt.valid_member(name) return true end
   function mt.load(k, create) return field:load(k, create) end

   if not mt.txn then return end

   mt.meta.type = 'collection'
   mt.meta.members = field:meta(pth.wildcard)
   mt.meta['ui-member'] = params.ui_member or string.gsub(
      mt.meta['ui-name'], 's$', ''
   )

   function mt.mmeta(name)
      local res = util.copy(mt.meta.members)
      res['ui-name'] = mt.meta['ui-member']..' '..name
      return res
   end

   function mt.members() return mt.txn:get(mt.addr) or {} end

   function mt.validate()
      if #mt.members() > 0 then return end
      if params.required then raise(mt.path, 'Collection cannot be empty') end
      if params.destroy then
	 mt.txn:set(mt.addr)
	 validate(mt.parent)
      end
   end
   
   function mt.save(k, v) field:save(k, v) end
end


List = class(Collection)

function List:init(context, params)
   super(self, List):init(context, params)

   local mt = getmetatable(self)
   mt.meta.type = 'list'

   local save = mt.save
   function mt.save(k, v)
      assert(type(k) == 'number')
      if v == nil then
	 local len = #mt.members()
	 while k < len do
	    mt.save(k, mt.load(k + 1))
	    k = k + 1
	 end
      end
      save(k, v)
   end

   function mt.insert(v, i)
      local len = #mt.members()
      if not i then i = len + 1 end
      for j = len,i,-1 do mt.save(j + 1, mt.load(j)) end
      mt.save(i, v)
   end
end


-- experimental
Mixed = class(Collection)

function Mixed:init(context, params)
   super(self, Mixed):init(context, params)

   -- TODO dynamic meta: list non-leaf children
   local mt = getmetatable(self)
   mt.meta = {type='mixed', ['ui-name']=mt.path}
   function mt.mmeta(name)
      return {type='mixed', ['ui-name']=pth.join(mt.path, name)}
   end
end


local function meta_func(attr)
   return function(node, ...)
	     local res = getmetatable(node)[attr]
	     if type(res) == 'function' then return res(unpack(arg)) end
	     return res
	  end
end

addr = meta_func('addr')
has_permission = meta_func('has_permission')
insert = meta_func('insert')
members = meta_func('members')
meta = meta_func('meta')
mmeta = meta_func('mmeta')
parent = meta_func('parent')
path = meta_func('path')
topology = meta_func('topology')
validate = meta_func('validate')


local rawpairs = pairs
local rawipairs = ipairs

function pairs(tbl)
   if not object.isinstance(tbl, TreeNode) then return rawpairs(tbl) end
   local mt = getmetatable(tbl)
   local res = {}
   for _, member in rawipairs(mt.members()) do res[member] = mt.load(member) end
   return rawpairs(res)
end

local function _ipairs(mt, i)
   i = i + 1
   local v = mt.load(i)
   if v == nil then return end
   return i, v
end
function ipairs(tbl)
   if not object.isinstance(tbl, TreeNode) then return rawipairs(tbl) end
   return _ipairs, getmetatable(tbl), 0
end