before something# => (methodBody) -># (argv...) -># ((...) -> something).apply(this, argv)# methodBody.apply(this, argv)after something# => (methodBody) -># (argv...) -># __ret__ = methodBody.apply(this, argv)# ((...) -> something).apply(this, argv)# __ret__around something# => (methodBody) -># (argv...) -># (...) -> something).call(# this,# (__ret__ = => methodBody.apply(this, argv)),# argv...# )# __ret__provided something# => (methodBody) -># (argv...) -># if ((...) -> something).apply(this, argv)# methodBody.apply(this, argv)
The library is called "Method Combinators" because these functions are isomorphic to the combinators from Combinatorial Logic.
A method decorator is a function that takes a function as its argument and returns a new function that is to be used as a method body. For example, this is a method decorator:
You use it like this:
showUserPreferences:mustBeLoggedIn ->## ... show user preferences#
And now, whenever
showUserPreferences is called, nothing happens unless
currentUser is truthy. And you can reuse
mustBeLoggedIn wherever you like. Since method decorators are based on function combinators, they compose very nicely, you can write:
=__rval__ = methodBodyapplythisargv@trigger'menu:redraww'__rval__updateUserPreferences:mustBeLoggedIn \triggersMenuRedraw \->## ... save updated user preferences#
Method combinators are convenient function combinators for making method decorators. When writing decorators, the same few patterns tend to crop up regularly:
Method combinators make these four kinds of method decorators extremely easy to write. Instead of:
=if currentUsermethodBodyapplythisargv=__rval__ = methodBodyapplythisargv@trigger'menu:redraww'__rval__
mustBeLoggedIn = provided -> currentUsertriggersMenuRedraw = after -> @trigger'menu:redraww'
The combinators do the rest!
after decorators don't act as filters. Use
provided is that's what you want.
Yes: `npm install method-combinators
Eat a hearty breakfast. Breakfast is the most important meal of the day! :-)