node package manager


Coffeescript/Javascript method combinations

You Are 'Da Chef

This library adds before, after, around, and 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.

For example:

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', ->
    .after 'roar', 'draw', ->
        @trigger 'action'
    .after 'run', ->
        @trigger 'move'

You can even use regular expressions to specify pointcuts:

class EnterpriseyLegume
  setId:         (@id)         ->
  setName:       (@name)       ->
  setDepartment: (@department) ->
  setCostCentre: (@costCentre) ->


  .around /set(.*)/, (pointcut, match, value) ->
    performTransaction () ->
      writeToLog "#{match[1]}: #{value}"




npm install YouAreDaChef

No, but it can make you appear smarter. Just explain that guard advice is a monad:


  .guard /write(.*)/, ->
    @user.hasPermission('write', match[1])

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.

You can also try making a cryptic reference to a computed, non-local COMEFROM.

Separating Concerns in Coffeescript using Aspect-Oriented Programming