Sha256: bd7a72c9773bd6fc45cf7ce5fdef3648d9cc744132b57fdefd75f3b4d5f0b514

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

require 'mongo_doc/cursor'

module MongoDoc
  class Collection
    attr_accessor :_collection

    delegate :[], :clear, :count, :create_index, :db, :distinct, :drop, :drop_index, :drop_indexes, :group, :hint, :index_information, :map_reduce, :mapreduce, :name, :options, :pk_factory, :remove, :rename, :size, :to => :_collection

    def initialize(name)
      self._collection = self.class.mongo_collection(name)
    end

    def find(query = {}, options = {})
      cursor = wrapped_cursor(query, options)
      if block_given?
        yield cursor
        cursor.close
      else
        cursor
      end
    end

    def find_one(spec_or_object_id = nil, options = {})
      MongoDoc::BSON.decode(_collection.find_one(spec_or_object_id, options))
    end

    def insert(doc_or_docs, options = {})
      _collection.insert(doc_or_docs.to_bson, options)
    end
    alias :<< :insert

    def save(doc, options = {})
      _collection.save(doc.to_bson, options)
    end

    def update(spec, doc, options = {})
      _collection.update(spec, doc.to_bson, options)
      (last_error || {})['updatedExisting'] || false
    end

    protected

    def last_error
      MongoDoc::Connection.database.command({'getlasterror' => 1})
    end

    def wrapped_cursor(query = {}, options = {})
      MongoDoc::Cursor.new(self, _collection.find(query, options))
    end

    def self.mongo_collection(name)
      MongoDoc::Connection.database.collection(name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_doc-0.3.1 lib/mongo_doc/collection.rb
mongo_doc-0.3.0 lib/mongo_doc/collection.rb