0

I'm trying to work on a finite state machine with AASM in Ruby. This is a part of my code:

event :Orthography, :before => :to_lowercase do
    puts "Check Orthography"
    transitions :from => :Initialized, :to => :UniquenessChecked
end

event :Uniqueness do
    puts "Check Uniqueness"
    transitions :from => :UniquenessChecked, :to => :OrthographyChecked
end

... 

def to_lowercase
    puts "To lowercase test"
end

I get as puts log:

Check Orthography
Check Uniqueness
To lowercase test

But I expect, because I use the before callback:

To lowercase test
Check Orthography
Check Uniqueness

How can I do something before or on enter of a event?

Jeroen Steen
  • 531
  • 8
  • 22

1 Answers1

1

Your to_lowercase method is called before the event.

I guess you are confused by the Check Orthography and Check Uniqueness outputs that happen before the To lowercase test. This happens because the first two puts are defined on class level and are printed when the file is read and the state machine is configured.

Whereas the last puts is called in a method, when the instance is already created and a event is fired.

You can test that by defining the state machine, but not firing any events: You will still see the output from the state machine definition.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • I see a difference when I.. fire a event "term.Orthography", or not. It will show or not show the puts logs, from the to_lowercase function. But I still don't understand the order. I want to see the "To lowercase test" log before "Check Orthography". – Jeroen Steen Nov 12 '16 at 21:35
  • The point is: You cannot do that, because the block defining the state is run when the class is loaded. The event is fired when on an instance. That are two different things. When you want to run this in a certain order, than you must are them into methods and call them like you call the `to_lowercase` method in your example. – spickermann Nov 12 '16 at 22:50
  • 1
    I would also suggest to lower case your event names... Only classes or modules should be capitalized.. – BvuRVKyUVlViVIc7 May 25 '17 at 21:07