Class: MongoThing::Cursor
- Inherits:
-
Object
- Object
- MongoThing::Cursor
- Includes:
- Enumerable
- Defined in:
- lib/mongo_thing/cursor.rb
Constant Summary collapse
- OPERATIONS =
Operations on the Mongo::Cursor object that will not get overriden by the MongoThing::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
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate over each document in the cursor and yield to it.
-
#initialize(klass, cursor) ⇒ Cursor
constructor
Create the new
MongoThing::Cursor
. -
#next_document ⇒ Object
Return the next document in the cursor.
-
#to_a ⇒ Object
Returns an array of all the documents in the cursor.
Constructor Details
#initialize(klass, cursor) ⇒ Cursor
Create the new MongoThing::Cursor
.
Options:
collection: The MongoThing::Collection instance. cursor: The Mongo::Cursor to be proxied.
Example:
MongoThing::Cursor.new(Person, cursor)
59 60 61 |
# File 'lib/mongo_thing/cursor.rb', line 59 def initialize(klass, cursor) @klass, @cursor = klass, cursor end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
27 28 29 |
# File 'lib/mongo_thing/cursor.rb', line 27 def collection @collection end |
Instance Method Details
#each ⇒ Object
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/mongo_thing/cursor.rb', line 43 def each @cursor.each do |document| yield @klass.new(document) end end |
#next_document ⇒ Object
Return the next document in the cursor. Will instantiate a new MongoThing document with the attributes.
Example:
cursor.next_document
69 70 71 |
# File 'lib/mongo_thing/cursor.rb', line 69 def next_document @klass.new(@cursor.next_document) end |
#to_a ⇒ Object
Returns an array of all the documents in the cursor.
Example:
cursor.to_a
78 79 80 |
# File 'lib/mongo_thing/cursor.rb', line 78 def to_a @cursor.to_a.collect { |attrs| @klass.new(attrs) } end |