Sha256: a4e50ea8f0365ca23bfd9f0609ec39aeb0b86641cca81bfd260ffdccbf37c802

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module ROM

  # Exposes mapped tuples via enumerable interface
  #
  # See example for each method
  #
  # @api public
  class Reader
    include Enumerable
    include Equalizer.new(:path, :relation, :mapper)

    attr_reader :path, :relation, :header, :mappers, :mapper

    # @api private
    def initialize(path, relation, mappers = {})
      @path = path.to_s
      @relation = relation
      @mappers = mappers

      names = @path.split('.')

      mapper_key = names.reverse.detect { |name| mappers.key?(name.to_sym) }
      @mapper = mappers.fetch(mapper_key.to_sym)
      @header = mapper.header
    end

    # Yields tuples mapped to objects
    #
    # @example
    #
    #   # accessing root relation
    #   rom.read(:users).each { |user| # ... }
    #
    #   # accessing virtual relations
    #   rom.read(:users).adults.recent.active.each { |user| # ... }
    #
    # @api public
    def each
      relation.each { |tuple| yield(mapper.load(tuple)) }
    end

    # @api private
    def respond_to_missing?(name, include_private = false)
      relation.respond_to?(name)
    end

    private

    # @api private
    def method_missing(name, *args, &block)
      new_relation = relation.public_send(name, *args, &block)

      splits = path.split('.')
      splits << name
      new_path = splits.join('.')

      self.class.new(new_path, new_relation, mappers)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.4.1 lib/rom/reader.rb