Module: EnumMachine::DriverActiveRecord

Defined in:
lib/enum_machine/driver_active_record.rb

Instance Method Summary collapse

Instance Method Details

#enum_machine(attr, enum_values, i18n_scope: nil, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/enum_machine/driver_active_record.rb', line 6

def enum_machine(attr, enum_values, i18n_scope: nil, &block)
  klass = self

  i18n_scope ||= "#{klass.base_class.to_s.underscore}.#{attr}"

  enum_const_name = attr.to_s.upcase
  machine = Machine.new(enum_values, klass, enum_const_name)
  machine.instance_eval(&block) if block

  enum_klass = BuildClass.call(enum_values: enum_values, i18n_scope: i18n_scope, machine: machine)

  enum_value_klass = BuildAttribute.call(enum_values: enum_values, i18n_scope: i18n_scope, machine: machine)
  enum_value_klass.extend(AttributePersistenceMethods[attr, enum_values])

  enum_klass.const_set :VALUE_KLASS, enum_value_klass

  # Hash.new with default_proc for working with custom values not defined in enum list
  value_attribute_mapping = Hash.new { |hash, enum_value| hash[enum_value] = enum_klass::VALUE_KLASS.new(enum_value).freeze }
  enum_klass.define_singleton_method(:value_attribute_mapping) { value_attribute_mapping }

  if machine.transitions?
    klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
      before_save :__enum_machine_#{attr}_before_save
      after_save :__enum_machine_#{attr}_after_save

      def __enum_machine_#{attr}_before_save
        if (attr_changes = changes['#{attr}']) && !@__enum_machine_#{attr}_skip_transitions
          value_was, value_new = *attr_changes
          self.class::#{enum_const_name}.machine.fetch_before_transitions(attr_changes).each do |block|
            @__enum_machine_#{attr}_forced_value = value_was
            instance_exec(self, value_was, value_new, &block)
          ensure
            @__enum_machine_#{attr}_forced_value = nil
          end
        end
      end

      def __enum_machine_#{attr}_after_save
        if (attr_changes = previous_changes['#{attr}']) && !@__enum_machine_#{attr}_skip_transitions
          self.class::#{enum_const_name}.machine.fetch_after_transitions(attr_changes).each { |block| instance_exec(self, *attr_changes, &block) }
        end
      end
    RUBY
  end

  define_methods = Module.new
  define_methods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def state
    #   enum_value = @__enum_machine_state_forced_value || super()
    #   return unless enum_value
    #
    #   unless @__enum_value_state == enum_value
    #     @__enum_value_state = self.class::STATE.value_attribute_mapping[enum_value].dup
    #     @__enum_value_state.parent = self
    #     @__enum_value_state.freeze
    #   end
    #
    #   @__enum_value_state
    # end
    #
    # def skip_state_transitions
    #   @__enum_machine_state_skip_transitions = true
    #   yield
    # ensure
    #   @__enum_machine_state_skip_transitions = false
    # end
    #
    # def initialize_dup(other)
    #   @__enum_value_state = nil
    #   super
    # end

    def #{attr}
      enum_value = @__enum_machine_#{attr}_forced_value || super()
      return unless enum_value

      unless @__enum_value_#{attr} == enum_value
        @__enum_value_#{attr} = self.class::#{enum_const_name}.value_attribute_mapping[enum_value].dup
        @__enum_value_#{attr}.parent = self
        @__enum_value_#{attr}.freeze
      end

      @__enum_value_#{attr}
    end

    def skip_#{attr}_transitions
      @__enum_machine_#{attr}_skip_transitions = true
      yield
    ensure
      @__enum_machine_#{attr}_skip_transitions = false
    end

    def initialize_dup(other)
      @__enum_value_#{attr} = nil
      super
    end
  RUBY

  enum_decorator =
    Module.new do
      define_singleton_method(:included) do |decorating_klass|
        decorating_klass.prepend define_methods
        decorating_klass.const_set enum_const_name, enum_klass
      end
    end
  enum_klass.define_singleton_method(:decorator_module) { enum_decorator }

  klass.include(enum_decorator)

  enum_decorator
end