Skip to content

Instantly share code, notes, and snippets.

@mkoryak
Created November 2, 2012 15:38
Show Gist options
  • Save mkoryak/4002081 to your computer and use it in GitHub Desktop.
Save mkoryak/4002081 to your computer and use it in GitHub Desktop.
Instrument all of object's functions with fn.before() and fn.after() functions
//requires underscore.js 1.3+
var wrapFunctions = function(obj){
_.each(obj, function(func, funcName){
if(_.isFunction(func)){
obj[funcName] = _.wrap(func, function(fn){
var wrappedArgs = _.toArray(arguments).slice(1);
wrappedArgs = obj[funcName].before && obj[funcName].before.apply(this, wrappedArgs) || wrappedArgs;
var ret = fn.apply(obj, wrappedArgs);
obj[funcName].after && obj[funcName].after.apply(this, wrappedArgs);
return ret;
});
}
});
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment