Skip to content

Instantly share code, notes, and snippets.

@obrie
Created March 19, 2011 18:27
Show Gist options
  • Save obrie/877688 to your computer and use it in GitHub Desktop.
Save obrie/877688 to your computer and use it in GitHub Desktop.
Demonstrates a possible technique for running events / defining callbacks
class Vehicle
attr_accessor :running
state_machine :initial => :parked do
after_transition {|object, transition| object.send(transition.event)}
event :ignite do
transition :parked => :idling
end
end
def ignite
self.running = true
end
end
v = Vehicle.new # => #<Vehicle:0xb722506c @state="parked">
v.state # => "parked"
v.running # => nil
v.fire_events(:ignite) # => true
v.state # => "idling"
v.running # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment