Created
November 2, 2012 15:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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