summaryrefslogtreecommitdiffstats
path: root/acf/persistence/backends/volatile.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf/persistence/backends/volatile.lua')
-rw-r--r--acf/persistence/backends/volatile.lua52
1 files changed, 52 insertions, 0 deletions
diff --git a/acf/persistence/backends/volatile.lua b/acf/persistence/backends/volatile.lua
new file mode 100644
index 0000000..63d7265
--- /dev/null
+++ b/acf/persistence/backends/volatile.lua
@@ -0,0 +1,52 @@
+--[[
+Copyright (c) 2012-2013 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+module(..., package.seeall)
+
+local copy = require('acf.util').copy
+
+
+local function keys(tbl)
+ local res = {}
+ for k, v in pairs(tbl) do table.insert(res, k) end
+ return res
+end
+
+
+backend = require('acf.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[tonumber(comp) or comp]
+ end
+ return res
+end
+
+function backend:get(path)
+ local res = self:_get(path)
+ return type(res) == 'table' and keys(res) or res
+end
+
+function backend:_set(path, tpe, value)
+ if tpe == 'table' then value = {} end
+
+ if #path == 0 then self.data = value
+
+ else
+ local comps = copy(path)
+ local name = comps[#comps]
+ table.remove(comps)
+ self:_get(comps)[tonumber(name) or name] = value
+ end
+end
+
+function backend:set(mods)
+ for _, mod in ipairs(mods) do self:_set(unpack(mod)) end
+end