Module: AASM::Persistence::DynamoidPersistence

Defined in:
lib/aasm/persistence/dynamoid_persistence.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aasm/persistence/dynamoid_persistence.rb', line 4

def self.included(base)
  base.send(:include, AASM::Persistence::Base)
  base.send(:include, AASM::Persistence::DynamoidPersistence::InstanceMethods)

  base.after_initialize :aasm_ensure_initial_state

  # Because Dynamoid only use define_method to add attribute assignment method in Class.
  #
  # In AASM::Base.initialize, it redefines and calls super in this method without superclass method.
  # We override method_missing to solve this problem.
  #
  base.class_eval %Q(
    def method_missing(method_name, *arguments, &block)
      if (AASM::StateMachineStore.fetch(self.class, true).machine_names.map { |state_machine_name| self.class.aasm(state_machine_name).attribute_name.to_s + "=" }).include? method_name.to_s
        attribute_name = method_name.to_s.gsub("=", '')
        write_attribute(attribute_name.to_sym, *arguments)
      else
        super
      end
    end
  )
end