Skip to content

Instantly share code, notes, and snippets.

@damonjmurray
Last active July 20, 2016 06:17
Show Gist options
  • Save damonjmurray/d8154eec0251f8189780faf35ff08887 to your computer and use it in GitHub Desktop.
Save damonjmurray/d8154eec0251f8189780faf35ff08887 to your computer and use it in GitHub Desktop.
after_transition on: vs. after_transition to:
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
@damonjmurray
Copy link
Author

cheers @kgunbin for the explanation...

on: is for events
to: is for states

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment