Class: ROM::Schema::Inferrer Private

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes, Initializer
Defined in:
lib/rom/schema/inferrer.rb

Overview

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.

API:

  • private

Constant Summary collapse

MissingAttributesError =

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

API:

  • private

::Class.new(::StandardError) do
  def initialize(name, attributes)
    super(
      "Following attributes in #{Relation::Name[name].relation.inspect} schema cannot " \
      "be inferred and have to be defined explicitly: #{attributes.map(&:inspect).join(', ')}"
    )
  end
end
DEFAULT_ATTRIBUTES =

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

API:

  • private

[EMPTY_ARRAY, EMPTY_ARRAY].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializer

extended

Instance Attribute Details

#attr_classClass(ROM::Attribute) (readonly)

Returns:



46
# File 'lib/rom/schema/inferrer.rb', line 46

option :attr_class, default: -> { self.class.attr_class }

#attributes_inferrerROM::Schema::AttributesInferrer (readonly)

Returns:



56
# File 'lib/rom/schema/inferrer.rb', line 56

option :attributes_inferrer, default: -> { self.class.attributes_inferrer }

#enabledBoolean (readonly) Also known as: enabled?

Returns:



50
# File 'lib/rom/schema/inferrer.rb', line 50

option :enabled, default: -> { true }

Class Method Details

.attr_classClass(ROM::Attribute) .attr_class(value) ⇒ Class(ROM::Attribute)

Overloads:



25
# File 'lib/rom/schema/inferrer.rb', line 25

defines :attributes_inferrer, :attr_class

.attributes_inferrerProc .attributes_inferrer(value) ⇒ Proc

Overloads:

  • .attributes_inferrerProc

    Returns:

  • .attributes_inferrer(value) ⇒ Proc

    Parameters:

    Returns:



25
# File 'lib/rom/schema/inferrer.rb', line 25

defines :attributes_inferrer, :attr_class

Instance Method Details

#call(schema, gateway) ⇒ 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.

API:

  • private



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rom/schema/inferrer.rb', line 59

def call(schema, gateway)
  if enabled?
    inferred, missing = attributes_inferrer.(schema, gateway, options)
  else
    inferred, missing = DEFAULT_ATTRIBUTES
  end

  attributes = merge_attributes(schema.attributes, inferred)

  check_all_attributes_defined(schema, attributes, missing)

  { attributes: attributes }
end

#check_all_attributes_defined(schema, all_known, not_inferred) ⇒ 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.

Raises:

API:

  • private



74
75
76
77
78
# File 'lib/rom/schema/inferrer.rb', line 74

def check_all_attributes_defined(schema, all_known, not_inferred)
  not_defined = not_inferred - all_known.map(&:name)

  raise MissingAttributesError.new(schema.name, not_defined) unless not_defined.empty?
end

#merge_attributes(defined, inferred) ⇒ 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.

rubocop:disable Metrics/AbcSize

API:

  • private



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rom/schema/inferrer.rb', line 82

def merge_attributes(defined, inferred)
  type_lookup = lambda do |attrs, name|
    attrs.find { |a| a.name == name }.type
  end
  defined_with_type, defined_names =
    defined.each_with_object([[], []]) do |attr, (attrs, names)|
      attrs << if attr.type.nil?
                 attr.class.new(
                   type_lookup.(inferred, attr.name),
                   **attr.options
                 )
               else
                 attr
               end
      names << attr.name
    end

  defined_with_type + inferred.reject do |attr|
    defined_names.include?(attr.name)
  end
end