Skip to content

Instantly share code, notes, and snippets.

@abevoelker
Created June 2, 2012 03:37
Show Gist options
  • Save abevoelker/2856419 to your computer and use it in GitHub Desktop.
Save abevoelker/2856419 to your computer and use it in GitHub Desktop.
require 'state_machine'
class LightSwitch
attr_accessor :dark_outside
alias :dark_outside? :dark_outside
state_machine :state, :initial => :_off_ do
event :turn_on do
transition :_off_ => :_on_, :if => lambda {|ls| ls.dark_outside? }
end
event :turn_off do
transition :_on_ => :_off_
end
end
end
ls = LightSwitch.new
ls.state # => "_off_"
ls.dark_outside = false
ls.turn_on # => false
ls.state # => "_off_"
ls.dark_outside = true
ls.turn_on # => true
ls.state # => "_on_"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment