Sha256: 9ee822840d95c0b6bf18ce4e4b33235276dbf484e44a38525a265854cce4adc2

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc
  class Cursor
    include Enumerable
    # Operations on the Mongo::Cursor object that will not get overriden by the
    # Mongoid::Cursor are defined here.
    OPERATIONS = [
      :admin,
      :close,
      :closed?,
      :count,
      :explain,
      :fields,
      :full_collection_name,
      :hint,
      :limit,
      :order,
      :query_options_hash,
      :query_opts,
      :selector,
      :skip,
      :snapshot,
      :sort,
      :timeout
    ]

    attr_reader :collection

    # The operations above will all delegate to the proxied Mongo::Cursor.
    #
    # Example:
    #
    # <tt>cursor.close</tt>
    OPERATIONS.each do |name|
      define_method(name) { |*args| @cursor.send(name, *args) }
    end

    # Iterate over each document in the cursor and yield to it.
    #
    # Example:
    #
    # <tt>cursor.each { |doc| p doc.title }</tt>
    def each
      @cursor.each do |document|
        yield Mongoid::Factory.build(document)
      end
    end

    # Create the new +Mongoid::Cursor+.
    #
    # Options:
    #
    # collection: The Mongoid::Collection instance.
    # cursor: The Mongo::Cursor to be proxied.
    #
    # Example:
    #
    # <tt>Mongoid::Cursor.new(collection, cursor)</tt>
    def initialize(collection, cursor)
      @collection, @cursor = collection, cursor
    end

    # Return the next document in the cursor. Will instantiate a new Mongoid
    # document with the attributes.
    #
    # Example:
    #
    # <tt>cursor.next_document</tt>
    def next_document
      Mongoid::Factory.build(@cursor.next_document)
    end

    # Returns an array of all the documents in the cursor.
    #
    # Example:
    #
    # <tt>cursor.to_a</tt>
    def to_a
      @cursor.to_a.collect { |attrs| Mongoid::Factory.build(attrs) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-1.2.3 lib/mongoid/cursor.rb
mongoid-1.2.2 lib/mongoid/cursor.rb
mongoid-1.2.1 lib/mongoid/cursor.rb
mongoid-1.2.0 lib/mongoid/cursor.rb