Class: Mongoid::Cursor

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/mongoid/cursor.rb

Constant Summary collapse

OPERATIONS =

Operations on the Mongo::Cursor object that will not get overriden by the Mongoid::Cursor are defined here.

[
  :close,
  :closed?,
  :count,
  :explain,
  :fields,
  :full_collection_name,
  :hint,
  :limit,
  :order,
  :query_options_hash,
  :query_opts,
  :selector,
  :skip,
  :snapshot,
  :sort,
  :timeout
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, collection, cursor) ⇒ Cursor

Create the new Mongoid::Cursor.

Options:

collection: The Mongoid::Collection instance. cursor: The Mongo::Cursor to be proxied.

Example:

Mongoid::Cursor.new(Person, cursor)



58
59
60
# File 'lib/mongoid/cursor.rb', line 58

def initialize(klass, collection, cursor)
  @klass, @collection, @cursor = klass, collection, cursor
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



26
27
28
# File 'lib/mongoid/cursor.rb', line 26

def collection
  @collection
end

Instance Method Details

#eachObject

Iterate over each document in the cursor and yield to it.

Example:

cursor.each { |doc| p doc.title }



42
43
44
45
46
# File 'lib/mongoid/cursor.rb', line 42

def each
  @cursor.each do |document|
    yield Mongoid::Factory.build(@klass, document)
  end
end

#next_documentObject

Return the next document in the cursor. Will instantiate a new Mongoid document with the attributes.

Example:

cursor.next_document



68
69
70
# File 'lib/mongoid/cursor.rb', line 68

def next_document
  Mongoid::Factory.build(@klass, @cursor.next_document)
end

#to_aObject

Returns an array of all the documents in the cursor.

Example:

cursor.to_a



77
78
79
# File 'lib/mongoid/cursor.rb', line 77

def to_a
  @cursor.to_a.collect { |attrs| Mongoid::Factory.build(@klass, attrs) }
end