use hook name as function name, if not specified

Example `"[hookName]": "ep_plugin/static/js/index:[hookName]"` becomes  `"[hookName]": "ep_plugin/static/js/index"`
This commit is contained in:
Matthias Bartelmeß 2012-04-04 15:03:28 +02:00
parent 49593a1c8e
commit 5115f74f66

View file

@ -51,10 +51,12 @@ exports.formatHooks = function () {
return res.join("\n"); return res.join("\n");
} }
exports.loadFn = function (path) { exports.loadFn = function (path, hookName) {
var x = path.split(":"); var x = path.split(":");
var fn = require(x[0]); var fn = require(x[0]);
_.each(x[1].split("."), function (name) { var functionName = x[1] ? x[1] : hookName;
_.each(functionName.split("."), function (name) {
fn = fn[name]; fn = fn[name];
}); });
return fn; return fn;
@ -63,10 +65,14 @@ exports.loadFn = function (path) {
exports.extractHooks = function (parts, hook_set_name) { exports.extractHooks = function (parts, hook_set_name) {
var hooks = {}; var hooks = {};
_.each(parts,function (part) { _.each(parts,function (part) {
_.chain(part[hook_set_name] || {}).keys().each(function (hook_name) { _.chain(part[hook_set_name] || {})
.keys()
.each(function (hook_name) {
if (hooks[hook_name] === undefined) hooks[hook_name] = []; if (hooks[hook_name] === undefined) hooks[hook_name] = [];
var hook_fn_name = part[hook_set_name][hook_name]; var hook_fn_name = part[hook_set_name][hook_name];
var hook_fn = exports.loadFn(part[hook_set_name][hook_name]); var hook_fn = exports.loadFn(hook_fn_name, hook_name);
if (hook_fn) { if (hook_fn) {
hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part}); hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
} else { } else {