aboutsummaryrefslogtreecommitdiffstats
path: root/core/objectmanager.lua
blob: 684ed8072875a364c98131517dd9a136f7db4d38 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
-- 
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--


local new_multitable = require "util.multitable".new;
local t_insert = table.insert;
local t_concat = table.concat;
local tostring = tostring;
local unpack = unpack;
local pairs = pairs;
local error = error;
local type = type;
local _G = _G;

local data = new_multitable();

module "objectmanager"

function set(...)
	return data:set(...);
end
function remove(...)
	return data:remove(...);
end
function get(...)
	return data:get(...);
end

local function get_path(path)
	if type(path) == "table" then return path; end
	local s = {};
	for part in tostring(path):gmatch("[%w_]+") do
		t_insert(s, part);
	end
	return s;
end

function get_object(path)
	path = get_path(path)
	return data:get(unpack(path)), path;
end
function set_object(path, object)
	path = get_path(path);
	data:set(unpack(path), object);
end

data:set("ls", function(_dir)
	local obj, dir = get_object(_dir);
	if not obj then error("object not found: " .. t_concat(dir, '/')); end
	local r = {};
	if type(obj) == "table" then
		for key, val in pairs(obj) do
			r[key] = type(val);
		end
	end
	return r;
end);
data:set("get", get_object);
data:set("set", set_object);
data:set("echo", function(...) return {...}; end);
data:set("_G", _G);

return _M;