state_machine

https://github.com/pluginaweek/state_machine

Ruby

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

StateMachine::EventCollection#transitions_for

Gets the list of transitions that can be run on the given object.

Valid requirement options:
* <tt>:from</tt> - One or more states being transitioned from.  If none
  are specified, then this will be the object's current state.
* <tt>:to</tt> - One or more states being transitioned to.  If none are
  specified, then this will match any to state.
* <tt>:on</tt> - One or more events that fire the transition.  If none
  are specified, then this will match any event.
* <tt>:guard</tt> - Whether to guard transitions with the if/unless
  conditionals defined for each one.  Default is true.

== Examples

  class Vehicle
    state_machine :initial => :parked do
      event :park do
        transition :idling => :parked
      end
      
      event :ignite do
        transition :parked => :idling
      end
    end
  end
  
  events = Vehicle.state_machine.events
  
  vehicle = Vehicle.new                             # => #<Vehicle:0xb7c464b0 @state="parked">
  events.transitions_for(vehicle)                   # => [#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
  
  vehicle.state = 'idling'
  events.transitions_for(vehicle)                   # => [#<StateMachine::Transition attribute=:state event=:park from="idling" from_name=:idling to="parked" to_name=:parked>]
  
  # Search for explicit transitions regardless of the current state
  events.transitions_for(vehicle, :from => :parked) # => [#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]

Source | Google | Stack overflow

Edit

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

cd state_machine

open lib/state_machine/event_collection.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-StateMachine--EventCollection-transitions_for-for-pr


# Commit to git

git add lib/state_machine/event_collection.rbgit commit -m "better docs for StateMachine::EventCollection#transitions_for"


# 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--EventCollection-transitions_for-for-pr

hub pull-request


# Celebrate!