summaryrefslogtreecommitdiffstats
path: root/acf/model/field.lua
blob: 4d539e8cbc7c4744bd01d32f27c7cd76b6f4da40 (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
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]

local M = {}

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 util = require('acf.util')


local function contains(list, value)
   for k, v in ipairs(list) do if v == value then return true end end
   return false
end

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
   return (name:sub(1, 1):upper()..name:sub(2)):gsub('-', ' ')
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


M.Field = class(M.Member)

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

   if self.choice and not self['ui-choice'] then
      self['ui-choice'] = util.map(
	 function(name) return self:auto_ui_name(name) end,
	 self.choice
      )
   end

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

function M.Field:meta(context)
   assert(self.dtype)
   local res = super(self, M.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 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 = context.txn:get(context.addr)
   if value == nil then return self.default end
   return value
end

function M.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 M.Field:validate(context, value) end

function M.Field:save(context, value)
   context.txn:set(context.addr, self:_validate(context, 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:_validate(context, value)
   return super(self, M.Number):_validate(
      context,
      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 context.txn:get(context.addr)
   ) 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

   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, {})
      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


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