summaryrefslogtreecommitdiffstats
path: root/acf2/persistence/backends/volatile.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-08 18:50:56 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-08 18:50:56 +0300
commit33728ad3382d74281412d4556561d479bb88832b (patch)
treee8a9b2798dec96d820715f729989e91f9e7d5e12 /acf2/persistence/backends/volatile.lua
parent3e48dd63e8bdf0c2641cfb73e6b20bea8c466ff8 (diff)
downloadaconf-0.1.0.tar.bz2
aconf-0.1.0.tar.xz
changed module paths from acf to acf2v0.1.0
Diffstat (limited to 'acf2/persistence/backends/volatile.lua')
-rw-r--r--acf2/persistence/backends/volatile.lua46
1 files changed, 46 insertions, 0 deletions
diff --git a/acf2/persistence/backends/volatile.lua b/acf2/persistence/backends/volatile.lua
new file mode 100644
index 0000000..e33068d
--- /dev/null
+++ b/acf2/persistence/backends/volatile.lua
@@ -0,0 +1,46 @@
+--[[
+Copyright (c) 2012-2013 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+local util = require('acf2.util')
+
+
+local backend = require('acf2.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