Class: TinyMongo::Cursor
- Inherits:
-
Object
- Object
- TinyMongo::Cursor
- Includes:
- Enumerable, Mongo::Conversions
- Defined in:
- lib/tinymongo/cursor.rb
Instance Method Summary collapse
- #count ⇒ Object
- #each ⇒ Object
- #explain ⇒ Object
- #forEach(*args) ⇒ Object
- #has_next? ⇒ Boolean
- #hasNext ⇒ Object
-
#initialize(cursor, model_class) ⇒ Cursor
constructor
instance attributes in Mongo::Cursor.
- #limit(number_to_return = nil) ⇒ Object
- #mongo_cursor ⇒ Object
- #next! ⇒ Object
- #next_document ⇒ Object
- #nextDocument ⇒ Object
- #size ⇒ Object
- #skip(number_to_skip = nil) ⇒ Object
- #sort(key_or_list, direction = nil) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(cursor, model_class) ⇒ Cursor
instance attributes in Mongo::Cursor
8 9 10 11 |
# File 'lib/tinymongo/cursor.rb', line 8 def initialize(cursor, model_class) @_tinymongo_cursor = cursor @_tinymongo_model_class = model_class end |
Instance Method Details
#count ⇒ Object
17 18 19 |
# File 'lib/tinymongo/cursor.rb', line 17 def count @_tinymongo_cursor.count end |
#each ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/tinymongo/cursor.rb', line 26 def each num_returned = 0 while(has_next? && (@_tinymongo_cursor.instance_variable_get(:@limit) <= 0 || num_returned < @_tinymongo_cursor.instance_variable_get(:@limit))) yield next! num_returned += 1 end end |
#explain ⇒ Object
39 40 41 |
# File 'lib/tinymongo/cursor.rb', line 39 def explain @_tinymongo_cursor.explain end |
#forEach(*args) ⇒ Object
35 36 37 |
# File 'lib/tinymongo/cursor.rb', line 35 def forEach(*args) each(*args) end |
#has_next? ⇒ Boolean
43 44 45 |
# File 'lib/tinymongo/cursor.rb', line 43 def has_next? @_tinymongo_cursor.has_next? end |
#hasNext ⇒ Object
47 48 49 |
# File 'lib/tinymongo/cursor.rb', line 47 def hasNext has_next? end |
#limit(number_to_return = nil) ⇒ Object
51 52 53 |
# File 'lib/tinymongo/cursor.rb', line 51 def limit(number_to_return=nil) call_and_wrap_retval_in_tinymongo_cursor(:limit, [number_to_return]) end |
#mongo_cursor ⇒ Object
13 14 15 |
# File 'lib/tinymongo/cursor.rb', line 13 def mongo_cursor @_tinymongo_cursor end |
#next! ⇒ Object
55 56 57 58 |
# File 'lib/tinymongo/cursor.rb', line 55 def next! doc = @_tinymongo_cursor.next_document Helper.deserialize_hashes_in(doc) end |
#next_document ⇒ Object
60 61 62 |
# File 'lib/tinymongo/cursor.rb', line 60 def next_document next! end |
#nextDocument ⇒ Object
64 65 66 |
# File 'lib/tinymongo/cursor.rb', line 64 def nextDocument next! end |
#size ⇒ Object
21 22 23 24 |
# File 'lib/tinymongo/cursor.rb', line 21 def size num = (count - skip) num = (num > 0) ? (((num > limit) && limit != 0) ? limit : num) : 0 end |
#skip(number_to_skip = nil) ⇒ Object
68 69 70 |
# File 'lib/tinymongo/cursor.rb', line 68 def skip(number_to_skip=nil) call_and_wrap_retval_in_tinymongo_cursor(:skip, [number_to_skip]) end |
#sort(key_or_list, direction = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/tinymongo/cursor.rb', line 72 def sort(key_or_list, direction=nil) if(key_or_list.kind_of? Hash) key_or_list = key_or_list.map { |k, v| [k.to_s, convert_ascending_descending(v)] } elsif(key_or_list.kind_of? Array) key_or_list = key_or_list.map { |o| (o.kind_of? Array) ? [o[0].to_s, convert_ascending_descending(o[1])] : nil }.compact end call_and_wrap_retval_in_tinymongo_cursor(:sort, [key_or_list, direction]) end |
#to_a ⇒ Object
82 83 84 85 86 87 |
# File 'lib/tinymongo/cursor.rb', line 82 def to_a return [] if @_tinymongo_cursor.nil? hashes = @_tinymongo_cursor.to_a hashes.map { |hash| Helper.deserialize_hashes_in(hash) } end |