Skip to content

Instantly share code, notes, and snippets.

@oki
Created December 15, 2009 01:00
Show Gist options
  • Select an option

  • Save oki/256618 to your computer and use it in GitHub Desktop.

Select an option

Save oki/256618 to your computer and use it in GitHub Desktop.
state_machine> t = TrafficLight.new
state_machine> t.change_color; t.state_name
=> :red_yellow
state_machine> t.change_color; t.state_name
=> :green
state_machine> t.change_color; t.state_name
=> :yellow
state_machine> t.change_color; t.state_name
=> :red
state_machine> t.change_color; t.state_name
=> :red_yellow
state_machine> t.can_set_red?
=> false
state_machine> t.can_set_green?
=> true
state_machine> t.can_set_yellow?
=> false
state_machine> t.set_green
=> true
state_machine> t.can_set_green?
=> false
state_machine> t.can_set_red
t.can_set_red_yellow? t.can_set_red?
state_machine> t.can_set_red?
=> false
state_machine> t.can_set_yellow?
=> true
state_machine>
class TrafficLight < ActiveRecord::Base
state_machine :state, :initial => :red do
event :change_color do
transition :red => :red_yellow
transition :red_yellow => :green
transition :green => :yellow
transition :yellow => :red
end
event :set_green do
transition :red_yellow => :green
end
event :set_yellow do
transition :green => :yellow
end
event :set_red do
transition :yellow => :red
end
event :set_red_yellow do
transition :red => :red_yellow
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment