summaryrefslogtreecommitdiffstats
path: root/acf/model/node.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf/model/node.lua')
-rw-r--r--acf/model/node.lua51
1 files changed, 28 insertions, 23 deletions
diff --git a/acf/model/node.lua b/acf/model/node.lua
index b0c6f8a..7a818be 100644
--- a/acf/model/node.lua
+++ b/acf/model/node.lua
@@ -11,6 +11,7 @@ local class = object.class
local super = object.super
local pth = require('acf.path')
+local update = require('acf.util').update
BoundField = class()
@@ -29,11 +30,16 @@ function BoundField:init(parent, field)
name = arg[1]
table.remove(arg, 1)
end
- return member(field,
- pmt.txn,
- pth.join(pmt.path, name),
- pmt.addr and pth.join(pmt.addr, name),
- unpack(arg))
+ return member(
+ field,
+ {
+ txn=pmt.txn,
+ parent=parent,
+ path=pth.join(pmt.path, name),
+ addr=pmt.addr and pth.join(pmt.addr, name)
+ },
+ unpack(arg)
+ )
end
end
@@ -43,13 +49,10 @@ end
TreeNode = class()
-function TreeNode:init(txn, path, addr)
+function TreeNode:init(context)
local mt = getmetatable(self)
- mt.txn = txn
- mt.path = path
- mt.addr = addr
-
- txn.validable[path] = addr
+ update(mt, context)
+ mt.txn.validable[mt.path] = mt.addr
end
function TreeNode:search(path)
@@ -66,22 +69,22 @@ end
Collection = class(TreeNode)
-function Collection:init(txn, path, addr, field, required)
- super(self, Collection):init(txn, path, addr)
+function Collection:init(context, params)
+ super(self, Collection):init(context)
self.init = nil
self.search = nil
local mt = getmetatable(self)
- mt.field = BoundField(self, field)
+ mt.field = BoundField(self, params.field)
mt.meta = {type='collection', members=mt.field:meta('$')}
function mt.mmeta(name) return mt.meta.members end
- function mt.members() return txn:get(addr) or {} end
+ function mt.members() return mt.txn:get(mt.addr) or {} end
function mt.validate()
- if required and #txn:get(addr) == 0 then
- raise(path, 'Collection cannot be empty')
+ if params.required and #mt.txn:get(mt.addr) == 0 then
+ raise(mt.path, 'Collection cannot be empty')
end
end
@@ -92,8 +95,8 @@ end
PrimitiveList = class(Collection)
-function PrimitiveList:init(txn, path, addr, field, required)
- super(self, PrimitiveList):init(txn, path, addr, field, required)
+function PrimitiveList:init(context, params)
+ super(self, PrimitiveList):init(context, params)
local mt = getmetatable(self)
local index = mt.__index
@@ -101,11 +104,11 @@ function PrimitiveList:init(txn, path, addr, field, required)
function mt.__index(t, k)
if type(k) == 'number' then return index(t, k) end
- for i, j in ipairs(txn:get(addr) or {}) do
+ for i, j in ipairs(mt.txn:get(mt.addr) or {}) do
assert(i == tonumber(j))
if mt.field:load(i) == k then return k end
end
- raise(path, 'Value does not exist: '..k)
+ raise(mt.path, 'Value does not exist: '..k)
end
end
@@ -113,8 +116,8 @@ end
-- experimental
Mixed = class(Collection)
-function Mixed:init(txn, path, addr, field, required)
- super(self, Mixed):init(txn, path, addr, field, required)
+function Mixed:init(context, params)
+ super(self, Mixed):init(context, params)
-- TODO dynamic meta: list non-leaf children
getmetatable(self).meta = {type='mixed'}
end
@@ -128,8 +131,10 @@ local function meta_func(attr)
end
end
+addr = meta_func('addr')
members = meta_func('members')
meta = meta_func('meta')
mmeta = meta_func('mmeta')
+parent = meta_func('parent')
path = meta_func('path')
validate = meta_func('validate')