Last active
July 20, 2016 06:17
-
-
Save damonjmurray/d8154eec0251f8189780faf35ff08887 to your computer and use it in GitHub Desktop.
after_transition on: vs. after_transition to:
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 'state_machine' | |
module Sandpit | |
class Foo | |
state_machine :state, initial: :created do | |
event :start do | |
transition created: :running | |
transition running: same | |
end | |
event :stop do | |
transition running: :stopped | |
end | |
after_transition on: :running do |foo, _| | |
puts 'Congratulations on starting to run!' | |
end | |
after_transition on: :stopped do |foo, _| | |
puts 'Thank goodness you stopped...' | |
end | |
end | |
end | |
class Bar | |
state_machine :state, initial: :created do | |
event :start do | |
transition created: :running | |
transition running: same | |
end | |
event :stop do | |
transition running: :stopped | |
end | |
after_transition to: :running do |foo, _| | |
puts 'Congratulations on starting to run!' | |
end | |
after_transition to: :stopped do |foo, _| | |
puts 'Thank goodness you stopped...' | |
end | |
end | |
end | |
end | |
foo = Sandpit::Foo.new | |
# => #<Sandpit::Foo:0x007fa3a230f780 @state="created"> | |
bar = Sandpit::Bar.new | |
# => #<Sandpit::Bar:0x007fa3a232d280 @state="created"> | |
foo.start | |
# => true | |
bar.start | |
# Congratulations on starting to run! | |
# => true | |
foo.stop | |
# => true | |
bar.stop | |
# Thank goodness you stopped... | |
# => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cheers @kgunbin for the explanation...