lib/mongodoc/collection.rb in mongodoc-0.2.1 vs lib/mongodoc/collection.rb in mongodoc-0.2.2

- old
+ new

@@ -1,18 +1,19 @@ require 'mongodoc/cursor' module MongoDoc class Collection attr_accessor :_collection - delegate :[], :clear, :count, :create_index, :db, :drop, :drop_index, :drop_indexes, :group, :hint, :index_information, :name, :options, :remove, :rename, :size, :to => :_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 = MongoDoc::Cursor.new(_collection.find(query, options)) + cursor = wrapped_cursor(query, options) if block_given? yield cursor cursor.close else cursor @@ -32,14 +33,23 @@ _collection.save(doc.to_bson, options) end def update(spec, doc, options = {}) _collection.update(spec, doc.to_bson, options) - result = MongoDoc.database.command({'getlasterror' => 1}) - (result and result.has_key?('updatedExisting')) ? result['updatedExisting'] : false + (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.database.collection(name) + MongoDoc::Connection.database.collection(name) end end end