state_machine

https://github.com/pluginaweek/state_machine

Ruby

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

StateMachine::Machine#initial_state

Gets the initial state of the machine for the given object. If a dynamic
initial state was configured for this machine, then the object will be
passed into the lambda block to help determine the actual state.

== Examples

With a static initial state:

  class Vehicle
    state_machine :initial => :parked do
      ...
    end
  end
  
  vehicle = Vehicle.new
  Vehicle.state_machine.initial_state(vehicle)  # => #<StateMachine::State name=:parked value="parked" initial=true>

With a dynamic initial state:

  class Vehicle
    attr_accessor :force_idle
    
    state_machine :initial => lambda {|vehicle| vehicle.force_idle ? :idling : :parked} do
      ...
    end
  end
  
  vehicle = Vehicle.new
  
  vehicle.force_idle = true
  Vehicle.state_machine.initial_state(vehicle)  # => #<StateMachine::State name=:idling value="idling" initial=false>
  
  vehicle.force_idle = false
  Vehicle.state_machine.initial_state(vehicle)  # => #<StateMachine::State name=:parked value="parked" initial=false>

Source | Google | Stack overflow

Edit

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

cd state_machine

open lib/state_machine/machine.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-StateMachine--Machine-initial_state-for-pr


# Commit to git

git add lib/state_machine/machine.rbgit commit -m "better docs for StateMachine::Machine#initial_state"


# 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--Machine-initial_state-for-pr

hub pull-request


# Celebrate!