class ROM::Finalize::FinalizeMappers

Attributes

mapper_classes[R]
mapper_objects[R]
registry_hash[R]

Public Class Methods

new(mapper_classes, mapper_objects) click to toggle source

@api private

# File lib/rom/setup/finalize/finalize_mappers.rb, line 11
def initialize(mapper_classes, mapper_objects)
  @mapper_classes = mapper_classes
  @mapper_objects = mapper_objects

  check_duplicate_registered_mappers

  @registry_hash = [@mapper_classes.map(&:base_relation) + @mapper_objects.keys]
    .flatten
    .uniq
    .each_with_object({}) { |n, h| h[n] = {} }
end

Public Instance Methods

run!() click to toggle source

@api private

# File lib/rom/setup/finalize/finalize_mappers.rb, line 24
def run!
  cache = Cache.new

  mappers = registry_hash.each_with_object({}) do |(relation_name, relation_mappers), h|
    relation_mappers.update(build_mappers(relation_name))

    if mapper_objects.key?(relation_name)
      relation_mappers.update(mapper_objects[relation_name])
    end

    h[relation_name] = MapperRegistry.new(relation_mappers, cache: cache)
  end

  Registry.new(mappers, cache: cache)
end

Private Instance Methods

build_mappers(relation_name) click to toggle source
# File lib/rom/setup/finalize/finalize_mappers.rb, line 55
def build_mappers(relation_name)
  mapper_classes
    .select { |klass| klass.base_relation == relation_name }
    .each_with_object({}) { |klass, h| h[klass.register_as || klass.relation] = klass.build }
end
check_duplicate_registered_mappers() click to toggle source
# File lib/rom/setup/finalize/finalize_mappers.rb, line 42
def check_duplicate_registered_mappers
  mapper_relation_register = mapper_classes.map { |mapper_class| [mapper_class.relation, mapper_class.register_as].compact }
  return if mapper_relation_register.uniq.count == mapper_classes.count

  mapper_relation_register.select { |relation_register_as| mapper_relation_register.count(relation_register_as) > 1 }
    .uniq
    .each do |duplicated_mappers|
      raise MapperAlreadyDefinedError,
            "Mapper with `register_as #{duplicated_mappers.last.inspect}` registered more " \
            "than once for relation #{duplicated_mappers.first.inspect}"
    end
end