Comparison

core/moduleapi.lua @ 4538:d0a89c1c43fd

moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jan 2012 19:35:50 +0000
parent 4534:7a0a31c4f6c5
child 4539:3cbfa768eb06
comparison
equal deleted inserted replaced
4537:d8d257c13562 4538:d0a89c1c43fd
98 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message 98 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
99 setfenv(f, self.environment); 99 setfenv(f, self.environment);
100 return f(); 100 return f();
101 end 101 end
102 102
103 function api:depends(name)
104 if not self.dependencies then
105 self.dependencies = {};
106 self:hook("module-reloaded", function (event)
107 if self.dependencies[event.module] then
108 self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module);
109 modulemanager.reload(self.host, self.name);
110 return;
111 end
112 end);
113 self:hook("module-unloaded", function (event)
114 if self.dependencies[event.module] then
115 self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module);
116 modulemanager.unload(self.host, self.name);
117 end
118 end);
119 end
120 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
121 if not mod then
122 local err;
123 mod, err = modulemanager.load(self.host, name);
124 if not mod then
125 return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) ));
126 end
127 end
128 self.dependencies[name] = true;
129 return mod;
130 end
131
103 function api:get_option(name, default_value) 132 function api:get_option(name, default_value)
104 local value = config.get(self.host, self.name, name); 133 local value = config.get(self.host, self.name, name);
105 if value == nil then 134 if value == nil then
106 value = config.get(self.host, "core", name); 135 value = config.get(self.host, "core", name);
107 if value == nil then 136 if value == nil then