Class: Mongo::Cursor

Inherits:
Object show all
Includes:
JavaImpl::Utils
Defined in:
lib/jmongo/cursor.rb

Constant Summary

Constants included from JavaImpl::Utils

JavaImpl::Utils::SortingHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JavaImpl::Utils

#from_dbobject, #prep_fields, #prep_hint, #prep_id, #prep_sort, #raise_not_implemented, #sort_value, #system_name?, #to_dbobject, #trap_raise, #validate_name

Constructor Details

#initialize(collection, options = {}) ⇒ Cursor

Returns a new instance of Cursor.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jmongo/cursor.rb', line 25

def initialize(collection, options={})
  @collection = collection
  @j_collection = collection.j_collection
  @query_run = false
  @selector   = convert_selector_for_query(options[:selector])
  @fields     = convert_fields_for_query(options[:fields])
  @admin      = options.fetch(:admin, false)
  @order      = nil
  @batch_size = Mongo::DEFAULT_BATCH_SIZE
  @skip       = 0
  @limit      = 0
  _skip options[:skip]
  _limit options[:limit]
  _sort options[:order]
  _batch_size options[:batch_size]
  @hint       = options[:hint]
  @snapshot   = options[:snapshot]
  @explain    = options[:explain]
  @socket     = options[:socket]
  @timeout    = options.fetch(:timeout, true)
  @tailable   = options.fetch(:tailable, false)
  @transformer = options[:transformer]

  @full_collection_name = "#{@collection.db.name}.#{@collection.name}"

  spawn_cursor
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def collection
  @collection
end

#fieldsObject (readonly)

Returns the value of attribute fields.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def fields
  @fields
end

#full_collection_nameObject (readonly)

Returns the value of attribute full_collection_name.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def full_collection_name
  @full_collection_name
end

#hintObject (readonly)

Returns the value of attribute hint.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def hint
  @hint
end

#j_cursorObject (readonly)

Returns the value of attribute j_cursor.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def j_cursor
  @j_cursor
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def options
  @options
end

#orderObject (readonly)

Returns the value of attribute order.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def order
  @order
end

#selectorObject (readonly)

Returns the value of attribute selector.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def selector
  @selector
end

#snapshotObject (readonly)

Returns the value of attribute snapshot.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def snapshot
  @snapshot
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def timeout
  @timeout
end

#transformerObject (readonly)

Returns the value of attribute transformer.



20
21
22
# File 'lib/jmongo/cursor.rb', line 20

def transformer
  @transformer
end

Instance Method Details

#add_option(opt) ⇒ Object



76
77
78
79
80
# File 'lib/jmongo/cursor.rb', line 76

def add_option(opt)
  check_modifiable
  @j_cursor.addOption(opt)
  options
end

#alive?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/jmongo/cursor.rb', line 72

def alive?
  cursor_id != 0
end

#batch_size(size = nil) ⇒ Object



136
137
138
139
140
# File 'lib/jmongo/cursor.rb', line 136

def batch_size(size=nil)
  _batch_size(size)
  @j_cursor = @j_cursor.batchSize(@batch_size) if @batch_size
  self
end

#closeObject



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

def close
  @query_run = true
  @j_cursor.close
end

#closed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/jmongo/cursor.rb', line 68

def closed?
  cursor_id == 0
end

#count(skip_and_limit = false) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/jmongo/cursor.rb', line 193

def count(skip_and_limit = false)
  if skip_and_limit && @skip && @limit
    check_modifiable
    @j_cursor.size
  else
    @j_cursor.count
  end
end

#current_documentObject



98
99
100
# File 'lib/jmongo/cursor.rb', line 98

def current_document
  _xform(from_dbobject(@j_cursor.curr))
end

#cursor_idObject



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

def cursor_id
  @j_cursor.get_cursor_id
end

#eachObject

iterate directly from the mongo db



121
122
123
124
125
126
# File 'lib/jmongo/cursor.rb', line 121

def each
  check_modifiable
  while has_next?
    yield next_document
  end
end

#explainObject



202
203
204
# File 'lib/jmongo/cursor.rb', line 202

def explain
  from_dbobject @j_cursor.explain
end

#has_next?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/jmongo/cursor.rb', line 116

def has_next?
  @j_cursor.has_next?
end

#limit(number_to_return = nil) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/jmongo/cursor.rb', line 150

def limit(number_to_return=nil)
  _limit(number_to_return)
  wrap_invalid_op do
    @j_cursor = @j_cursor.limit(@limit) if @limit
  end
  self
end

#map(&block) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/jmongo/cursor.rb', line 206

def map(&block)
  ret = []
  rewind! unless has_next?
  while has_next?
    ret << block.call(__next)
  end
  ret
end

#next_documentObject Also known as: next



102
103
104
# File 'lib/jmongo/cursor.rb', line 102

def next_document
  _xform(has_next? ? __next : nil)
end

#query_optsObject



86
87
88
89
90
# File 'lib/jmongo/cursor.rb', line 86

def query_opts
  warn "The method Cursor#query_opts has been deprecated " +
    "and will removed in v2.0. Use Cursor#options instead."
  options
end

#remove_option(opt) ⇒ Object



92
93
94
95
96
# File 'lib/jmongo/cursor.rb', line 92

def remove_option(opt)
  check_modifiable
  @j_cursor.setOptions(options & ~opt)
  options
end

#rewind!Object



53
54
55
56
57
# File 'lib/jmongo/cursor.rb', line 53

def rewind!
  close
  @query_run = false
  spawn_cursor
end

#sizeObject



189
190
191
# File 'lib/jmongo/cursor.rb', line 189

def size
  @j_cursor.size
end

#skip(number_to_skip = nil) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/jmongo/cursor.rb', line 166

def skip(number_to_skip=nil)
  _skip(number_to_skip)
  wrap_invalid_op do
    @j_cursor = @j_cursor.skip(@skip) if @skip
  end
  self
end

#sort(key_or_list, direction = nil) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/jmongo/cursor.rb', line 181

def sort(key_or_list, direction=nil)
  _sort(key_or_list, direction)
  wrap_invalid_op do
    @j_cursor = @j_cursor.sort(@order) if @order
  end
  self
end

#to_aObject



215
216
217
218
219
220
221
222
# File 'lib/jmongo/cursor.rb', line 215

def to_a
  ret = []
  rewind! unless has_next?
  while has_next?
    ret << __next
  end
  ret
end

#to_setObject



224
225
226
# File 'lib/jmongo/cursor.rb', line 224

def to_set
  Set.new self.to_a
end