Sha256: 4f3c20c8981622372ac0d19f4c3f58229adada607fde04b0b6b5fdf1c11918f3

Contents?: true

Size: 1.41 KB

Versions: 19

Compression:

Stored size: 1.41 KB

Contents

module Mongoo
  class Cursor
    include Enumerable

    attr_accessor :mongo_cursor

    def initialize(obj_class, mongo_cursor)
      @obj_class    = obj_class
      @mongo_cursor = mongo_cursor
    end

    def next_document
      if doc = @mongo_cursor.next_document
        obj_from_doc(doc)
      end
    end

    alias :next :next_document

    def each
      @mongo_cursor.each do |doc|
        yield obj_from_doc(doc)
      end
    end

    def to_a
      @mongo_cursor.to_a.collect { |doc| obj_from_doc(doc) }
    end

    def count
      @mongo_cursor.count
    end

    def sort(key_or_list, direction=nil)
      @mongo_cursor.sort(key_or_list, direction)
      self
    end

    def limit(number_to_return=nil)
      @mongo_cursor.limit(number_to_return)
      self
    end

    def skip(number_to_return=nil)
      @mongo_cursor.skip(number_to_return)
      self
    end

    def batch_size(size=0)
      @mongo_cursor.batch_size(size)
      self
    end

    def method_missing(name, *args)
      if @mongo_cursor.respond_to?(name)
        @mongo_cursor.send name, *args
      else
        super
      end
    end
  
    def obj_from_doc(doc)
      obj = nil
      if Mongoo::IdentityMap.on?
        if obj = Mongoo::IdentityMap.read(doc["_id"])
          obj.merge!(doc)
        end
      end
      obj ||= @obj_class.new(doc, true)
      Mongoo::IdentityMap.write(obj) if Mongoo::IdentityMap.on?
      obj
    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mongoo-0.5.7 lib/mongoo/cursor.rb
mongoo-0.5.6 lib/mongoo/cursor.rb
mongoo-0.5.5 lib/mongoo/cursor.rb
mongoo-0.5.4 lib/mongoo/cursor.rb
mongoo-0.5.3 lib/mongoo/cursor.rb
mongoo-0.5.2 lib/mongoo/cursor.rb
mongoo-0.5.1 lib/mongoo/cursor.rb
mongoo-0.5.0 lib/mongoo/cursor.rb
mongoo-0.4.10 lib/mongoo/cursor.rb
mongoo-0.4.9 lib/mongoo/cursor.rb
mongoo-0.4.8 lib/mongoo/cursor.rb
mongoo-0.4.7 lib/mongoo/cursor.rb
mongoo-0.4.6 lib/mongoo/cursor.rb
mongoo-0.4.5 lib/mongoo/cursor.rb
mongoo-0.4.4 lib/mongoo/cursor.rb
mongoo-0.4.3 lib/mongoo/cursor.rb
mongoo-0.4.2 lib/mongoo/cursor.rb
mongoo-0.4.1 lib/mongoo/cursor.rb
mongoo-0.4.0 lib/mongoo/cursor.rb