A few functions that simplify definition of everyday methods with common logic
Class.hasParent = function(klass) {
var caller = klass.$caller;
return !!(caller.$owner.parent && caller.$owner.parent.prototype[caller.$name]);
};
Macro = {};
Make stackable function what executes it’s parent before itself
Macro.onion = function(callback) {
return function() {
if (!this.parent.apply(this, arguments)) return;
return callback.apply(this, arguments) !== false;
};
};
Make getter-function with cache. Returned function alculates values on first call, after return this[name]. To reset cache use:
delete this[name];
Macro.getter = function(name, callback) {
return function() {
if (!this[name]) this[name] = callback.apply(this, arguments);
return this[name];
};
};
Make function that runs it’s parent if it exists, and runs itself if does not
Macro.defaults = function(callback) {
return function() {
if (Class.hasParent(this)) {
return this.parent.apply(this, arguments);
} else {
return callback.apply(this, arguments);
}
};
};
Make function what returns property ‘name’ of passed argument
Macro.map = function(name) {
return function(item) {
return item[name];
};
};
Make function Macro.map but diference that Macro.proc calls ‘name’ method
Macro.proc = function(name, args) {
return function(item) {
return item[name].apply(item, args || arguments);
};
};
Make function what call method ‘method’ of property this[name] with passed arguments
Macro.delegate = function(name, method) {
return function() {
if (this[name]) return this[name][method].apply(this[name], arguments);
};
};