Class: TinyMongo::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Mongo::Conversions
Defined in:
lib/tinymongo/cursor.rb

Instance Method Summary collapse

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

#countObject



17
18
19
# File 'lib/tinymongo/cursor.rb', line 17

def count
  @_tinymongo_cursor.count
end

#eachObject



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

#explainObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/tinymongo/cursor.rb', line 43

def has_next?
  @_tinymongo_cursor.has_next?
end

#hasNextObject



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_cursorObject



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_documentObject



60
61
62
# File 'lib/tinymongo/cursor.rb', line 60

def next_document
  next!
end

#nextDocumentObject



64
65
66
# File 'lib/tinymongo/cursor.rb', line 64

def nextDocument
  next!
end

#sizeObject



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_aObject



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