Coder Social home page Coder Social logo

aasm-js's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.