state_machine

https://github.com/pluginaweek/state_machine

Ruby

Adds support for creating state machines for attributes on any Ruby class

StateMachine::StateCollection#match

Determines the current state of the given object as configured by this
state machine.  This will attempt to find a known state that matches
the value of the attribute on the object.

== Examples

  class Vehicle
    state_machine :initial => :parked do
      other_states :idling
    end
  end
  
  states = Vehicle.state_machine.states
  
  vehicle = Vehicle.new         # => #<Vehicle:0xb7c464b0 @state="parked">
  states.match(vehicle)         # => #<StateMachine::State name=:parked value="parked" initial=true>
  
  vehicle.state = 'idling'
  states.match(vehicle)         # => #<StateMachine::State name=:idling value="idling" initial=true>
  
  vehicle.state = 'invalid'
  states.match(vehicle)         # => nil

Source | Google | Stack overflow

Edit

git clone [email protected]:pluginaweek/state_machine.git

cd state_machine

open lib/state_machine/state_collection.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-StateMachine--StateCollection-match-for-pr


# Commit to git

git add lib/state_machine/state_collection.rbgit commit -m "better docs for StateMachine::StateCollection#match"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-StateMachine--StateCollection-match-for-pr

hub pull-request


# Celebrate!