state_machine in module

171 views
Skip to first unread message

Rob Ui

unread,
Nov 2, 2012, 4:17:21 AM11/2/12
to pluginaw...@googlegroups.com
Hi,
im trying move state_machine code from class to module, but cant make it work.
Sample code:

module SMtest
  state_machine do
    state :sample
   end
 end

Gives an error:

NoMethodError: undefined method `state_machine' for SMtest:Module

I tried couple things with no luck, is it not possible to put the SM in module or am I missing something?

Thank You,
Rob

François Beausoleil

unread,
Nov 2, 2012, 9:37:49 AM11/2/12
to pluginaw...@googlegroups.com
It is, but you have to run your code only after it's been included, such as:

module SMTest

  def self.included(base)
    base.state_machine :state, initial: :ready do
      event :run
    end
  end

end

You *may* need to use base.send(:state_machine, :state, initial: :ready) if the #state_machine method is private / protected.

Hope that helps!
François

--
You received this message because you are subscribed to the Google Groups "PluginAWeek: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pluginaweek-talk/-/Q2tzpwHi-XYJ.
To post to this group, send email to pluginaw...@googlegroups.com.
To unsubscribe from this group, send email to pluginaweek-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pluginaweek-talk?hl=en.

Rob Ui

unread,
Nov 8, 2012, 3:54:37 AM11/8/12
to pluginaw...@googlegroups.com
Hi François,
using class_eval did the trick. Here's the fixed sample code:

module SMtest
  def self.included(base)
    base.class_eval do

      state_machine do
        state :sample
      end
    end
  end
end

Thanks for your time,
Rob
Reply all
Reply to author
Forward
0 new messages