state_machine
https://github.com/pluginaweek/state_machine
Ruby
Adds support for creating state machines for attributes on any Ruby class
StateMachine::EventCollection#valid_for
Gets the list of events that can be fired 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(:state).events
vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
events.valid_for(vehicle) # => [#<StateMachine::Event name=:ignite transitions=[:parked => :idling]>]
vehicle.state = 'idling'
events.valid_for(vehicle) # => [#<StateMachine::Event name=:park transitions=[:idling => :parked]>]Edit
git clone [email protected]:pluginaweek/state_machine.git
cd state_machine
open lib/state_machine/event_collection.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-StateMachine--EventCollection-valid_for-for-pr
# Commit to gitgit add lib/state_machine/event_collection.rbgit commit -m "better docs for StateMachine::EventCollection#valid_for"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-StateMachine--EventCollection-valid_for-for-pr
hub pull-request
# Celebrate!