Created
December 9, 2010 20:28
-
-
Save pklingem/735281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
State Machine (Finite-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