Class: Mongoo::Cursor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj_class, mongo_cursor) ⇒ Cursor

Returns a new instance of Cursor.


7
8
9
10
# File 'lib/mongoo/cursor.rb', line 7

def initialize(obj_class, mongo_cursor)
  @obj_class    = obj_class
  @mongo_cursor = mongo_cursor
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object


54
55
56
57
58
59
60
# File 'lib/mongoo/cursor.rb', line 54

def method_missing(name, *args)
  if @mongo_cursor.respond_to?(name)
    @mongo_cursor.send name, *args
  else
    super
  end
end

Instance Attribute Details

#mongo_cursorObject

Returns the value of attribute mongo_cursor


5
6
7
# File 'lib/mongoo/cursor.rb', line 5

def mongo_cursor
  @mongo_cursor
end

Instance Method Details

#batch_size(size = 0) ⇒ Object


49
50
51
52
# File 'lib/mongoo/cursor.rb', line 49

def batch_size(size=0)
  @mongo_cursor.batch_size(size)
  self
end

#countObject


30
31
32
# File 'lib/mongoo/cursor.rb', line 30

def count
  @mongo_cursor.count
end

#eachObject


20
21
22
23
24
# File 'lib/mongoo/cursor.rb', line 20

def each
  @mongo_cursor.each do |doc|
    yield obj_from_doc(doc)
  end
end

#limit(number_to_return = nil) ⇒ Object


39
40
41
42
# File 'lib/mongoo/cursor.rb', line 39

def limit(number_to_return=nil)
  @mongo_cursor.limit(number_to_return)
  self
end

#next_documentObject Also known as: next


12
13
14
15
16
# File 'lib/mongoo/cursor.rb', line 12

def next_document
  if doc = @mongo_cursor.next_document
    obj_from_doc(doc)
  end
end

#obj_from_doc(doc) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoo/cursor.rb', line 62

def obj_from_doc(doc)
  obj = nil
  if Mongoo::IdentityMap.on?
    if obj = Mongoo::IdentityMap.read(doc["_id"])
      obj.merge!(doc)
    end
  end
  obj ||= @obj_class.new(doc, true)
  Mongoo::IdentityMap.write(obj) if Mongoo::IdentityMap.on?
  obj
end

#skip(number_to_return = nil) ⇒ Object


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

def skip(number_to_return=nil)
  @mongo_cursor.skip(number_to_return)
  self
end

#sort(key_or_list, direction = nil) ⇒ Object


34
35
36
37
# File 'lib/mongoo/cursor.rb', line 34

def sort(key_or_list, direction=nil)
  @mongo_cursor.sort(key_or_list, direction)
  self
end

#to_aObject


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

def to_a
  @mongo_cursor.to_a.collect { |doc| obj_from_doc(doc) }
end