Sha256: 9b3560bb491aecc2d888598fac8aaff26486d06081810d8c68ad1b1cab28e113

Contents?: true

Size: 1.83 KB

Versions: 21

Compression:

Stored size: 1.83 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(@klass, 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(Person, cursor)</tt>
    def initialize(klass, collection, cursor)
      @klass, @collection, @cursor = klass, 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(@klass, @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(@klass, attrs) }
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

Version Path
mongoid-2.0.0.beta.7 lib/mongoid/cursor.rb
mongoid-2.0.0.beta.8 lib/mongoid/cursor.rb
mongoid-2.0.0.beta.9 lib/mongoid/cursor.rb
mongoid-1.9.1 lib/mongoid/cursor.rb
chhean-mongoid-2.0.1.beta1 lib/mongoid/cursor.rb
mongoid-2.0.0.beta.5 lib/mongoid/cursor.rb
mongoid-1.9.0 lib/mongoid/cursor.rb
mongoid-pre-2.0.0.beta1 lib/mongoid/cursor.rb
mongoid-2.0.0.alpha lib/mongoid/cursor.rb
mongoid-1.2.14 lib/mongoid/cursor.rb
mongoid-1.2.13 lib/mongoid/cursor.rb
mongoid-1.2.12 lib/mongoid/cursor.rb
mongoid-1.2.11 lib/mongoid/cursor.rb
mongoid-1.2.10 lib/mongoid/cursor.rb
mongoid-1.2.9 lib/mongoid/cursor.rb
mongoid-1.2.8 lib/mongoid/cursor.rb
mongoid-1.2.7 lib/mongoid/cursor.rb
mongoid-pre-2.0.0.pre lib/mongoid/cursor.rb
mongoid-1.2.6 lib/mongoid/cursor.rb
mongoid-1.2.5 lib/mongoid/cursor.rb