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
|
--[[
Copyright (c) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details
--]]
local M = {}
local TreeNode = require('aconf.model.field').TreeNode
local node = require('aconf.model.node')
local object = require('aconf.object')
local pth = require('aconf.path')
local setdefaults = require('aconf.util').setdefaults
M.Set = object.class(require('aconf.model.node').List)
function M.Set:init(context, params)
assert(not object.isinstance(params.field, TreeNode))
params.field.dereference = false
params.dtype = 'set'
object.super(self, M.Set):init(context, params)
local function find(value)
value = node.BoundMember(
self, pth.wildcard, params.field
):normalize(value)
for i, member in node.pairs(self) do
if member == value then return i, value end
end
end
local mt = getmetatable(self)
function mt.get(k, options)
options = setdefaults(options or {}, {dereference=true})
local i, v = find(k)
if i then return mt.load(i, options) end
if options.create then return v end
end
function mt.__newindex(t, k, v)
assert(v == nil)
local i = find(k)
if not i then return end
mt.save(i, nil)
end
function mt.contains(v) return find(v) and true or false end
local insert = mt.insert
function mt.insert(v) if not mt.contains(v) then insert(v) end end
end
return M
|