Class: ROM::StructCompiler Private

Inherits:
Dry::Types::Compiler
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/struct_compiler.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(*args) ⇒ StructCompiler

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 StructCompiler.



21
22
23
24
# File 'lib/rom/struct_compiler.rb', line 21

def initialize(*args)
  super
  @cache = cache.namespaced(:structs)
end

Instance Method Details

#call(*args) ⇒ Object Also known as: []

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.

Build a struct class based on relation header ast



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rom/struct_compiler.rb', line 29

def call(*args)
  cache.fetch_or_store(args) do
    name, header, ns = args
    attributes = header.map(&method(:visit)).compact

    if attributes.empty?
      ROM::OpenStruct
    else
      build_class(name, ROM::Struct, ns) do |klass|
        attributes.each do |(attr_name, type)|
          klass.attribute(attr_name, type)
        end
      end
    end
  end
end