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
- (Cursor) initialize(obj_class, mongo_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
- (Object) method_missing(name, *args)
19
20
21
|
# File 'lib/mongomatic/cursor.rb', line 19
def method_missing(name, *args)
@mongo_cursor.send name, *args
end
|
Instance Attribute Details
- (Object) mongo_cursor
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
- (Object) current_limit
42
43
44
|
# File 'lib/mongomatic/cursor.rb', line 42
def current_limit
@mongo_cursor.limit
end
|
- (Object) current_skip
50
51
52
|
# File 'lib/mongomatic/cursor.rb', line 50
def current_skip
@mongo_cursor.skip
end
|
- (Object) each
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
|
- (Boolean) empty?
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
|
- (Object) limit(number_to_return)
46
47
48
|
# File 'lib/mongomatic/cursor.rb', line 46
def limit(number_to_return)
@mongo_cursor.limit(number_to_return); self
end
|
- (Object) next_document
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
|
- (Object) skip(number_to_skip)
54
55
56
|
# File 'lib/mongomatic/cursor.rb', line 54
def skip(number_to_skip)
@mongo_cursor.skip(number_to_skip); self
end
|
- (Object) sort(key_or_list, direction = nil)
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
|