Skip to content

Instantly share code, notes, and snippets.

@pklingem
Created December 9, 2010 20:28
Show Gist options
  • Save pklingem/735281 to your computer and use it in GitHub Desktop.
Save pklingem/735281 to your computer and use it in GitHub Desktop.
# State Machine - http://en.wikipedia.org/wiki/State_machine
# Traffic Light - http://en.wikipedia.org/wiki/Traffic_light
# Workflow - http://en.wikipedia.org/wiki/Workflow
# Wizard - http://en.wikipedia.org/wiki/Wizard_%28software%29
# state_machine - https://github.com/pluginaweek/state_machine
# traffic_light - https://gist.github.com/735281
class TrafficLight
WAIT_TIME_IN_SECONDS = 5
state_machine :state, :initial => :red
state :red
state :yellow
state :green
state :blinking_red
state :blinking_yellow
after_transition :on => :slow, :do => :wait_then_stop
event :go do
transition :from => :red, :to => :green
end
event :slow do
transition :from => :green, :to => :yellow
end
event :stop do
transition :from => [:yellow, :blinking_yellow, :blinking_red], :to => :red
end
event :proceed_with_caution do
transition :from => any, :to => :blinking_yellow
end
event :stop_before_proceeding do
transition :from => any, :to => :blinking_red
end
end
def wait_then_stop
sleep WAIT_TIME_IN_SECONDS
stop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment