GithubHelp home page GithubHelp logo

therocketforever / dm-is-state_machine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from datamapper/dm-is-state_machine

2.0 2.0 0.0 868 KB

DataMapper plugin for creating state machines

Home Page: http://datamapper.org/

License: MIT License

dm-is-state_machine's Introduction

dm-is-state_machine

DataMapper plugin that adds state machine functionality to your models.

Why is this plugin useful?

Your DataMapper resource might benefit from a state machine if it:

  • has different “modes” of operation

  • has discrete behaviors

  • especially if the behaviors are mutually exclusive

And you want a clean, high-level way of describing these modes / behaviors and how the resource moves between them. This plugin allows you to declaratively describe the states and transitions involved.

Installation

  1. Download dm-more.

  2. Install dm-is-state_machine using the supplied rake files.

Setting up with Merb ##

Add this line to your init.rb:

dependency "dm-is-state_machine"

## Example DataMapper resource (i.e. model) ##

# /app/models/traffic_light.rb
class TrafficLight
  include DataMapper::Resource

  property :id, Serial

  is :state_machine, :initial => :green, :column => :color do
    state :green
    state :yellow
    state :red,    :enter => :red_hook
    state :broken

    event :forward do
      transition :from => :green,  :to => :yellow
      transition :from => :yellow, :to => :red
      transition :from => :red,    :to => :green
    end
  end

  def red_hook
    # Do something
  end
end

What this gives you

Explained in words

The above DSL (domain specific language) does these things “behind the scenes”:

  1. Defines a DataMapper property called ‘color’.

  2. Makes the current state available by using ‘traffic_light.color’.

  3. Defines the ‘forward!’ transition method. This method triggers the appropriate transition based on the current state and comparing it against the various :from states. It will raise an error if you attempt to call it with an invalid state (such as :broken, see above). After the method runs successfully, the state machine will be left in the :to state.

Explained with some code examples

# Somewhere in your controller, perhaps
light = TrafficLight.new

# Move to the next state
light.forward!

# Do something based on the current state
case light.color
when "green"
  # do something green-related
when "yellow"
  # do something yellow-related
when "red"
  # do something red-related
end

Specific examples

We would also like to hear how you are using state machines in your code.

See also

Here are some other projects you might want to look at. Most of them are probably intended for ActiveRecord. They take different approaches, which is pretty interesting. If you find something you like in these other projects, let us know. Maybe we can incorporate some of your favorite parts. That said, I do not want to create a Frankenstein. :)

dm-is-state_machine's People

Contributors

dkubb avatar snusnu avatar solnic avatar bernerdschaefer avatar michaelklishin avatar myabc avatar namelessjon avatar somebee avatar paul avatar

Stargazers

Angus H. avatar Don Wei avatar

Watchers

Don Wei avatar James Cloos avatar

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.