diff options
author | Matthew Wild <mwild1@gmail.com> | 2012-01-22 19:48:53 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2012-01-22 19:48:53 +0000 |
commit | 7c5700ac5402bf47a6c55d55a7f1fa1a8b8cd0d0 (patch) | |
tree | 2d5095d7097f4bad8e049bd9805da8ac510a4bb9 /core | |
parent | 2f397255c26a57c36102ece3883c3765870c0bf9 (diff) | |
download | prosody-7c5700ac5402bf47a6c55d55a7f1fa1a8b8cd0d0.tar.gz prosody-7c5700ac5402bf47a6c55d55a7f1fa1a8b8cd0d0.zip |
moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Diffstat (limited to 'core')
-rw-r--r-- | core/moduleapi.lua | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 44ae9a07..7a4d1fa6 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -129,6 +129,29 @@ function api:depends(name) return mod; end +-- Returns one or more shared tables at the specified virtual paths +-- Intentionally does not allow the table at a path to be _set_, it +-- is auto-created if it does not exist. +function api:shared(...) + local paths = { n = select("#", ...), ... }; + local data_array = {}; + local default_path_components = { self.host, self.name }; + for i = 1, paths.n do + local path = paths[i]; + if path:sub(1,1) ~= "/" then -- Prepend default components + local n_components = select(2, path:gsub("/", "%1")); + path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path; + end + local shared = shared_data[path]; + if not shared then + shared = {}; + shared_data[path] = shared; + end + t_insert(data_array, shared); + end + return unpack(data_array); +end + function api:get_option(name, default_value) local value = config.get(self.host, self.name, name); if value == nil then |