diff options
Diffstat (limited to 'acf/model/node.lua')
-rw-r--r-- | acf/model/node.lua | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/acf/model/node.lua b/acf/model/node.lua index 7a818be..cf3111d 100644 --- a/acf/model/node.lua +++ b/acf/model/node.lua @@ -52,6 +52,11 @@ TreeNode = class() function TreeNode:init(context) local mt = getmetatable(self) update(mt, context) + + function mt.set(k, v) rawset(self, k, v) end + function mt.__index(t, k) return mt.get(k) end + function mt.__newindex(t, k, v) mt.set(k, v) end + mt.txn.validable[mt.path] = mt.addr end @@ -88,28 +93,8 @@ function Collection:init(context, params) end end - function mt.__index(t, k) return mt.field:load(k) end - function mt.__newindex(t, k, v) mt.field:save(k, v) end -end - - -PrimitiveList = class(Collection) - -function PrimitiveList:init(context, params) - super(self, PrimitiveList):init(context, params) - - local mt = getmetatable(self) - local index = mt.__index - - function mt.__index(t, k) - if type(k) == 'number' then return index(t, k) end - - 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(mt.path, 'Value does not exist: '..k) - end + function mt.get(k) return mt.field:load(k) end + function mt.set(k, v) mt.field:save(k, v) end end @@ -138,3 +123,14 @@ mmeta = meta_func('mmeta') parent = meta_func('parent') path = meta_func('path') validate = meta_func('validate') + + +local rawpairs = pairs + +function pairs(tbl) + if not object.isinstance(tbl, TreeNode) then return rawpairs(tbl) end + local mt = getmetatable(tbl) + local res = {} + for _, member in ipairs(mt.members()) do res[member] = mt.get(member) end + return rawpairs(res) +end |