RUBY-203 Use Hash#fetch when possible

This commit is contained in:
Kyle Banker 2011-01-05 09:34:09 -05:00
parent 556d4aa271
commit fa583762e7
3 changed files with 6 additions and 6 deletions

View File

@ -77,7 +77,7 @@ module Mongo
@cache_time = @db.cache_time @cache_time = @db.cache_time
@cache = Hash.new(0) @cache = Hash.new(0)
unless pk_factory unless pk_factory
@safe = options.has_key?(:safe) ? options[:safe] : @db.safe @safe = options.fetch(:safe, @db.safe)
end end
@pk_factory = pk_factory || options[:pk] || BSON::ObjectId @pk_factory = pk_factory || options[:pk] || BSON::ObjectId
@hint = nil @hint = nil
@ -274,7 +274,7 @@ module Mongo
def insert(doc_or_docs, options={}) def insert(doc_or_docs, options={})
doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array) doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array)
doc_or_docs.collect! { |doc| @pk_factory.create_pk(doc) } 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 = insert_documents(doc_or_docs, @name, true, safe)
result.size > 1 ? result : result.first result.size > 1 ? result : result.first
end end
@ -310,7 +310,7 @@ module Mongo
# @core remove remove-instance_method # @core remove remove-instance_method
def remove(selector={}, opts={}) def remove(selector={}, opts={})
# Initial byte is 0. # 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") message = BSON::ByteBuffer.new("\0\0\0\0")
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}") BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
message.put_int(0) message.put_int(0)

View File

@ -46,7 +46,7 @@ module Mongo
@order = options[:order] @order = options[:order]
@hint = options[:hint] @hint = options[:hint]
@snapshot = options[:snapshot] @snapshot = options[:snapshot]
@timeout = options.has_key?(:timeout) ? options[:timeout] : true @timeout = options.fetch(:timeout, true)
@explain = options[:explain] @explain = options[:explain]
@socket = options[:socket] @socket = options[:socket]
@tailable = options[:tailable] || false @tailable = options[:tailable] || false

View File

@ -81,7 +81,7 @@ module Mongo
@connection = connection @connection = connection
@strict = options[:strict] @strict = options[:strict]
@pk_factory = options[:pk] @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. @cache_time = options[:cache_time] || 300 #5 minutes.
end end
@ -276,7 +276,7 @@ module Mongo
if strict? && !collection_names.include?(name) if strict? && !collection_names.include?(name)
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode." raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
else else
options[:safe] = options.has_key?(:safe) ? options[:safe] : @safe options[:safe] = options.fetch(:safe, @safe)
options.merge!(:pk => @pk_factory) unless options[:pk] options.merge!(:pk => @pk_factory) unless options[:pk]
Collection.new(self, name, options) Collection.new(self, name, options)
end end