Class: ROM::Repository::StructAttributes Private

Inherits:
Module
  • Object
show all
Defined in:
lib/rom/repository/struct_attributes.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ StructAttributes

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of StructAttributes.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rom/repository/struct_attributes.rb', line 5

def initialize(attributes)
  super()

  define_constructor(attributes)

  module_eval do
    include Dry::Equalizer.new(*attributes)

    attr_reader(*attributes)

    define_method(:to_h) do
      attributes.each_with_object({}) do |attribute, h|
        h[attribute] = __send__(attribute)
      end
    end
  end
end

Instance Method Details

#define_constructor(attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rom/repository/struct_attributes.rb', line 23

def define_constructor(attributes)
  module_eval do
    def __missing_keyword__(keyword)
      raise ArgumentError.new("missing keyword: #{keyword}")
    end
    private :__missing_keyword__
  end

  kwargs = attributes.map { |a| "#{a}: __missing_keyword__(:#{a})" }.join(', ')

  ivs = attributes.map { |a| "@#{a}" }.join(', ')
  values = attributes.join(', ')

  assignment = attributes.size > 0 ? "#{ivs} = #{values}" : EMPTY_STRING

  module_eval("    def initialize(\#{kwargs})\n      \#{assignment}\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1)