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
|
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
module(..., package.seeall)
local err = require('acf.error')
local raise = err.raise
local node = require('acf.model.node')
local object = require('acf.object')
local class = object.class
local super = object.super
local map = require('acf.util').map
local function contains(list, value)
for k, v in ipairs(list) do if v == value then return true end end
return false
end
local function auto_ui_name(name)
if not name then return end
return string.gsub(string.upper(string.sub(name, 1, 1))..string.sub(name, 2),
'_', ' ')
end
Member = class()
function Member:init(params)
for k, v in pairs(params or {}) do
if self[k] == nil then self[k] = v end
end
end
function Member:meta(context)
return {
name=self.name,
description=self.description,
['ui-name']=self['ui-name'] or auto_ui_name(self.name)
}
end
Field = class(Member)
function Field:init(params)
super(self, Field):init(params)
if self.choice and not self['ui-choice'] then
self['ui-choice'] = map(auto_ui_name, self.choice)
end
if not self.widget then
self.widget = self.choice and 'combobox' or 'field'
end
end
function Field:meta(context)
assert(self.dtype)
local res = super(self, Field):meta(context)
res.type = self.dtype
res.required = self.required
res.default = self.default
res.choice = self.choice
res.widget = self.widget
res['ui-choice'] = self['ui-choice']
return res
end
function Field:btype(context) return self.dtype end
function Field:load(context)
local value = context.txn:get(context.addr, self:btype(context))
if value == nil then return self.default end
return value
end
function Field:_validate(context, value)
if self.required and value == nil then
raise(context.path, 'Required value not set')
end
if self.choice and value ~= nil and not contains(self.choice, value) then
raise(context.path, 'Invalid value')
end
if value ~= nil then self:validate(context, value) end
return value
end
function Field:validate(context, value) end
function Field:save(context, value)
context.txn:set(
context.addr, self:btype(context), self:_validate(context, value)
)
end
function Field:validate_saved(context)
self:save(context, self:load(context))
end
TreeNode = class(Field)
function TreeNode:save(context, value)
local path = context.path
-- TODO hack, allow preserving old instance on parent update
if value == path then return end
if object.isinstance(value, node.TreeNode) then
-- TODO clone if TreeNode has wrong path
if node.path(value) ~= path then
raise(path, 'Attempted to assign foreign object as value')
end
return
end
context.txn:set(context.addr)
if value then
if type(value) ~= 'table' then
raise(path, 'Cannot assign primitive value')
end
context.txn:set(context.addr, 'table')
local new = self:load(context, true)
local errors = err.ErrorDict()
for k, v in pairs(value) do
errors:collect(self.save_member, new, k, v)
end
errors:raise()
end
end
function TreeNode.save_member(node, k, v) node[k] = v end
Model = class(TreeNode)
function Model:init(params)
super(self, Model):init(params)
assert(self.model)
self.dtype = 'model'
self.widget = self.dtype
end
function Model:load(context, create)
if not (create or context.txn:get(context.addr, 'table')) then return end
return self.model(context)
end
|