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
after_transition on:
vs.after_transition to:
The
state_machine
docs only ever seem to showon:
being used in the short-hand syntax that calls a method and not with a block. I've seenafter_transition on: :event_name do |obj, transition|
used in various places though. Haven't dug into the code yet to see why the behaviour is different, but probably something to be aware of.