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

local M = {}

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

local fld = require('acf2.model.field')
local Field = fld.Field
local Member = fld.Member

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

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

local pth = require('acf2.path')

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


local function to_member(obj, params)
   if not params then params = {} end
   if object.issubclass(obj, M.Model) then
      params.model = obj
      return fld.Model(params)
   end
   local res = getmetatable(obj).class and obj or obj(params)
   assert(isinstance(res, Member))
   return res
end

function M.to_field(obj, params)
   local res = to_member(obj, params)
   assert(isinstance(res, Field))
   return res
end


M.Action = class(Member)

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

   if not self.func then error('Function not defined for action') end

   if self.field then
      assert(type(self.field) == 'table')
      self.field = M.to_field(self.field)
      self.field.addr = node.null_addr
   end
   
   getmetatable(self).__newindex = function(t, k, v)
				      assert(k == 'name')
				      rawset(t, k, v)
				      if t.field then t.field.name = v end
				   end
end

function M.Action:meta(context)
   local res = super(self, M.Action):meta(context)
   if self.field then res.arg = self.field:meta(context) end
   return res
end


function M.new(base)
   if not base then base = M.Model end

   local res = class(base)
   res.members = base == node.TreeNode and {} or copy(base.members)

   local mt = copy(getmetatable(res))

   function mt.__index(t, k) return base[k] end

   function mt.__newindex(t, k, v)
      assert(v)

      local override = t[k]
      if type(v) == 'table' then v = to_member(v) end

      rawset(t, k, v)

      if isinstance(v, Member) then
	 v.name = k
	 if not override then table.insert(t.members, k) end
      end
   end

   setmetatable(res, mt)

   if isinstance(base, M.Model) then util.setdefaults(res, base) end

   return res
end

M.Model = M.new(node.TreeNode)

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

   local mt = getmetatable(self)

   function mt.member(name, loose, tpe)
      local m = mt.class[name]

      if not tpe then tpe = Member end
      if not isinstance(m, tpe) then m = nil end

      if m == nil then
	 if loose then return end
	 raise(mt.path, 'Does not exist: '..name)
      end

      return BoundMember(self, name, m)
   end

   local function _members(tpe)
      local res = {}
      for _, name in ipairs(self.members) do
	 local m = mt.member(name, true, tpe)
	 if m then table.insert(res, m) end
      end
      return res
   end

   function mt.topology()
      local res = {}
      for _, f in ipairs(_members(Field)) do util.extend(res, f:topology()) end
      return res
   end

   function mt.load(k, options)
      local v = mt.class[k]
      local create = options and options.create

      if isinstance(v, Field) then
	 v = BoundMember(self, k, v)
	 local res = v:load{create=create}
	 if res == nil and v.compute then return v:compute() end
	 return res
      end

      assert(mt.txn)

      if isinstance(v, M.Action) then
	 local f = v.field and BoundMember(self, k, v.field)
	 if create then return f and f:load{create=true} end

	 return function(var)
		   if f then f:save(var)
		   elseif var ~= nil then
		      raise(
			 pth.join(mt.path, v.name),
			 'Action does not accept an input argument'
		      )
		   end
		   local res = v.func(self, f and f:load())
		   if f then f:_save() end
		   return res
		end
      end

      return v
   end

   if not mt.txn then return end


   function mt.mmeta(name) return mt.member(name):meta() end

   function mt.save(k, v) return mt.member(k, false, Field):save(v) end

   local function tmeta(tpe)
      return map(function(m) return m:meta() end, _members(tpe))
   end

   mt._meta.type = 'model'
   function mt.meta()
      if not mt._meta.fields then
	 mt._meta.fields = tmeta(Field)
	 mt._meta.actions = tmeta(M.Action)
      end
      return mt._meta
   end

   function mt.members()
      return map(function(f) return f.name end, mt.meta().fields)
   end

   function mt.match(filter)
      for k, v in pairs(filter) do
	 if not util.contains(v, mt.load(k)) then return false end
      end
      return true
   end

   function mt.validate()
      for _, f in ipairs(_members(Field)) do
	 if f.editable and not f.compute then
	    if mt.match(f.condition or {}) then f:validate_saved()
	    else f:_save() end
	 end
      end
      if self.validate then self:validate() end
   end

   if self.has_permission then
      function mt.has_permission(user, permission)
	 return self:has_permission(user, permission)
      end
   end
end


return M