Created
April 23, 2014 21:26
-
-
Save thoolihan/11233024 to your computer and use it in GitHub Desktop.
StateMachine Example
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
require 'rubygems' | |
require 'statemachine' | |
class MowerContext | |
def cut_mode | |
puts "blade start spinning" | |
end | |
def idle_mode | |
puts "engine idling" | |
end | |
def stop_mode | |
puts "completely stopped" | |
end | |
def chug | |
puts "chugging to start" | |
end | |
end | |
mower1 = Statemachine.build do | |
state :stopped do | |
on_entry :stop_mode | |
event :key, :running | |
on_exit :chug | |
end | |
state :running do | |
on_entry :idle_mode | |
event :key, :stopped | |
event :handle, :cutting | |
end | |
state :cutting do | |
on_entry :cut_mode | |
event :key, :stopped | |
event :handle, :running | |
end | |
context MowerContext.new | |
end | |
puts "===first use===" | |
puts "turning key" | |
mower1.key | |
puts "using handle" | |
mower1.handle | |
puts "using handle" | |
mower1.handle | |
puts "turning key" | |
mower1.key | |
puts "\n===another day===" | |
puts "turning key" | |
mower1.key | |
puts "using handle" | |
mower1.handle | |
puts "turning key" | |
mower1.key | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment