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

module(..., package.seeall)

err = require('acf.error')
local raise = err.raise
local relabel = err.relabel

local combination = require('acf.model.combination')
Union = combination.Union
Range = combination.Range

local fld = require('acf.model.field')
local Field = fld.Field
Boolean = fld.Boolean
Integer = fld.Integer
Number = fld.Number
String = fld.String

local model = require('acf.model.model')
Action = model.Action
new = model.new
local to_field = model.to_field

net = require('acf.model.net')

node = require('acf.model.node')
permission = require('acf.model.permission')
register = require('acf.model.root').register
node.Set = require('acf.model.set').Set

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

local pth = require('acf.path')
local map = require('acf.util').map


require 'stringy'


Reference = class(Field)

function Reference:init(params)
   if not params.widget then params.widget = 'reference' end
   super(self, Reference):init(params)
   self.dtype = 'reference'
   if not self.scope then self.scope = '/' end
end

function Reference:topology(context)
   local res = super(self, Reference):topology(context)
   res[1].scope = self.scope
   return res
end

function Reference:abs_scope(context)
   return pth.to_absolute(self.scope, node.path(context.parent))
end

function Reference:meta(context)
   local res = super(self, Reference):meta(context)
   res.scope = self:abs_scope(context)

   local txn = context.txn
   local objs = txn:get(
      node.addr(relabel('system', txn.fetch, txn, res.scope))
   ) or {}
   res.choice = map(function(p) return pth.join(res.scope, p) end, objs)
   res['ui-choice'] = objs

   return res
end

function Reference:follow(context, value)
   return context.txn:fetch(pth.rawjoin(self:abs_scope(context), value))
end

function Reference:load(context)
   local ref = super(self, Reference):load(context)
   return (context.txn and ref) and self:follow(context, ref) or ref
end

function Reference:_validate(context, value)
   super(self, Reference):_validate(context, value)

   if value == nil then return end

   if object.isinstance(value, node.TreeNode) then value = node.path(value) end

   local path = context.path

   if pth.is_absolute(value) then
      local scope = self:abs_scope(context)
      local prefix = scope..'/'
      if not stringy.startswith(value, prefix) then
	 raise(path, 'Reference out of scope ('..scope..')')
      end
      value = string.sub(value, string.len(prefix) + 1, -1)
   end

   -- assume one-level ref for now
   if #pth.split(value) > 1 then
      raise(path, 'Subtree references not yet supported')
   end

   -- TODO check instance type
   relabel(path, self.follow, self, context, value)

   return value
end


Model = fld.Model


Collection = class(fld.TreeNode)

function Collection:init(params, itype)
   if params.create == nil then params.create = true end
   super(self, Collection):init(params)

   assert(self.type)
   self.itype = itype or node.Collection
   self.iparams = {
      destroy=self.destroy,
      required=self.required,
      ui_member=self.ui_member
   }

   self.dtype = 'collection'
   self.widget = self.dtype
end

function Collection:auto_ui_name(name)
   if not name then return end
   if string.sub(name, -1, -1) ~= 's' then name = name..'s' end
   return super(self, Collection):auto_ui_name(name)
end

function Collection:load(context, create)
   if not self.iparams.field then self.iparams.field = to_field(self.type) end
   return super(self, Collection):load(context, create)
end


List = class(Collection)
function List:init(params) super(self, List):init(params, node.List) end


Set = class(Collection)
function Set:init(params) super(self, Set):init(params, node.Set) end
function Set.save_member(tn, k, v) node.insert(tn, v) end


-- experimental
Mixed = class(Collection)

function Mixed:init(params)
   params.type = Mixed
   super(self, Mixed):init(params, node.Mixed)
   self.pfield = Field()
end

function Mixed:topology(context) return {} end

function Mixed:load(context)
   local value = self.pfield:load(context)
   if type(value) == 'table' then return super(self, Mixed):load(context) end
   return value
end

function Mixed:save(context, value)
   if type(value) == 'table' then super(self, Mixed):save(context, value)
   else self.pfield:save(context, value) end
end