Sha256: cb440891be5e17d66d7ae0ccf13a225b01cd60cea77a3a17d8eca6d641b51771

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'rom/mapper_registry'

module ROM
  # @api private
  class ReaderBuilder
    DEFAULT_OPTIONS = { inherit_header: true }.freeze

    attr_reader :relations, :readers

    # @api private
    def initialize(relations)
      @relations = relations
      @readers = {}
    end

    # @api private
    def call(name, input_options = {}, &block)
      with_options(input_options) do |options|
        parent = relations[options.fetch(:parent) { name }]

        builder = MapperBuilder.new(name, parent, options)
        builder.instance_exec(&block) if block
        mapper = builder.call

        mappers =
          if options[:parent]
            readers.fetch(parent.name).mappers
          else
            MapperRegistry.new
          end

        mappers[name] = mapper

        unless options[:parent]
          readers[name] = Reader.build(
            name, parent, mappers, parent.class.relation_methods
          )
        end
      end
    end

    private

    def with_options(options)
      yield(DEFAULT_OPTIONS.merge(options))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.5.0 lib/rom/reader_builder.rb