Module: AASM::ClassMethods

Defined in:
lib/aasm/aasm.rb

Instance Method Summary collapse

Instance Method Details

#aasm(*args, &block) ⇒ Object

this is the entry point for all state and event definitions

Raises:

  • (ArgumentError)

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aasm/aasm.rb', line 28

def aasm(*args, &block)
  if args[0].is_a?(Symbol) || args[0].is_a?(String)
    # using custom name
    state_machine_name = args[0].to_sym
    options = args[1] || {}
  else
    # using the default state_machine_name
    state_machine_name = :default
    options = args[0] || {}
  end

  AASM::StateMachineStore.fetch(self, true).register(state_machine_name, AASM::StateMachine.new(state_machine_name))

  # use a default despite the DSL configuration default.
  # this is because configuration hasn't been setup for the AASM class but we are accessing a DSL option already for the class.
  aasm_klass = options[:with_klass] || AASM::Base

  raise ArgumentError, "The class #{aasm_klass} must inherit from AASM::Base!" unless aasm_klass.ancestors.include?(AASM::Base)

  @aasm ||= Concurrent::Map.new
  if @aasm[state_machine_name]
    # make sure to use provided options
    options.each do |key, value|
      @aasm[state_machine_name].state_machine.config.send("#{key}=", value)
    end
  else
    # create a new base
    @aasm[state_machine_name] = aasm_klass.new(
      self,
      state_machine_name,
      AASM::StateMachineStore.fetch(self, true).machine(state_machine_name),
      options
    )
  end
  @aasm[state_machine_name].instance_eval(&block) if block # new DSL
  @aasm[state_machine_name]
end

#inherited(base) ⇒ Object

make sure inheritance (aka subclassing) works with AASM


21
22
23
24
25
# File 'lib/aasm/aasm.rb', line 21

def inherited(base)
  AASM::StateMachineStore.register(base, self)

  super
end