Class: Mongomatic::Cursor
- Inherits:
-
Object
- Object
- Mongomatic::Cursor
show all
- Includes:
- Enumerable
- Defined in:
- lib/mongomatic/cursor.rb
Overview
Wraps a Mongo::Cursor for managing result sets from MongoDB:
cursor = User.find({"zip" => 94107})
user1 = cursor.next
User.find({"zip" => 94107}).each { |u| puts u["name"] }
User.find({"zip" => 94107}).count
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(obj_class, mongo_cursor) ⇒ Cursor
Returns a new instance of Cursor.
14
15
16
17
|
# File 'lib/mongomatic/cursor.rb', line 14
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
19
20
21
|
# File 'lib/mongomatic/cursor.rb', line 19
def method_missing(name, *args)
@mongo_cursor.send name, *args
end
|
Instance Attribute Details
#mongo_cursor ⇒ Object
Returns the value of attribute mongo_cursor.
12
13
14
|
# File 'lib/mongomatic/cursor.rb', line 12
def mongo_cursor
@mongo_cursor
end
|
Instance Method Details
#current_limit ⇒ Object
42
43
44
|
# File 'lib/mongomatic/cursor.rb', line 42
def current_limit
@mongo_cursor.limit
end
|
#current_skip ⇒ Object
50
51
52
|
# File 'lib/mongomatic/cursor.rb', line 50
def current_skip
@mongo_cursor.skip
end
|
#each ⇒ Object
36
37
38
39
40
|
# File 'lib/mongomatic/cursor.rb', line 36
def each
@mongo_cursor.each do |doc|
yield(@obj_class.new(doc, false))
end
end
|
#empty? ⇒ Boolean
Is the cursor empty? This method is much more efficient than doing cursor.count == 0
24
25
26
|
# File 'lib/mongomatic/cursor.rb', line 24
def empty?
@mongo_cursor.has_next? == false
end
|
#limit(number_to_return) ⇒ Object
46
47
48
|
# File 'lib/mongomatic/cursor.rb', line 46
def limit(number_to_return)
@mongo_cursor.limit(number_to_return); self
end
|
#next_document ⇒ Object
Also known as:
next
28
29
30
31
32
|
# File 'lib/mongomatic/cursor.rb', line 28
def next_document
if doc = @mongo_cursor.next_document
@obj_class.new(doc, false)
end
end
|
#skip(number_to_skip) ⇒ Object
54
55
56
|
# File 'lib/mongomatic/cursor.rb', line 54
def skip(number_to_skip)
@mongo_cursor.skip(number_to_skip); self
end
|
#sort(key_or_list, direction = nil) ⇒ Object
58
59
60
|
# File 'lib/mongomatic/cursor.rb', line 58
def sort(key_or_list, direction = nil)
@mongo_cursor.sort(key_or_list, direction); self
end
|