cpatni / aasm-js

CoffeeScript state machine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AASM.js

aasm.js is a node.js framework to build state machines. It provides a DSL to mixin finite state machines behavior to CoffeeScript classes.

AASM has the following features:

  • States
  • Machines
  • Events
  • Transitions

The framework is based on Scott Barron aasm work on ruby.

Download


The latest AASM can currently be pulled from the git repository on github.

http://github.com/rubyorchard/aasm-js/tree/master

Installation


From npm repository


npm install aasm-js

Simple Example


Here's a quick example highlighting some of the features.

AASM = require 'aasm'

module.exports = class Conversation
  AASM.include(this)

  @aasmInitialState 'needsAttention'

  @aasmState 'needsAttention'
  @aasmState 'read'
  @aasmState 'closed'
  @aasmState 'awaitingResponse'
  @aasmState 'junk'

  @aasmEvent 'newMessage', ()->

  @aasmEvent 'view', ()->
    @transitions {to: 'read', from: ['needsAttention']}

  @aasmEvent 'reply', ()->

  @aasmEvent 'close', ()->
    @transitions {to: 'closed', from: ['read', 'awaitingResponse']}

  @aasmEvent 'junk', ()->
    @transitions {to: 'junk', from: ['read']}

  @aasmEvent 'unjunk', ()->

A Slightly More Complex Example


This example uses a few of the more complex features available.

class Relationship
  AASM.include(this)

  @aasmInitialState (relationship) ->
    if relationship.isStrictlyForFun() then 'intimate' else 'dating'

  @aasmState 'dating',   {enter: 'makeHappy', exit: 'makeDepressed'}
  @aasmState 'intimate', {enter: 'makeVeryHappy', exit: 'neverSpeakAgain'}
  @aasmState 'married',  {enter: 'makeHappy', exit: 'buyExoticCarAndWearACombover'}

  @aasmEvent 'getIntimate', ->
    @transitions to: 'intimate', from: ['dating'], guard: 'isDrunk'
  @aasmEvent 'getMarried', ->
    @transitions to: 'married', from: ['dating', 'married'], guard: 'isWillingToGiveUpManhood'

  isStrictlyForFun: ->
  isDrunk: ->
  isWillingToGiveUpManhood: ->
  makeHappy: ->
  makeDepressed: ->
  makeVeryHappy: ->
  neverSpeakAgain: ->
  giveUpIntimacy: ->
  buyExoticCarAndWearACombover: ->

Other Stuff


  • Based On:: Scott Barron aasm for ruby. I wrote this to learn coffeescript originally.
  • Author:: Chandra Patni
  • License:: Original code Copyright 2011 by Chandra Patni. Released under an MIT-style license. See the LICENSE file included in the distribution.

Warranty


This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.

About

CoffeeScript state machine

License:MIT License


Languages

Language:CoffeeScript 65.5%Language:JavaScript 34.3%Language:Ruby 0.1%