Merge with waqas
[prosody.git] / core / objectmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 \r
10 local new_multitable = require "util.multitable".new;\r
11 local t_insert = table.insert;\r
12 local t_concat = table.concat;\r
13 local tostring = tostring;\r
14 local unpack = unpack;\r
15 local pairs = pairs;\r
16 local error = error;\r
17 local type = type;\r
18 local _G = _G;\r
19 \r
20 local data = new_multitable();\r
21 \r
22 module "objectmanager"\r
23 \r
24 function set(...)\r
25         return data:set(...);\r
26 end\r
27 function remove(...)\r
28         return data:remove(...);\r
29 end\r
30 function get(...)\r
31         return data:get(...);\r
32 end\r
33 \r
34 local function get_path(path)\r
35         if type(path) == "table" then return path; end\r
36         local s = {};\r
37         for part in tostring(path):gmatch("[%w_]+") do\r
38                 t_insert(s, part);\r
39         end\r
40         return s;\r
41 end\r
42 \r
43 function get_object(path)\r
44         path = get_path(path)\r
45         return data:get(unpack(path)), path;\r
46 end\r
47 function set_object(path, object)\r
48         path = get_path(path);\r
49         data:set(unpack(path), object);\r
50 end\r
51 \r
52 data:set("ls", function(_dir)\r
53         local obj, dir = get_object(_dir);\r
54         if not obj then error("object not found: " .. t_concat(dir, '/')); end\r
55         local r = {};\r
56         if type(obj) == "table" then\r
57                 for key, val in pairs(obj) do\r
58                         r[key] = type(val);\r
59                 end\r
60         end\r
61         return r;\r
62 end);\r
63 data:set("get", get_object);\r
64 data:set("set", set_object);\r
65 data:set("echo", function(...) return {...}; end);\r
66 data:set("_G", _G);\r
67 \r
68 return _M;\r