Class: Kongo::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/kongo.rb

Overview

Cursor is an object that wraps around a Mongo::Cursor, wrapping objects it returns in Kongo::Model objects.

Instance Method Summary collapse

Constructor Details

#initialize(cursor, coll) ⇒ Cursor

‘initialize` is typically only used internally by Kongo::Collection when it returns cursors.



157
158
159
160
# File 'lib/kongo.rb', line 157

def initialize(cursor, coll)
  @coll = coll
  @cursor = cursor
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

Any method is forwarded to its wrapped cursor.



164
165
166
# File 'lib/kongo.rb', line 164

def method_missing(*args, &block)
  @cursor.send(*args, &block)
end

Instance Method Details

#eachObject

‘each` yields Kongo::Model objects.



177
178
179
# File 'lib/kongo.rb', line 177

def each
  @cursor.each { |e| yield Model.new(e, @coll) }
end

#nextObject

‘next` wraps responses in Kongo::Model.



170
171
172
173
# File 'lib/kongo.rb', line 170

def next
  (e = @cursor.next).is_a?(Hash) ?
    Model.new(e, @coll) : e
end

#to_aObject

‘to_a` returns an array of Kongo::Model.



193
194
195
196
197
# File 'lib/kongo.rb', line 193

def to_a
  arr = []
  each { |e| arr << e }
  arr
end

#to_enumObject

‘to_enum` returns an Enumerator which yields the results of the cursor.



183
184
185
186
187
188
189
# File 'lib/kongo.rb', line 183

def to_enum
  Enumerator.new do |yielder|
    while @cursor.has_next?
      yielder.yield(self.next)
    end
  end
end