Software / code / prosody
Comparison
core/objectmanager.lua @ 889:bb959588bbc4
Added core.objectmanager
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sun, 08 Mar 2009 01:07:29 +0500 |
| child | 1522:569d58d21612 |
comparison
equal
deleted
inserted
replaced
| 888:1059230969c3 | 889:bb959588bbc4 |
|---|---|
| 1 | |
| 2 local new_multitable = require "util.multitable".new; | |
| 3 local t_insert = table.insert; | |
| 4 local t_concat = table.concat; | |
| 5 local tostring = tostring; | |
| 6 local unpack = unpack; | |
| 7 local pairs = pairs; | |
| 8 local error = error; | |
| 9 local type = type; | |
| 10 local _G = _G; | |
| 11 | |
| 12 local data = new_multitable(); | |
| 13 | |
| 14 module "objectmanager" | |
| 15 | |
| 16 function set(...) | |
| 17 return data:set(...); | |
| 18 end | |
| 19 function remove(...) | |
| 20 return data:remove(...); | |
| 21 end | |
| 22 function get(...) | |
| 23 return data:get(...); | |
| 24 end | |
| 25 | |
| 26 local function get_path(path) | |
| 27 if type(path) == "table" then return path; end | |
| 28 local s = {}; | |
| 29 for part in tostring(path):gmatch("[%w_]+") do | |
| 30 t_insert(s, part); | |
| 31 end | |
| 32 return s; | |
| 33 end | |
| 34 | |
| 35 function get_object(path) | |
| 36 path = get_path(path) | |
| 37 return data:get(unpack(path)), path; | |
| 38 end | |
| 39 function set_object(path, object) | |
| 40 path = get_path(path); | |
| 41 data:set(unpack(path), object); | |
| 42 end | |
| 43 | |
| 44 data:set("ls", function(_dir) | |
| 45 local obj, dir = get_object(_dir); | |
| 46 if not obj then error("object not found: " .. t_concat(dir, '/')); end | |
| 47 local r = {}; | |
| 48 if type(obj) == "table" then | |
| 49 for key, val in pairs(obj) do | |
| 50 r[key] = type(val); | |
| 51 end | |
| 52 end | |
| 53 return r; | |
| 54 end); | |
| 55 data:set("get", get_object); | |
| 56 data:set("set", set_object); | |
| 57 data:set("echo", function(...) return {...}; end); | |
| 58 data:set("_G", _G); | |
| 59 | |
| 60 return _M; |