Module: EnumMachine::AttributePersistenceMethods

Defined in:
lib/enum_machine/attribute_persistence_methods.rb

Class Method Summary collapse

Class Method Details

.[](attr, enum_values) ⇒ 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
# File 'lib/enum_machine/attribute_persistence_methods.rb', line 6

def self.[](attr, enum_values)
  Module.new do
    define_singleton_method(:extended) do |klass|
      klass.attr_accessor :parent

      klass.define_method(:inspect) do
        "#<EnumMachine:BuildAttribute value=#{self} parent=#{parent.inspect}>"
      end

      enum_values.each do |enum_value|
        enum_name = enum_value.underscore

        klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          # def to_created!
          #   parent.update!('state' => 'created')
          # end

          def to_#{enum_name}!
            parent.update!('#{attr}' => '#{enum_value}')
          end
        RUBY
      end
    end
  end
end