summaryrefslogtreecommitdiffstats
path: root/acf2/model/field.lua
blob: 7a7595abc9a89fc9beb05968104779e4215f1df1 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]

local M = {}

local err = require('acf2.error')
local raise = err.raise

local node = require('acf2.model.node')

local object = require('acf2.object')
local class = object.class
local super = object.super

local util = require('acf2.util')
local map = util.map


M.Member = class()

function M.Member:init(params)
   for k, v in pairs(params or {}) do
      if self[k] == nil then self[k] = v end
   end
end

function M.Member:auto_ui_name(name)
   if not name then return end
   local res = (name:sub(1, 1):upper()..name:sub(2)):gsub('-', ' ')
   return res
end

function M.Member:meta(context)
   return {
      name=self.name,
      description=self.description,
      ['ui-name']=self.ui_name or self:auto_ui_name(self.name)
   }
end


function M.conv_filter(filter)
   return filter and map(
      function(values)
	 return type(values) == 'table' and values or {values}
      end,
      filter
   ) or nil
end


M.Field = class(M.Member)

function M.Field:init(params)
   if not params then params = {} end
   util.setdefault(params, 'editable', not params.compute)

   super(self, M.Field):init(params)

   if self.compute then self.addr = node.null_addr end

   if type(self.editable) ~= 'function' then
      function self.editable(context) return params.editable end
   end

   self.condition = M.conv_filter(self.condition)

   if self.choice then
      self.choice = map(
	 function(choice)
	    if type(choice) ~= 'table' then choice = {choice} end
	    for i, k in ipairs{'value', 'ui-value'} do
	       if choice[i] then
		  assert(not choice[k])
		  choice[k] = choice[i]
		  choice[i] = nil
	       end
	    end
	    return util.setdefaults(
	       choice,
	       {enabled=true, ['ui-value']=self:auto_ui_name(choice.value)}
	    )
	 end,
	 self.choice
      )
   end

   if not self.widget then
      self.widget = self.choice and 'combobox' or 'field'
   end
end

function M.Field:_choice(context) return self.choice end

function M.Field:meta(context)
   assert(self.dtype)
   local res = super(self, M.Field):meta(context)

   res.type = self.dtype
   res.editable = self:editable(context)
   res.condition = self.condition
   res.required = self.required
   res.default = self.default
   res.choice = self:_choice(context)
   res.widget = self.widget

   return res
end

function M.Field:topology(context)
   return {
      {path=context.path, addr=context.addr, type=self.dtype}
   }
end

function M.Field:load(context)
   if not context.txn then return setmetatable({}, context) end
   local value
   if self.compute then value = self:compute(context) end
   if value == nil then value = self:_load(context) end
   if value == nil then return self.default end
   return value
end

function M.Field:_load(context) return context.txn:get(context.addr) end

function M.Field:_validate(context, value)
   if value == nil then
      self:check_required(context)
      return
   end

   local save
   value, save = self:normalize(context, value)

   local committing = context.txn:committing()
   local choice = self:_choice(context)
   if choice and not util.contains(
      map(
	 function(ch) return ch.value end,
	 util.filter(function(ch) return committing or ch.enabled end, choice)
      ),
      value
   ) then
      raise(context.path, 'Invalid value')
   end

   self:validate(context, value)

   if save == nil then save = value end
   return save
end

function M.Field:check_required(context)
   if self.required then raise(context.path, 'Required value not set') end
end

function M.Field:normalize(context, value) return value end

function M.Field:validate(context, value) end

function M.Field:save(context, value)
   if not self:editable(context) then
      raise(context.path, 'Is not editable')
   end
   self:_save(context, self:_validate(context, value))
end

function M.Field:_save(context, value) context.txn:set(context.addr, value) end

function M.Field:validate_saved(context)
   self:save(context, self:load(context))
end


local Primitive = class(M.Field)

function Primitive:validate(context, value)
   local t = self.dtype
   if type(value) ~= t then raise(context.path, 'Not a '..t) end
end


M.String = class(Primitive)

function M.String:init(params)
   super(self, M.String):init(params)
   self.dtype = 'string'
end

function M.String:validate(context, value)
   super(self, M.String):validate(context, value)
   if self['max-length'] and value:len() > self['max-length'] then
      raise(context.path, 'Maximum length exceeded')
   end
   if self.pattern and not value:match('^'..self.pattern..'$') then
      raise(context.path, 'Invalid value')
   end
end

function M.String:meta(context)
   local res = super(self, M.String):meta(context)
   res['max-length'] = self['max-length']
   return res
end


M.Number = class(Primitive)

function M.Number:init(params)
   super(self, M.Number):init(params)
   self.dtype = 'number'
end

function M.Number:normalize(context, value)
   return value and tonumber(value) or value
end


M.Integer = class(M.Number)

function M.Integer:validate(context, value)
   super(self, M.Integer):validate(context, value)
   if math.floor(value) ~= value then raise(context.path, 'Not an integer') end
end


M.Boolean = class(Primitive)

function M.Boolean:init(params)
   super(self, M.Boolean):init(params)
   self.dtype = 'boolean'
   self.widget = self.dtype
end


M.TreeNode = class(M.Field)

function M.TreeNode:init(params)
   if not params.widget then params.widget = 'link' end
   super(self, M.TreeNode):init(params)
end

function M.TreeNode:topology(context)
   local res = super(self, M.TreeNode):topology(context)
   res[1].type = 'table'
   util.extend(res, node.topology(self:load(context, {create=true})))
   return res
end

function M.TreeNode:load(context, options)
   if context.txn and not (
      (
	 options and options.create
      ) or self.create or self:_load(context)
   ) then return end
   return self.itype(context, self.iparams)
end

function M.TreeNode:save(context, value)
   local path = context.path

   if value == path then return end
   if type(value) == 'string' then value = context.txn:fetch(value) end
   if object.isinstance(value, node.TreeNode) and node.path(value) == path then
      return
   end

   self:_save(context)

   if value then
      if type(value) ~= 'table' then
	 raise(path, 'Cannot assign primitive value')
      end

      self:_save(context, {})
      local new = self:load(context, {create=true})

      local errors = err.ErrorDict()
      for k, v in node.pairs(value) do
	 errors:collect(self.save_member, new, k, v)
      end
      errors:raise()
   end
end

function M.TreeNode.save_member(node, k, v) node[k] = v end

function M.TreeNode:validate_saved(context)
   if self:load(context) == nil then self:check_required(context) end
end


M.Model = class(M.TreeNode)

function M.Model:init(params)
   super(self, M.Model):init(params)

   assert(self.model)
   self.itype = self.model
   self.dtype = 'model'
end


return M