This library adds
guard method combinations to underscore.js projects, in much the same style as the Common Lisp Object System or Ruby on Rails controllers. With method combinations, you can easily separate concerns.
class Wumpus roar: -> # ... run: -> #... class Hunter draw: (bow) -> # ... quiver: -> # ... run: -> #... hydrate = (object) -> # code that hydrates the object from storage YouAreDaChef(Wumpus, Hunter) .before 'roar', 'draw', 'run', -> hydrate(this) .after 'roar', 'draw', -> @trigger 'action'
C'mon, meta-programmed code is read-only. It looks good, but when it comes time to debug or modify anything, it's a nightmare to step through it in the debugger and figure out what's going on.
That's often the case, but starting with version 1.0, YouAreDaChef is designed to make code that's easy to write, not just easy to read. Instead of blindly patching methods with wrapper functions, YouAreDaChef stores all of the "advice" functions in a special data structure in the class. You can inspect each class separately. You can provide names for the advice you add to methods, which makes it easier to keep track of the advice you have provided. Since you have the advice and can inspect it, you can write unit tests for your advice and debug the advice you have provided more easily. The
.inspect function does add some code complexity to the YouAreDaChef library, but it makes writing and debugging code written with the YouAreDaChef library much easier.
npm install YouAreDaChef
No, but it can make you appear smarter. Just explain that guard advice is a monad:
YouAreDaChef(EnterpriseyLegume) .when /write(.*)/, -> @user.hasPermission('write', match)
Guard advice works like a before combination, with the bonus that if it returns something falsely, the pointcut will not be executed. This behaviour is similar to the way ActiveRecord callbacks work.
Have a look at method-combinators.
YouAreDaChef's method advice is loosely based on Lisp Flavors, specifically the inheritance of
after advice plus the overriding of
default advice (called
daemon in Flavors). Dan Weinreb (d. 2012) played an important role in the development of Lisp. He is missed by many.
I'm writing a book called CoffeeScript Ristretto. Check it out!