Sha256: a230af61bb97af868917c515819a3727248f3f6fee1b613406f851b33ce555f0

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

module Elasticsearch
  module Model
    module Response

      # Encapsulates the collection of records returned from the database
      #
      # Implements Enumerable and forwards its methods to the {#records} object,
      # which is provided by an {Elasticsearch::Model::Adapter::Adapter} implementation.
      #
      class Records
        include Enumerable

        delegate :each, :empty?, :size, :slice, :[], :to_a, :to_ary, to: :records

        attr_accessor :options

        include Base

        # @see Base#initialize
        #
        def initialize(klass, response, options={})
          super

          # Include module provided by the adapter in the singleton class ("metaclass")
          #
          adapter = Adapter.from_class(klass)
          metaclass = class << self; self; end
          metaclass.__send__ :include, adapter.records_mixin

          self.options = options
          self
        end

        # Returns the hit IDs
        #
        def ids
          response.response['hits']['hits'].map { |hit| hit['_id'] }
        end

        # Returns the {Results} collection
        #
        def results
          response.results
        end

        # Yields [record, hit] pairs to the block
        #
        def each_with_hit(&block)
          records.to_a.zip(results).each(&block)
        end

        # Yields [record, hit] pairs and returns the result
        #
        def map_with_hit(&block)
          records.to_a.zip(results).map(&block)
        end

        # Delegate methods to `@records`
        #
        def method_missing(method_name, *arguments)
          records.respond_to?(method_name) ? records.__send__(method_name, *arguments) : super
        end

        # Respond to methods from `@records`
        #
        def respond_to?(method_name, include_private = false)
          records.respond_to?(method_name) || super
        end

      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
elasticsearch-model-6.1.2 lib/elasticsearch/model/response/records.rb
elasticsearch-model-6.1.1 lib/elasticsearch/model/response/records.rb
elasticsearch-model-6.1.0 lib/elasticsearch/model/response/records.rb
elasticsearch-model-6.0.0 lib/elasticsearch/model/response/records.rb
elasticsearch-model-6.0.0.pre lib/elasticsearch/model/response/records.rb
elasticsearch-model-5.1.0 lib/elasticsearch/model/response/records.rb
elasticsearch-model-5.0.2 lib/elasticsearch/model/response/records.rb
elasticsearch-model-2.0.1 lib/elasticsearch/model/response/records.rb
elasticsearch-model-5.0.1 lib/elasticsearch/model/response/records.rb
elasticsearch-model-2.0.0 lib/elasticsearch/model/response/records.rb
elasticsearch-model-5.0.0 lib/elasticsearch/model/response/records.rb
elasticsearch-model-0.1.9 lib/elasticsearch/model/response/records.rb