class Stately
state_machine :state, initial: :pending do
state :approved
state :declined
event :approve do
transition pending: :approved
end
event :decline do
transition all => :declined
end
end
end
stately = Stately.new
stately.state
#=> :pending
stately.state_events
#=> [ :approve, :decline ]
stately.approve
stately.state_events
#=> [ :decline ]
If what you want to do is to prevent yourself from accidentally triggering an event that will throw an exception (much narrower scope than trying to see ALL events) then you can also do this...
stately = Stately.new
stately.state
#=> :pending
stately.can_approve?
#=> true
stately.can_decline?
#=> true
stately.approve
stately.can_approve?
#=> false
stately.can_decline?
#=> true