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
243
244
245
246
247
248
249
250
251
|
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
local M = {}
M.error = require('acf2.error')
local raise = M.error.raise
local relabel = M.error.relabel
local combination = require('acf2.model.combination')
M.Union = combination.Union
M.Range = combination.Range
local fld = require('acf2.model.field')
local Field = fld.Field
M.Boolean = fld.Boolean
M.Integer = fld.Integer
M.Number = fld.Number
M.String = fld.String
local model = require('acf2.model.model')
M.Action = model.Action
M.new = model.new
local to_field = model.to_field
M.net = require('acf2.model.net')
local node = require('acf2.model.node')
M.node = {}
for _, m in ipairs{
'List',
'Set',
'TreeNode',
'contains',
'has_permission',
'insert',
'meta',
'mmeta',
'name',
'parent',
'path',
'pairs',
'ipairs'
} do M.node[m] = node[m] end
M.permission = require('acf2.model.permission')
M.register = require('acf2.model.root').register
M.service = require('acf2.model.service')
M.node.Set = require('acf2.model.set').Set
local object = require('acf2.object')
local class = object.class
local isinstance = object.isinstance
local super = object.super
local pth = require('acf2.path')
local DataStore = require('acf2.persistence')
local util = require('acf2.util')
local update = util.update
local stringy = require('stringy')
M.Reference = class(Field)
function M.Reference:init(params)
if not params.widget then params.widget = 'reference' end
super(self, M.Reference):init(params)
self.dtype = 'reference'
self.dereference = true
if not self.scope then self.scope = '/' end
self.filter = fld.conv_filter(self.filter)
end
function M.Reference:topology(context)
local res = super(self, M.Reference):topology(context)
res[1].scope = self.scope
return res
end
function M.Reference:abs_scope(context)
return pth.to_absolute(self.scope, node.path(context.parent))
end
-- assume one-level refs for now
function M.Reference:_choice(context)
local res = {}
local txn = context.txn
local obj = relabel('system', txn.fetch, txn, self:abs_scope(context))
assert(isinstance(obj, node.Collection))
for k, v in node.pairs(obj) do
local ch = {enabled=true}
if isinstance(v, node.TreeNode) then
ch.ref = node.path(v)
if pth.is_subordinate(context.path, ch.ref) then ch = nil end
if ch then
ch['ui-value'] = pth.name(ch.ref)
ch.value = self.dereference and ch.ref or pth.escape(ch['ui-value'])
if self.filter then
assert(isinstance(v, model.Model))
if not node.match(v, self.filter) then ch.enabled = false end
end
end
else update(ch, {value=pth.escape(v), ['ui-value']=v}) end
if ch then table.insert(res, ch) end
end
return res
end
function M.Reference:meta(context)
return update(
super(self, M.Reference):meta(context),
{scope=self:abs_scope(context), dynamic=self.filter and true or false}
)
end
function M.Reference:follow(context, value)
return context.txn:fetch(pth.rawjoin(self:abs_scope(context), value))
end
function M.Reference:load(context, options)
local ref = super(self, M.Reference):load(context)
return (
util.setdefault(
options or {}, 'dereference', self.dereference
) and context.txn and ref
) and self:follow(context, ref) or ref
end
function M.Reference:normalize(context, value)
if isinstance(value, node.TreeNode) then value = node.path(value) end
local path = context.path
if type(value) ~= 'string' then raise(path, 'Path name must be string') end
local rel = value
if pth.is_absolute(rel) then
local scope = self:abs_scope(context)
local prefix = scope..'/'
if not stringy.startswith(rel, prefix) then
raise(path, 'Reference out of scope ('..scope..')')
end
rel = rel:sub(prefix:len() + 1, -1)
end
-- assume one-level ref for now
if #pth.split(rel) > 1 then
raise(path, 'Subtree references not yet supported')
end
-- TODO check instance type
relabel(path, self.follow, self, context, rel)
return self.dereference and value or rel, rel
end
function M.Reference:deleted(context, addr)
local target = self:load(context)
if target and node.addr(target) == addr then
-- TODO raise error for the target object
raise(context.path, 'Refers to '..addr)
end
end
M.Model = fld.Model
M.Collection = class(fld.TreeNode)
function M.Collection:init(params, itype)
if params.create == nil then params.create = true end
super(self, M.Collection):init(params)
assert(self.type)
self.itype = itype or node.Collection
self.iparams = {
destroy=self.destroy,
editable=self:editable(),
layout=self.layout,
required=self.required,
ui_member=self.ui_member
}
self.dtype = 'collection'
end
function M.Collection:auto_ui_name(name)
if not name then return end
if name:sub(-1, -1) ~= 's' then name = name..'s' end
return super(self, M.Collection):auto_ui_name(name)
end
function M.Collection:load(context, options)
if not self.iparams.field then self.iparams.field = to_field(self.type) end
return super(self, M.Collection):load(context, options)
end
M.List = class(M.Collection)
function M.List:init(params) super(self, M.List):init(params, node.List) end
M.Set = class(M.Collection)
function M.Set:init(params)
if not params.widget and isinstance(params.type, M.Reference) then
params.widget = 'checkboxes'
end
super(self, M.Set):init(params, M.node.Set)
end
function M.Set.save_member(tn, k, v) node.insert(tn, v) end
-- experimental
M.Mixed = class(M.Collection)
function M.Mixed:init(params)
params.type = M.Mixed
super(self, M.Mixed):init(params, node.Mixed)
self.pfield = Field()
end
function M.Mixed:topology(context) return {} end
function M.Mixed:load(context)
local value = self.pfield:load(context)
if type(value) == 'table' then return super(self, M.Mixed):load(context) end
return value
end
function M.Mixed:save(context, value)
if type(value) == 'table' then super(self, M.Mixed):save(context, value)
else self.pfield:save(context, value) end
end
function M.trigger(phase, addr, func) DataStore:trigger(phase, addr, func) end
return M
|