Class: Humanoid::Cursor

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

Constant Summary collapse

OPERATIONS =

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

[
  :admin,
  :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 Humanoid::Cursor.

Options:

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

Example:

Humanoid::Cursor.new(Person, cursor)



59
60
61
# File 'lib/humanoid/cursor.rb', line 59

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



27
28
29
# File 'lib/humanoid/cursor.rb', line 27

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 }



43
44
45
46
47
# File 'lib/humanoid/cursor.rb', line 43

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

#next_documentObject

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

Example:

cursor.next_document



69
70
71
# File 'lib/humanoid/cursor.rb', line 69

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

#to_aObject

Returns an array of all the documents in the cursor.

Example:

cursor.to_a



78
79
80
# File 'lib/humanoid/cursor.rb', line 78

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