summaryrefslogtreecommitdiffstats
path: root/aconf/persistence/backends/volatile.lua
blob: 7016a9bd078bc270541a698d31aa5bd187fd14bc (plain)
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
--[[
Copyright (c) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details
--]]

local util = require('aconf.util')


local backend = require('aconf.object').class()

function backend:init(data) self.data = data or {} end

function backend:_get(path)
   local res = self.data
   for _, comp in ipairs(path) do
      if res == nil then return end
      assert(type(res) == 'table')
      res = res[comp]
   end
   return res
end

function backend:get(path, top)
   local res = self:_get(path)
   return type(res) == 'table' and util.keys(res) or res
end

function backend:_set(path, value)
   if type(value) == 'table' then value = {} end

   if #path == 0 then self.data = value

   else
      local comps = util.copy(path)
      local name = comps[#comps]
      table.remove(comps)
      self:_get(comps)[name] = value
   end
end

function backend:set(mods)
   for _, mod in ipairs(mods) do self:_set(unpack(mod)) end
end


return backend