class ROM::Schema::Inferrer

@api private

Constants

DEFAULT_ATTRIBUTES
MissingAttributesError

Public Instance Methods

call(schema, gateway) click to toggle source

@api private

# File lib/rom/schema/inferrer.rb, line 61
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) click to toggle source

@api private

# File lib/rom/schema/inferrer.rb, line 76
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) click to toggle source

@api private

# File lib/rom/schema/inferrer.rb, line 83
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