Search code examples
rubyaasm

How to pass a parameter to new/init in AASM gem


I'm trying to make a finite state machine chain with AASM gem. I want to check if a string is unique (not existing in the database).

I wrote:

require 'rubygems'
require 'aasm'

class Term
  include AASM

  aasm do
    state :Beginning, :initial => true
    state :CheckUniqueness

    def initialize(term)
        print term
    end

    event :UniquenessChecking do
        print "Check uniqueness"
      transitions :from => :Beginning, :to => :CheckUniqueness
    end

  end

end

term = Term.new("textstring")
term.CheckUniqueness

But when I use Term.new("textstring"), it doesn't allow me to pass a parameter I think, because I get a error:

`initialize': wrong number of arguments (1 for 0) (ArgumentError)
 from try.rb:24:in `new'
 from try.rb:24:in `<main>'

Is it possible to pass a parameter with init in AASM? I would like to know how I can do that?


Solution

  • You defined initialize inside of the aasm block, just move it out of that block:

    require 'rubygems'
    require 'aasm'
    
    class Term
      include AASM
    
      def initialize(term)
        print term
      end
    
      aasm do
        # ...
      end
    end