Skip to content

Commit a197ea7

Browse files
committed
RUBY-219 minor: use opts instead of options throughout
1 parent 0b07efe commit a197ea7

File tree

7 files changed

+73
-70
lines changed

7 files changed

+73
-70
lines changed

lib/bson/bson_ruby.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ def deserialize_array_data(buf)
334334
def deserialize_regex_data(buf)
335335
str = deserialize_cstr(buf)
336336
options_str = deserialize_cstr(buf)
337-
options = 0
338-
options |= Regexp::IGNORECASE if options_str.include?('i')
339-
options |= Regexp::MULTILINE if options_str.include?('m')
340-
options |= Regexp::EXTENDED if options_str.include?('x')
341-
Regexp.new(str, options)
337+
opts = 0
338+
opts |= Regexp::IGNORECASE if options_str.include?('i')
339+
opts |= Regexp::MULTILINE if options_str.include?('m')
340+
opts |= Regexp::EXTENDED if options_str.include?('x')
341+
Regexp.new(str, opts)
342342
end
343343

344344
def encoded_str(str)

lib/mongo/collection.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class Collection
2727
# @param [String, Symbol] name the name of the collection.
2828
# @param [DB] db a MongoDB database instance.
2929
#
30-
# @option options [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
30+
# @option opts [:create_pk] :pk (BSON::ObjectId) A primary key factory to use
3131
# other than the default BSON::ObjectId.
3232
#
33-
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
33+
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
3434
# for insert, update, and remove method called on this Collection instance. If no
3535
# value is provided, the default value set on this instance's DB will be used. This
3636
# default can be overridden for any invocation of insert, update, or remove.
@@ -44,7 +44,7 @@ class Collection
4444
# @return [Collection]
4545
#
4646
# @core collections constructor_details
47-
def initialize(name, db, options={})
47+
def initialize(name, db, opts={})
4848
if db.is_a?(String) && name.is_a?(Mongo::DB)
4949
warn "Warning: the order of parameters to initialize a collection have changed. " +
5050
"Please specify the collection name first, followed by the db."
@@ -69,10 +69,10 @@ def initialize(name, db, options={})
6969
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
7070
end
7171

72-
if options.respond_to?(:create_pk) || !options.is_a?(Hash)
72+
if opts.respond_to?(:create_pk) || !opts.is_a?(Hash)
7373
warn "The method for specifying a primary key factory on a Collection has changed.\n" +
7474
"Please specify it as an option (e.g., :pk => PkFactory)."
75-
pk_factory = options
75+
pk_factory = opts
7676
else
7777
pk_factory = nil
7878
end
@@ -83,9 +83,9 @@ def initialize(name, db, options={})
8383
@cache_time = @db.cache_time
8484
@cache = Hash.new(0)
8585
unless pk_factory
86-
@safe = options.fetch(:safe, @db.safe)
86+
@safe = opts.fetch(:safe, @db.safe)
8787
end
88-
@pk_factory = pk_factory || options[:pk] || BSON::ObjectId
88+
@pk_factory = pk_factory || opts[:pk] || BSON::ObjectId
8989
@hint = nil
9090
end
9191

@@ -277,10 +277,10 @@ def save(doc, opts={})
277277
# @see DB#remove for options that can be passed to :safe.
278278
#
279279
# @core insert insert-instance_method
280-
def insert(doc_or_docs, options={})
280+
def insert(doc_or_docs, opts={})
281281
doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array)
282282
doc_or_docs.collect! { |doc| @pk_factory.create_pk(doc) }
283-
safe = options.fetch(:safe, @safe)
283+
safe = opts.fetch(:safe, @safe)
284284
result = insert_documents(doc_or_docs, @name, true, safe)
285285
result.size > 1 ? result : result.first
286286
end

lib/mongo/connection.rb

+20-19
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ class Connection
5555
# @param [String, Hash] host.
5656
# @param [Integer] port specify a port number here if only one host is being specified.
5757
#
58-
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
58+
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
5959
# propogated to DB objects instantiated off of this Connection. This
6060
# default can be overridden upon instantiation of any DB by explicity setting a :safe value
6161
# on initialization.
62-
# @option options [Boolean] :slave_ok (false) Must be set to +true+ when connecting
62+
# @option opts [Boolean] :slave_ok (false) Must be set to +true+ when connecting
6363
# to a single, slave node.
64-
# @option options [Logger, #debug] :logger (nil) Logger instance to receive driver operation log.
65-
# @option options [Integer] :pool_size (1) The maximum number of socket connections allowed per
64+
# @option opts [Logger, #debug] :logger (nil) Logger instance to receive driver operation log.
65+
# @option opts [Integer] :pool_size (1) The maximum number of socket connections allowed per
6666
# connection pool. Note: this setting is relevant only for multi-threaded applications.
67-
# @option options [Float] :timeout (5.0) When all of the connections a pool are checked out,
67+
# @option opts [Float] :timeout (5.0) When all of the connections a pool are checked out,
6868
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
6969
# Note: this setting is relevant only for multi-threaded applications (which in Ruby are rare).
7070
#
@@ -86,16 +86,16 @@ class Connection
8686
# driver fails to connect to a replica set with that name.
8787
#
8888
# @core connections
89-
def initialize(host=nil, port=nil, options={})
89+
def initialize(host=nil, port=nil, opts={})
9090
@host_to_try = format_pair(host, port)
9191

9292
# Host and port of current master.
9393
@host = @port = nil
9494

9595
# slave_ok can be true only if one node is specified
96-
@slave_ok = options[:slave_ok]
96+
@slave_ok = opts[:slave_ok]
9797

98-
setup(options)
98+
setup(opts)
9999
end
100100

101101
# DEPRECATED
@@ -109,9 +109,9 @@ def initialize(host=nil, port=nil, options={})
109109
# @param nodes [Array] An array of arrays, each of which specifies a host and port.
110110
# @param opts [Hash] Any of the available options that can be passed to Connection.new.
111111
#
112-
# @option options [String] :rs_name (nil) The name of the replica set to connect to. An exception will be
112+
# @option opts [String] :rs_name (nil) The name of the replica set to connect to. An exception will be
113113
# raised if unable to connect to a replica set with this name.
114-
# @option options [Boolean] :read_secondary (false) When true, this connection object will pick a random slave
114+
# @option opts [Boolean] :read_secondary (false) When true, this connection object will pick a random slave
115115
# to send reads to.
116116
#
117117
# @example
@@ -263,12 +263,13 @@ def database_names
263263
# See DB#new for valid options hash parameters.
264264
#
265265
# @param [String] db_name a valid database name.
266+
# @param [Hash] opts options to be passed to the DB constructor.
266267
#
267268
# @return [Mongo::DB]
268269
#
269270
# @core databases db-instance_method
270-
def db(db_name, options={})
271-
DB.new(db_name, self, options)
271+
def db(db_name, opts={})
272+
DB.new(db_name, self, opts)
272273
end
273274

274275
# Shortcut for returning a database. Use DB#db to accept options.
@@ -518,22 +519,22 @@ def checkin_writer(socket)
518519
protected
519520

520521
# Generic initialization code.
521-
def setup(options)
522+
def setup(opts)
522523
# Authentication objects
523-
@auths = options.fetch(:auths, [])
524+
@auths = opts.fetch(:auths, [])
524525

525526
# Lock for request ids.
526527
@id_lock = Mutex.new
527528

528529
# Pool size and timeout.
529-
@pool_size = options[:pool_size] || 1
530-
@timeout = options[:timeout] || 5.0
530+
@pool_size = opts[:pool_size] || 1
531+
@timeout = opts[:timeout] || 5.0
531532

532533
# Mutex for synchronizing pool access
533534
@connection_mutex = Mutex.new
534535

535536
# Global safe option. This is false by default.
536-
@safe = options[:safe] || false
537+
@safe = opts[:safe] || false
537538

538539
# Create a mutex when a new key, in this case a socket,
539540
# is added to the hash.
@@ -546,9 +547,9 @@ def setup(options)
546547
@primary = nil
547548
@primary_pool = nil
548549

549-
@logger = options[:logger] || nil
550+
@logger = opts[:logger] || nil
550551

551-
should_connect = options.fetch(:connect, true)
552+
should_connect = opts.fetch(:connect, true)
552553
connect if should_connect
553554
end
554555

lib/mongo/cursor.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ class Cursor
3333
# @return [Cursor]
3434
#
3535
# @core cursors constructor_details
36-
def initialize(collection, options={})
36+
def initialize(collection, opts={})
3737
@db = collection.db
3838
@collection = collection
3939
@connection = @db.connection
4040
@logger = @connection.logger
4141

42-
@selector = options[:selector] || {}
43-
@fields = convert_fields_for_query(options[:fields])
44-
@skip = options[:skip] || 0
45-
@limit = options[:limit] || 0
46-
@order = options[:order]
47-
@hint = options[:hint]
48-
@snapshot = options[:snapshot]
49-
@timeout = options.fetch(:timeout, true)
50-
@explain = options[:explain]
51-
@socket = options[:socket]
52-
@tailable = options[:tailable] || false
42+
@selector = opts[:selector] || {}
43+
@fields = convert_fields_for_query(opts[:fields])
44+
@skip = opts[:skip] || 0
45+
@limit = opts[:limit] || 0
46+
@order = opts[:order]
47+
@hint = opts[:hint]
48+
@snapshot = opts[:snapshot]
49+
@timeout = opts.fetch(:timeout, true)
50+
@explain = opts[:explain]
51+
@socket = opts[:socket]
52+
@tailable = opts[:tailable] || false
5353
@closed = false
5454
@query_run = false
55-
batch_size(options[:batch_size] || 0)
55+
batch_size(opts[:batch_size] || 0)
5656

5757
@full_collection_name = "#{@collection.db.name}.#{@collection.name}"
5858
@cache = []

lib/mongo/db.rb

+19-19
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,29 @@ def strict?; @strict; end
6060
# @param [Mongo::Connection] connection a connection object pointing to MongoDB. Note
6161
# that databases are usually instantiated via the Connection class. See the examples below.
6262
#
63-
# @option options [Boolean] :strict (False) If true, collections must exist to be accessed and must
63+
# @option opts [Boolean] :strict (False) If true, collections must exist to be accessed and must
6464
# not exist to be created. See DB#collection and DB#create_collection.
6565
#
66-
# @option options [Object, #create_pk(doc)] :pk (Mongo::ObjectId) A primary key factory object,
66+
# @option opts [Object, #create_pk(doc)] :pk (Mongo::ObjectId) A primary key factory object,
6767
# which should take a hash and return a hash which merges the original hash with any primary key
6868
# fields the factory wishes to inject. (NOTE: if the object already has a primary key,
6969
# the factory should not inject a new key).
7070
#
71-
# @option options [Boolean, Hash] :safe (false) Set the default safe-mode options
71+
# @option opts [Boolean, Hash] :safe (false) Set the default safe-mode options
7272
# propogated to Collection objects instantiated off of this DB. If no
7373
# value is provided, the default value set on this instance's Connection object will be used. This
7474
# default can be overridden upon instantiation of any collection by explicity setting a :safe value
7575
# on initialization
76-
# @option options [Integer] :cache_time (300) Set the time that all ensure_index calls should cache the command.
76+
# @option opts [Integer] :cache_time (300) Set the time that all ensure_index calls should cache the command.
7777
#
7878
# @core databases constructor_details
79-
def initialize(name, connection, options={})
79+
def initialize(name, connection, opts={})
8080
@name = Mongo::Support.validate_db_name(name)
8181
@connection = connection
82-
@strict = options[:strict]
83-
@pk_factory = options[:pk]
84-
@safe = options.fetch(:safe, @connection.safe)
85-
@cache_time = options[:cache_time] || 300 #5 minutes.
82+
@strict = opts[:strict]
83+
@pk_factory = opts[:pk]
84+
@safe = opts.fetch(:safe, @connection.safe)
85+
@cache_time = opts[:cache_time] || 300 #5 minutes.
8686
end
8787

8888
# Authenticate with the given username and password. Note that mongod
@@ -233,20 +233,20 @@ def collections_info(coll_name=nil)
233233
#
234234
# @param [String] name the name of the new collection.
235235
#
236-
# @option options [Boolean] :capped (False) created a capped collection.
236+
# @option opts [Boolean] :capped (False) created a capped collection.
237237
#
238-
# @option options [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of
238+
# @option opts [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of
239239
# bytes for the capped collection. If +false+, specifies the number of bytes allocated
240240
# for the initial extent of the collection.
241241
#
242-
# @option options [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records
242+
# @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records
243243
# in a capped collection.
244244
#
245245
# @raise [MongoDBError] raised under two conditions: either we're in +strict+ mode and the collection
246246
# already exists or collection creation fails on the server.
247247
#
248248
# @return [Mongo::Collection]
249-
def create_collection(name, options={})
249+
def create_collection(name, opts={})
250250
# Does the collection already exist?
251251
if collection_names.include?(name)
252252
if strict?
@@ -259,26 +259,26 @@ def create_collection(name, options={})
259259
# Create a new collection.
260260
oh = BSON::OrderedHash.new
261261
oh[:create] = name
262-
doc = command(oh.merge(options || {}))
262+
doc = command(oh.merge(opts || {}))
263263
return Collection.new(name, self, :pk => @pk_factory) if ok?(doc)
264264
raise MongoDBError, "Error creating collection: #{doc.inspect}"
265265
end
266266

267267
# Get a collection by name.
268268
#
269269
# @param [String] name the collection name.
270-
# @param [Hash] options any valid options that can me passed to Collection#new.
270+
# @param [Hash] opts any valid options that can me passed to Collection#new.
271271
#
272272
# @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode.
273273
#
274274
# @return [Mongo::Collection]
275-
def collection(name, options={})
275+
def collection(name, opts={})
276276
if strict? && !collection_names.include?(name)
277277
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
278278
else
279-
options[:safe] = options.fetch(:safe, @safe)
280-
options.merge!(:pk => @pk_factory) unless options[:pk]
281-
Collection.new(name, self, options)
279+
opts[:safe] = opts.fetch(:safe, @safe)
280+
opts.merge!(:pk => @pk_factory) unless opts[:pk]
281+
Collection.new(name, self, opts)
282282
end
283283
end
284284
alias_method :[], :collection

lib/mongo/repl_set_connection.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ class ReplSetConnection < Connection
5050
# @example Connect to a replica set and provide two seed nodes:
5151
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001])
5252
#
53-
# @example Connect to a replica set providing two seed nodes and allowing reads from a
54-
# secondary node:
53+
# @example Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named 'prod':
54+
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :rs_name => 'prod')
55+
#
56+
# @example Connect to a replica set providing two seed nodes and allowing reads from a secondary node:
5557
# ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read_secondary => true)
5658
#
5759
# @see http://api.mongodb.org/ruby/current/file.REPLICA_SETS.html Replica sets in Ruby

lib/mongo/util/pool.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class Pool
2222

2323
# Create a new pool of connections.
2424
#
25-
def initialize(connection, host, port, options={})
25+
def initialize(connection, host, port, opts={})
2626
@connection = connection
2727

2828
@host, @port = host, port
2929

3030
# Pool size and timeout.
31-
@size = options[:size] || 1
32-
@timeout = options[:timeout] || 5.0
31+
@size = opts[:size] || 1
32+
@timeout = opts[:timeout] || 5.0
3333

3434
# Mutex for synchronizing pool access
3535
@connection_mutex = Mutex.new

0 commit comments

Comments
 (0)