Class: ROM::Schema::Inferrer Private

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes, Initializer
Defined in:
core/lib/rom/schema/inferrer.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.

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.

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.

[EMPTY_ARRAY, EMPTY_ARRAY].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attr_classClass(ROM::Attribute) (readonly)

Returns:



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

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

#attributes_inferrerROM::Schema::AttributesInferrer (readonly)

Returns:

  • (ROM::Schema::AttributesInferrer)


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

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

#enabledBoolean (readonly) Also known as: enabled?

Returns:

  • (Boolean)


50
# File 'core/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 'core/lib/rom/schema/inferrer.rb', line 25

defines :attributes_inferrer, :attr_class

.attributes_inferrerProc .attributes_inferrer(value) ⇒ Proc

Overloads:

  • .attributes_inferrerProc

    Returns:

    • (Proc)
  • .attributes_inferrer(value) ⇒ Proc

    Parameters:

    • value (Proc)

    Returns:

    • (Proc)


25
# File 'core/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.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'core/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.



74
75
76
77
78
# File 'core/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.



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

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