class Monga::CallbackCursor

Public Instance Methods

all() { |err| ... } click to toggle source
# File lib/monga/cursor.rb, line 213
def all
  documents = []
  each_batch do |err, batch, iter|
    if err
      block_given? ? yield(err) : raise(err)
    else
      if iter
        documents += batch
        iter.next
      else
        block_given? ? yield(nil, documents) : documents
      end
    end
  end
end
each_batch(&blk) click to toggle source
# File lib/monga/cursor.rb, line 156
def each_batch(&blk)
  iter_more = true
  iterator = Proc.new do
    if iter_more
      next_batch do |err, batch, more|
        iter_more = more
        (more || batch || err) ? blk.call(err, batch, iterator) : blk.call
      end
    else
      # iteration stopped
      blk.call
    end
  end
  class << iterator
    alias :next :call
  end
  iterator.next
end
each_doc(&blk) click to toggle source
# File lib/monga/cursor.rb, line 193
def each_doc(&blk)
  iter_more = true
  iterator = Proc.new do
    if iter_more
      next_doc do |err, doc, more|
        iter_more = more
        (more || doc || err) ? blk.call(err, doc, iterator) : blk.call
      end
    else
      # iteration stopped
      blk.call
    end
  end
  class << iterator
    alias :next :call
  end
  iterator.next
end
Also aliased as: each_document
each_document(&blk)
Alias for: each_doc
first() { |err| ... } click to toggle source
# File lib/monga/cursor.rb, line 229
def first
  limit(1).all do |err, resp|
    if err
      block_given? ? yield(err) : raise(err)
    else
      block_given? ? yield(nil, resp.first) : resp.first
    end
  end
end
next_batch() { |err, batch, more| ... } click to toggle source
# File lib/monga/cursor.rb, line 146
def next_batch
  get_more(get_batch_size) do |err, batch, more|
    if block_given?
      yield(err, batch, more)
    else
      err ? raise(err) : [batch, more]
    end
  end
end
next_doc() { |nil, doc, more?| ... } click to toggle source
# File lib/monga/cursor.rb, line 175
def next_doc
  if doc = @fetched_docs.shift
    block_given? ? yield(nil, doc, more?) : [doc, more?]
  else
    get_more(get_batch_size) do |err, batch, more|
      if err
        block_given? ? yield(err, nil, false) : raise(err)
      else
        @fetched_docs = batch
        doc = @fetched_docs.shift
        m = more || @fetched_docs.any?
        block_given? ? yield(err, doc, m) : [doc, m]
      end
    end
  end
end
Also aliased as: next_document
next_document()
Alias for: next_doc