From fa583762e790bec8f460e0f53056c4eb51c29a31 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 5 Jan 2011 09:34:09 -0500 Subject: [PATCH] RUBY-203 Use Hash#fetch when possible --- lib/mongo/collection.rb | 6 +++--- lib/mongo/cursor.rb | 2 +- lib/mongo/db.rb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 259a486..5dd542c 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -77,7 +77,7 @@ module Mongo @cache_time = @db.cache_time @cache = Hash.new(0) unless pk_factory - @safe = options.has_key?(:safe) ? options[:safe] : @db.safe + @safe = options.fetch(:safe, @db.safe) end @pk_factory = pk_factory || options[:pk] || BSON::ObjectId @hint = nil @@ -274,7 +274,7 @@ module Mongo def insert(doc_or_docs, options={}) doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array) doc_or_docs.collect! { |doc| @pk_factory.create_pk(doc) } - safe = options.has_key?(:safe) ? options[:safe] : @safe + safe = options.fetch(:safe, @safe) result = insert_documents(doc_or_docs, @name, true, safe) result.size > 1 ? result : result.first end @@ -310,7 +310,7 @@ module Mongo # @core remove remove-instance_method def remove(selector={}, opts={}) # Initial byte is 0. - safe = opts.has_key?(:safe) ? opts[:safe] : @safe + safe = opts.fetch(:safe, @safe) message = BSON::ByteBuffer.new("\0\0\0\0") BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}") message.put_int(0) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 786cec9..620dab8 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -46,7 +46,7 @@ module Mongo @order = options[:order] @hint = options[:hint] @snapshot = options[:snapshot] - @timeout = options.has_key?(:timeout) ? options[:timeout] : true + @timeout = options.fetch(:timeout, true) @explain = options[:explain] @socket = options[:socket] @tailable = options[:tailable] || false diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 341b5e2..4e8bc4b 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -81,7 +81,7 @@ module Mongo @connection = connection @strict = options[:strict] @pk_factory = options[:pk] - @safe = options.has_key?(:safe) ? options[:safe] : @connection.safe + @safe = options.fetch(:safe, @connection.safe) @cache_time = options[:cache_time] || 300 #5 minutes. end @@ -276,7 +276,7 @@ module Mongo if strict? && !collection_names.include?(name) raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode." else - options[:safe] = options.has_key?(:safe) ? options[:safe] : @safe + options[:safe] = options.fetch(:safe, @safe) options.merge!(:pk => @pk_factory) unless options[:pk] Collection.new(self, name, options) end