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
|
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
local M = {}
local loadmods = require('acf2.loader')
local topology = require('acf2.model.root').topology
local object = require('acf2.object')
local pth = require('acf2.path')
local util = require('acf2.util')
local contains = util.contains
local stringy = require('stringy')
local DataStore = object.class(
require('acf2.transaction.backend').TransactionBackend
)
function DataStore:init()
object.super(self, DataStore):init()
self.backends = util.map(
function(m) return m() end,
loadmods('persistence/backends')
)
end
function DataStore:split_path(path)
local comps = pth.split(path)
local backend = self.backends[comps[1]]
assert(backend)
table.remove(comps, 1)
return backend, comps
end
function DataStore:get(path)
local backend, comps = self:split_path(path)
local top = topology(path)
local res = backend:get(comps, top)
if top then
local t = top.type
if t and res ~= nil then
local atype = type(res)
if t == 'table' then assert(atype == 'table')
else
assert(atype ~= 'table')
if t == 'string' then res = tostring(res)
elseif t == 'number' then res = tonumber(res)
elseif t == 'boolean' then
if atype == 'string' then res = res:lower() end
if res == 1 or contains({'1', 't', 'true', 'y', 'yes'}, res) then
res = true
elseif res == 0 or contains(
{'0', 'f', 'false', 'n', 'no'}, res
) then
res = false
else res = res and true or false end
elseif t == 'reference' then assert(atype == 'string')
else assert(false) end
end
end
end
return util.copy(res), self.mod_time[path] or 0
end
function DataStore:_set_multiple(mods)
local bms = {}
for _, mod in ipairs(mods) do
local path, value = unpack(mod)
local backend, comps = self:split_path(path)
table.insert(util.setdefault(bms, backend, {}), {comps, value})
end
for backend, bm in pairs(bms) do backend:set(bm) end
end
return DataStore()
|