Friday, January 3, 2014

Coding Requires Abandonment

Sure, I've stayed up all night coding, just 'cause I couldn't stop. But that's not what I'm talking about.

I'm referring to the notion that a node module requires a return value. Abandon that thought - I did recently while breaking my liquid.coffee code into framework modules.  Lets look at what I did.

First, the framework needs a  namespace, or root object.

module.exports =

  VERSION:  require('../package.json').version

require './liquid/drop'
require './liquid/interrupt'
require './liquid/strainer'
require './liquid/context'
...
figure1: ./src/liquid.coffee

Then, we link up the framework classes:

Liquid = require('../liquid')

class Liquid.Interrupt

  message: ''

  constructor: (@message = 'interrupt') ->

class Liquid.BreakInterrupt extends Liquid.Interrupt

class Liquid.ContinueInterrupt extends Liquid.Interrupt

figure2: ./liquid/interrupt.coffee

Note the lack of a module.exports in the child class. Instead, I've required the namespace root, and defined the classes as properties of that.
We can repeat this pattern to fill out the entire framework - you can view the full example on github.


No comments:

Post a Comment