Comparison

core/moduleapi.lua @ 4539:3cbfa768eb06

moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jan 2012 19:48:53 +0000
parent 4538:d0a89c1c43fd
child 4605:69746db53125
comparison
equal deleted inserted replaced
4538:d0a89c1c43fd 4539:3cbfa768eb06
127 end 127 end
128 self.dependencies[name] = true; 128 self.dependencies[name] = true;
129 return mod; 129 return mod;
130 end 130 end
131 131
132 -- Returns one or more shared tables at the specified virtual paths
133 -- Intentionally does not allow the table at a path to be _set_, it
134 -- is auto-created if it does not exist.
135 function api:shared(...)
136 local paths = { n = select("#", ...), ... };
137 local data_array = {};
138 local default_path_components = { self.host, self.name };
139 for i = 1, paths.n do
140 local path = paths[i];
141 if path:sub(1,1) ~= "/" then -- Prepend default components
142 local n_components = select(2, path:gsub("/", "%1"));
143 path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
144 end
145 local shared = shared_data[path];
146 if not shared then
147 shared = {};
148 shared_data[path] = shared;
149 end
150 t_insert(data_array, shared);
151 end
152 return unpack(data_array);
153 end
154
132 function api:get_option(name, default_value) 155 function api:get_option(name, default_value)
133 local value = config.get(self.host, self.name, name); 156 local value = config.get(self.host, self.name, name);
134 if value == nil then 157 if value == nil then
135 value = config.get(self.host, "core", name); 158 value = config.get(self.host, "core", name);
136 if value == nil then 159 if value == nil then