forwarder
Version:
Forwarding of messages to different objects (mostly properties of the receiver)
58 lines (50 loc) • 1.34 kB
JavaScript
// Generated by CoffeeScript 1.4.0
(function() {
var _forward,
__slice = [].slice;
_forward = exports.forward = function(obj, message, _arg) {
var as, target, to, wth;
to = _arg.to, as = _arg.as, wth = _arg["with"];
if (!to) {
throw new TypeError("missing keyword arg to:");
}
if (as == null) {
as = message;
}
if (wth == null) {
wth = [];
}
target = obj[to];
if (typeof target === typeof Function) {
target = target.call(obj);
}
if (as === '[]') {
return obj[message] = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
args = wth.concat(args);
return target[args];
};
} else {
return obj[message] = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
args = wth.concat(args);
return target[as].apply(target, args);
};
}
};
exports.Forwarder = (function() {
function Forwarder() {}
Forwarder.prototype.forward = function(message, _arg) {
var as, to, wth;
to = _arg.to, as = _arg.as, wth = _arg["with"];
return _forward(this, message, {
to: to,
as: as,
"with": wth
});
};
return Forwarder;
})();
}).call(this);