Sha256: 0a2332940aed60d2de8bec0aecd2ccc7c5ec61faee38f0c0fa5aeab725765e4a

Contents?: true

Size: 1.65 KB

Versions: 29

Compression:

Stored size: 1.65 KB

Contents

require 'mongo_doc/cursor'
require 'mongo_doc/criteria'

module MongoDoc
  class Collection
    attr_accessor :_collection

    include MongoDoc::Criteria

    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 collection
      self
    end

    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

29 entries across 29 versions & 2 rubygems

Version Path
mongo_doc-0.6.26 lib/mongo_doc/collection.rb
mongo_doc-0.6.25 lib/mongo_doc/collection.rb
mongo_doc-0.6.23 lib/mongo_doc/collection.rb
mongo_doc-0.6.22 lib/mongo_doc/collection.rb
mongo_doc-0.6.21 lib/mongo_doc/collection.rb
mongo_doc-0.6.20 lib/mongo_doc/collection.rb
mongo_doc-0.6.19 lib/mongo_doc/collection.rb
mongo_doc-0.6.18 lib/mongo_doc/collection.rb
mongo_doc-0.6.17 lib/mongo_doc/collection.rb
mongo_doc-0.6.16 lib/mongo_doc/collection.rb
mongo_doc-0.6.15 lib/mongo_doc/collection.rb
mongo_doc-0.6.14 lib/mongo_doc/collection.rb
mongo_doc-0.6.13 lib/mongo_doc/collection.rb
mongo_doc-0.6.12 lib/mongo_doc/collection.rb
mongo_doc-0.6.11 lib/mongo_doc/collection.rb
mongo_doc-0.6.10 lib/mongo_doc/collection.rb
mongo_doc-0.6.9 lib/mongo_doc/collection.rb
mongo_doc-0.6.8 lib/mongo_doc/collection.rb
mongo_doc-0.6.7 lib/mongo_doc/collection.rb
mongo_doc-0.6.6 lib/mongo_doc/collection.rb