Class: Mongrep::QueryResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongrep/query_result.rb

Overview

A wrapper around mongo cursors

Constant Summary collapse

DELEGATED_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The methods to be delegated to the underlying mongo cursor

%i(limit projection skip sort).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_view, &initialize_model) ⇒ QueryResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of QueryResult.



13
14
15
16
# File 'lib/mongrep/query_result.rb', line 13

def initialize(collection_view, &initialize_model)
  @initialize_model = initialize_model
  @collection_view = collection_view
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



11
12
13
# File 'lib/mongrep/query_result.rb', line 11

def model_class
  @model_class
end

Instance Method Details

#countInteger

Returns The amount of documents in this result.

Returns:

  • (Integer)

    The amount of documents in this result



33
34
35
36
37
# File 'lib/mongrep/query_result.rb', line 33

def count
  @collection_view.count
ensure
  @collection_view.close_query
end

#each {|item| ... } ⇒ void

This method returns an undefined value.

Iterates over the query result

Yield Parameters:

  • item (Model)

    A model representing a document from the query result



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mongrep/query_result.rb', line 43

def each
  return enum_for(:each) unless block_given?

  begin
    @collection_view.each do |document|
      yield @initialize_model.call(document)
    end
  ensure
    @collection_view.close_query
  end
end