RUBY-219 minor: use opts instead of options throughout

This commit is contained in:
Kyle Banker 2011-01-05 11:30:20 -05:00
parent 0b07efeb37
commit a197ea7852
7 changed files with 73 additions and 70 deletions

View File

@ -334,11 +334,11 @@ module BSON
def deserialize_regex_data(buf)
str = deserialize_cstr(buf)
options_str = deserialize_cstr(buf)
options = 0
options |= Regexp::IGNORECASE if options_str.include?('i')
options |= Regexp::MULTILINE if options_str.include?('m')
options |= Regexp::EXTENDED if options_str.include?('x')
Regexp.new(str, options)
opts = 0
opts |= Regexp::IGNORECASE if options_str.include?('i')
opts |= Regexp::MULTILINE if options_str.include?('m')
opts |= Regexp::EXTENDED if options_str.include?('x')
Regexp.new(str, opts)
end
def encoded_str(str)

View File

@ -27,10 +27,10 @@ module Mongo
# @param [String, Symbol] name the name of the collection.
# @param [DB] db a MongoDB database instance.
#
# @option options [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
# @option opts [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
# other than the default BSON::ObjectId.
#
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
# for insert, update, and remove method called on this Collection instance. If no
# value is provided, the default value set on this instance's DB will be used. This
# default can be overridden for any invocation of insert, update, or remove.
@ -44,7 +44,7 @@ module Mongo
# @return [Collection]
#
# @core collections constructor_details
def initialize(name, db, options={})
def initialize(name, db, opts={})
if db.is_a?(String) && name.is_a?(Mongo::DB)
warn "Warning: the order of parameters to initialize a collection have changed. " +
"Please specify the collection name first, followed by the db."
@ -69,10 +69,10 @@ module Mongo
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
end
if options.respond_to?(:create_pk) || !options.is_a?(Hash)
if opts.respond_to?(:create_pk) || !opts.is_a?(Hash)
warn "The method for specifying a primary key factory on a Collection has changed.\n" +
"Please specify it as an option (e.g., :pk => PkFactory)."
pk_factory = options
pk_factory = opts
else
pk_factory = nil
end
@ -83,9 +83,9 @@ module Mongo
@cache_time = @db.cache_time
@cache = Hash.new(0)
unless pk_factory
@safe = options.fetch(:safe, @db.safe)
@safe = opts.fetch(:safe, @db.safe)
end
@pk_factory = pk_factory || options[:pk] || BSON::ObjectId
@pk_factory = pk_factory || opts[:pk] || BSON::ObjectId
@hint = nil
end
@ -277,10 +277,10 @@ module Mongo
# @see DB#remove for options that can be passed to :safe.
#
# @core insert insert-instance_method
def insert(doc_or_docs, options={})
def insert(doc_or_docs, opts={})
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.fetch(:safe, @safe)
safe = opts.fetch(:safe, @safe)
result = insert_documents(doc_or_docs, @name, true, safe)
result.size > 1 ? result : result.first
end

View File

@ -55,16 +55,16 @@ module Mongo
# @param [String, Hash] host.
# @param [Integer] port specify a port number here if only one host is being specified.
#
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
# propogated to DB objects instantiated off of this Connection. This
# default can be overridden upon instantiation of any DB by explicity setting a :safe value
# on initialization.
# @option options [Boolean] :slave_ok (false) Must be set to +true+ when connecting
# @option opts [Boolean] :slave_ok (false) Must be set to +true+ when connecting
# to a single, slave node.
# @option options [Logger, #debug] :logger (nil) Logger instance to receive driver operation log.
# @option options [Integer] :pool_size (1) The maximum number of socket connections allowed per
# @option opts [Logger, #debug] :logger (nil) Logger instance to receive driver operation log.
# @option opts [Integer] :pool_size (1) The maximum number of socket connections allowed per
# connection pool. Note: this setting is relevant only for multi-threaded applications.
# @option options [Float] :timeout (5.0) When all of the connections a pool are checked out,
# @option opts [Float] :timeout (5.0) When all of the connections a pool are checked out,
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
# Note: this setting is relevant only for multi-threaded applications (which in Ruby are rare).
#
@ -86,16 +86,16 @@ module Mongo
# driver fails to connect to a replica set with that name.
#
# @core connections
def initialize(host=nil, port=nil, options={})
def initialize(host=nil, port=nil, opts={})
@host_to_try = format_pair(host, port)
# Host and port of current master.
@host = @port = nil
# slave_ok can be true only if one node is specified
@slave_ok = options[:slave_ok]
@slave_ok = opts[:slave_ok]
setup(options)
setup(opts)
end
# DEPRECATED
@ -109,9 +109,9 @@ module Mongo
# @param nodes [Array] An array of arrays, each of which specifies a host and port.
# @param opts [Hash] Any of the available options that can be passed to Connection.new.
#
# @option options [String] :rs_name (nil) The name of the replica set to connect to. An exception will be
# @option opts [String] :rs_name (nil) The name of the replica set to connect to. An exception will be
# raised if unable to connect to a replica set with this name.
# @option options [Boolean] :read_secondary (false) When true, this connection object will pick a random slave
# @option opts [Boolean] :read_secondary (false) When true, this connection object will pick a random slave
# to send reads to.
#
# @example
@ -263,12 +263,13 @@ module Mongo
# See DB#new for valid options hash parameters.
#
# @param [String] db_name a valid database name.
# @param [Hash] opts options to be passed to the DB constructor.
#
# @return [Mongo::DB]
#
# @core databases db-instance_method
def db(db_name, options={})
DB.new(db_name, self, options)
def db(db_name, opts={})
DB.new(db_name, self, opts)
end
# Shortcut for returning a database. Use DB#db to accept options.
@ -518,22 +519,22 @@ module Mongo
protected
# Generic initialization code.
def setup(options)
def setup(opts)
# Authentication objects
@auths = options.fetch(:auths, [])
@auths = opts.fetch(:auths, [])
# Lock for request ids.
@id_lock = Mutex.new
# Pool size and timeout.
@pool_size = options[:pool_size] || 1
@timeout = options[:timeout] || 5.0
@pool_size = opts[:pool_size] || 1
@timeout = opts[:timeout] || 5.0
# Mutex for synchronizing pool access
@connection_mutex = Mutex.new
# Global safe option. This is false by default.
@safe = options[:safe] || false
@safe = opts[:safe] || false
# Create a mutex when a new key, in this case a socket,
# is added to the hash.
@ -546,9 +547,9 @@ module Mongo
@primary = nil
@primary_pool = nil
@logger = options[:logger] || nil
@logger = opts[:logger] || nil
should_connect = options.fetch(:connect, true)
should_connect = opts.fetch(:connect, true)
connect if should_connect
end

View File

@ -33,26 +33,26 @@ module Mongo
# @return [Cursor]
#
# @core cursors constructor_details
def initialize(collection, options={})
def initialize(collection, opts={})
@db = collection.db
@collection = collection
@connection = @db.connection
@logger = @connection.logger
@selector = options[:selector] || {}
@fields = convert_fields_for_query(options[:fields])
@skip = options[:skip] || 0
@limit = options[:limit] || 0
@order = options[:order]
@hint = options[:hint]
@snapshot = options[:snapshot]
@timeout = options.fetch(:timeout, true)
@explain = options[:explain]
@socket = options[:socket]
@tailable = options[:tailable] || false
@selector = opts[:selector] || {}
@fields = convert_fields_for_query(opts[:fields])
@skip = opts[:skip] || 0
@limit = opts[:limit] || 0
@order = opts[:order]
@hint = opts[:hint]
@snapshot = opts[:snapshot]
@timeout = opts.fetch(:timeout, true)
@explain = opts[:explain]
@socket = opts[:socket]
@tailable = opts[:tailable] || false
@closed = false
@query_run = false
batch_size(options[:batch_size] || 0)
batch_size(opts[:batch_size] || 0)
@full_collection_name = "#{@collection.db.name}.#{@collection.name}"
@cache = []

View File

@ -60,29 +60,29 @@ module Mongo
# @param [Mongo::Connection] connection a connection object pointing to MongoDB. Note
# that databases are usually instantiated via the Connection class. See the examples below.
#
# @option options [Boolean] :strict (False) If true, collections must exist to be accessed and must
# @option opts [Boolean] :strict (False) If true, collections must exist to be accessed and must
# not exist to be created. See DB#collection and DB#create_collection.
#
# @option options [Object, #create_pk(doc)] :pk (Mongo::ObjectId) A primary key factory object,
# @option opts [Object, #create_pk(doc)] :pk (Mongo::ObjectId) A primary key factory object,
# which should take a hash and return a hash which merges the original hash with any primary key
# fields the factory wishes to inject. (NOTE: if the object already has a primary key,
# the factory should not inject a new key).
#
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
# propogated to Collection objects instantiated off of this DB. If no
# value is provided, the default value set on this instance's Connection object will be used. This
# default can be overridden upon instantiation of any collection by explicity setting a :safe value
# on initialization
# @option options [Integer] :cache_time (300) Set the time that all ensure_index calls should cache the command.
# @option opts [Integer] :cache_time (300) Set the time that all ensure_index calls should cache the command.
#
# @core databases constructor_details
def initialize(name, connection, options={})
def initialize(name, connection, opts={})
@name = Mongo::Support.validate_db_name(name)
@connection = connection
@strict = options[:strict]
@pk_factory = options[:pk]
@safe = options.fetch(:safe, @connection.safe)
@cache_time = options[:cache_time] || 300 #5 minutes.
@strict = opts[:strict]
@pk_factory = opts[:pk]
@safe = opts.fetch(:safe, @connection.safe)
@cache_time = opts[:cache_time] || 300 #5 minutes.
end
# Authenticate with the given username and password. Note that mongod
@ -233,20 +233,20 @@ module Mongo
#
# @param [String] name the name of the new collection.
#
# @option options [Boolean] :capped (False) created a capped collection.
# @option opts [Boolean] :capped (False) created a capped collection.
#
# @option options [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of
# @option opts [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of
# bytes for the capped collection. If +false+, specifies the number of bytes allocated
# for the initial extent of the collection.
#
# @option options [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records
# @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records
# in a capped collection.
#
# @raise [MongoDBError] raised under two conditions: either we're in +strict+ mode and the collection
# already exists or collection creation fails on the server.
#
# @return [Mongo::Collection]
def create_collection(name, options={})
def create_collection(name, opts={})
# Does the collection already exist?
if collection_names.include?(name)
if strict?
@ -259,7 +259,7 @@ module Mongo
# Create a new collection.
oh = BSON::OrderedHash.new
oh[:create] = name
doc = command(oh.merge(options || {}))
doc = command(oh.merge(opts || {}))
return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
raise MongoDBError, "Error creating collection: #{doc.inspect}"
end
@ -267,18 +267,18 @@ module Mongo
# Get a collection by name.
#
# @param [String] name the collection name.
# @param [Hash] options any valid options that can me passed to Collection#new.
# @param [Hash] opts any valid options that can me passed to Collection#new.
#
# @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode.
#
# @return [Mongo::Collection]
def collection(name, options={})
def collection(name, opts={})
if strict? && !collection_names.include?(name)
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
else
options[:safe] = options.fetch(:safe, @safe)
options.merge!(:pk => @pk_factory) unless options[:pk]
Collection.new(name, self, options)
opts[:safe] = opts.fetch(:safe, @safe)
opts.merge!(:pk => @pk_factory) unless opts[:pk]
Collection.new(name, self, opts)
end
end
alias_method :[], :collection

View File

@ -50,8 +50,10 @@ module Mongo
# @example Connect to a replica set and provide two seed nodes:
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001])
#
# @example Connect to a replica set providing two seed nodes and allowing reads from a
# secondary node:
# @example Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named 'prod':
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :rs_name => 'prod')
#
# @example Connect to a replica set providing two seed nodes and allowing reads from a secondary node:
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read_secondary => true)
#
# @see http://api.mongodb.org/ruby/current/file.REPLICA_SETS.html Replica sets in Ruby

View File

@ -22,14 +22,14 @@ module Mongo
# Create a new pool of connections.
#
def initialize(connection, host, port, options={})
def initialize(connection, host, port, opts={})
@connection = connection
@host, @port = host, port
# Pool size and timeout.
@size = options[:size] || 1
@timeout = options[:timeout] || 5.0
@size = opts[:size] || 1
@timeout = opts[:timeout] || 5.0
# Mutex for synchronizing pool access
@connection_mutex = Mutex.new