summaryrefslogtreecommitdiffstats
path: root/acf/model/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf/model/init.lua')
-rw-r--r--acf/model/init.lua125
1 files changed, 70 insertions, 55 deletions
diff --git a/acf/model/init.lua b/acf/model/init.lua
index e1a4dd9..9d09204 100644
--- a/acf/model/init.lua
+++ b/acf/model/init.lua
@@ -3,34 +3,46 @@ Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
-module(..., package.seeall)
+local M = {}
-err = require('acf.error')
-local raise = err.raise
-local relabel = err.relabel
+M.error = require('acf.error')
+local raise = M.error.raise
+local relabel = M.error.relabel
local combination = require('acf.model.combination')
-Union = combination.Union
-Range = combination.Range
+M.Union = combination.Union
+M.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
+M.Boolean = fld.Boolean
+M.Integer = fld.Integer
+M.Number = fld.Number
+M.String = fld.String
local model = require('acf.model.model')
-Action = model.Action
-new = model.new
+M.Action = model.Action
+M.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
+M.net = require('acf.model.net')
+
+local node = require('acf.model.node')
+M.node = {}
+for _, m in ipairs{
+ 'TreeNode',
+ 'has_permission',
+ 'insert',
+ 'meta',
+ 'mmeta',
+ 'path',
+ 'pairs',
+ 'ipairs'
+} do M.node[m] = node[m] end
+
+M.permission = require('acf.model.permission')
+M.register = require('acf.model.root').register
+M.node.Set = require('acf.model.set').Set
local object = require('acf.object')
local class = object.class
@@ -40,30 +52,30 @@ local pth = require('acf.path')
local map = require('acf.util').map
-require 'stringy'
+local stringy = require('stringy')
-Reference = class(Field)
+M.Reference = class(Field)
-function Reference:init(params)
+function M.Reference:init(params)
if not params.widget then params.widget = 'reference' end
- super(self, Reference):init(params)
+ super(self, M.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)
+function M.Reference:topology(context)
+ local res = super(self, M.Reference):topology(context)
res[1].scope = self.scope
return res
end
-function Reference:abs_scope(context)
+function M.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)
+function M.Reference:meta(context)
+ local res = super(self, M.Reference):meta(context)
res.scope = self:abs_scope(context)
local txn = context.txn
@@ -76,17 +88,17 @@ function Reference:meta(context)
return res
end
-function Reference:follow(context, value)
+function M.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)
+function M.Reference:load(context)
+ local ref = super(self, M.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)
+function M.Reference:_validate(context, value)
+ super(self, M.Reference):_validate(context, value)
if value == nil then return end
@@ -114,7 +126,7 @@ function Reference:_validate(context, value)
return value
end
-function Reference:deleted(context, addr)
+function M.Reference:deleted(context, addr)
local target = self:load(context)
if target and node.addr(target) == addr then
-- TODO raise error for the target object
@@ -123,14 +135,14 @@ function Reference:deleted(context, addr)
end
-Model = fld.Model
+M.Model = fld.Model
-Collection = class(fld.TreeNode)
+M.Collection = class(fld.TreeNode)
-function Collection:init(params, itype)
+function M.Collection:init(params, itype)
if params.create == nil then params.create = true end
- super(self, Collection):init(params)
+ super(self, M.Collection):init(params)
assert(self.type)
self.itype = itype or node.Collection
@@ -144,45 +156,48 @@ function Collection:init(params, itype)
self.widget = self.dtype
end
-function Collection:auto_ui_name(name)
+function M.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)
+ return super(self, M.Collection):auto_ui_name(name)
end
-function Collection:load(context, create)
+function M.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)
+ return super(self, M.Collection):load(context, create)
end
-List = class(Collection)
-function List:init(params) super(self, List):init(params, node.List) end
+M.List = class(M.Collection)
+function M.List:init(params) super(self, M.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
+M.Set = class(M.Collection)
+function M.Set:init(params) super(self, M.Set):init(params, M.node.Set) end
+function M.Set.save_member(tn, k, v) node.insert(tn, v) end
-- experimental
-Mixed = class(Collection)
+M.Mixed = class(M.Collection)
-function Mixed:init(params)
- params.type = Mixed
- super(self, Mixed):init(params, node.Mixed)
+function M.Mixed:init(params)
+ params.type = M.Mixed
+ super(self, M.Mixed):init(params, node.Mixed)
self.pfield = Field()
end
-function Mixed:topology(context) return {} end
+function M.Mixed:topology(context) return {} end
-function Mixed:load(context)
+function M.Mixed:load(context)
local value = self.pfield:load(context)
- if type(value) == 'table' then return super(self, Mixed):load(context) end
+ if type(value) == 'table' then return super(self, M.Mixed):load(context) end
return value
end
-function Mixed:save(context, value)
- if type(value) == 'table' then super(self, Mixed):save(context, value)
+function M.Mixed:save(context, value)
+ if type(value) == 'table' then super(self, M.Mixed):save(context, value)
else self.pfield:save(context, value) end
end
+
+
+return M