Skip to content

Commit 63b4e31

Browse files
committed
RUBY-1021 Initialize options to Redacted and update uri parser
1 parent afbe202 commit 63b4e31

File tree

10 files changed

+45
-34
lines changed

10 files changed

+45
-34
lines changed

lib/mongo.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require 'forwardable'
1616
require 'bson'
1717
require 'openssl'
18+
require 'mongo/options'
1819
require 'mongo/loggable'
1920
require 'mongo/monitoring'
2021
require 'mongo/logger'
@@ -32,7 +33,6 @@
3233
require 'mongo/dbref'
3334
require 'mongo/grid'
3435
require 'mongo/index'
35-
require 'mongo/options'
3636
require 'mongo/protocol'
3737
require 'mongo/server'
3838
require 'mongo/server_selector'

lib/mongo/client.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def hash
153153
# logs at the default 250 characters.
154154
#
155155
# @since 2.0.0
156-
def initialize(addresses_or_uri, options = {})
156+
def initialize(addresses_or_uri, options = Options::Redacted.new)
157157
@monitoring = Monitoring.new(options)
158158
if addresses_or_uri.is_a?(::String)
159159
create_from_uri(addresses_or_uri, options)
@@ -215,7 +215,7 @@ def use(name)
215215
# @return [ Mongo::Client ] A new client instance.
216216
#
217217
# @since 2.0.0
218-
def with(new_options = {})
218+
def with(new_options = Options::Redacted.new)
219219
clone.tap do |client|
220220
opts = Options::Redacted.new(new_options) || Options::Redacted.new
221221
client.options.update(opts)
@@ -291,13 +291,13 @@ def list_databases
291291

292292
private
293293

294-
def create_from_addresses(addresses, opts = {})
294+
def create_from_addresses(addresses, opts = Options::Redacted.new)
295295
@options = Options::Redacted.new(Database::DEFAULT_OPTIONS.merge(opts)).freeze
296296
@cluster = Cluster.new(addresses, @monitoring, options)
297297
@database = Database.new(self, options[:database], options)
298298
end
299299

300-
def create_from_uri(connection_string, opts = {})
300+
def create_from_uri(connection_string, opts = Options::Redacted.new)
301301
uri = URI.new(connection_string, opts)
302302
@options = Options::Redacted.new(Database::DEFAULT_OPTIONS.merge(uri.client_options.merge(opts))).freeze
303303
@cluster = Cluster.new(uri.servers, @monitoring, options)

lib/mongo/cluster.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def add(host)
8888
# @param [ Hash ] options The options.
8989
#
9090
# @since 2.0.0
91-
def initialize(seeds, monitoring, options = {})
91+
def initialize(seeds, monitoring, options = Options::Redacted.new)
9292
@addresses = []
9393
@servers = []
9494
@monitoring = monitoring
@@ -125,7 +125,7 @@ def inspect
125125
#
126126
# @since 2.0.0
127127
def next_primary
128-
ServerSelector.get(Options::Redacted.new({ mode: :primary }.merge(options))).select_server(self)
128+
ServerSelector.get(ServerSelector::PRIMARY.merge(options)).select_server(self)
129129
end
130130

131131
# Elect a primary server from the description that has just changed to a

lib/mongo/collection/view/readable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def projection(document = nil)
298298
# @since 2.0.0
299299
def read(value = nil)
300300
return default_read if value.nil?
301-
selector = value.is_a?(Hash) ? ServerSelector.get(Options::Redacted.new(value.merge(client.options))) : value
301+
selector = value.is_a?(Hash) ? ServerSelector.get(client.options.merge(value)) : value
302302
configure(:read, selector)
303303
end
304304

lib/mongo/database.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Database
3636
# The default database options.
3737
#
3838
# @since 2.0.0
39-
DEFAULT_OPTIONS = { :database => ADMIN }.freeze
39+
DEFAULT_OPTIONS = Options::Redacted.new(:database => ADMIN).freeze
4040

4141
# Database name field constant.
4242
#
@@ -148,7 +148,7 @@ def collections
148148
#
149149
# @return [ Hash ] The result of the command execution.
150150
def command(operation, opts = {})
151-
preference = opts[:read] ? ServerSelector.get(Options::Redacted.new(opts[:read].merge(options))) : read_preference
151+
preference = opts[:read] ? ServerSelector.get(client.options.merge(opts[:read])) : read_preference
152152
server = preference.select_server(cluster)
153153
Operation::Command.new({
154154
:selector => operation,

lib/mongo/grid/fs_bucket.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Grid
1919
#
2020
# @since 2.0.0
2121
class FSBucket
22+
extend Forwardable
2223

2324
# The default root prefix.
2425
#
@@ -55,6 +56,10 @@ class FSBucket
5556
# @since 2.1.0
5657
attr_reader :options
5758

59+
# Get client from the database.
60+
def_delegators :database,
61+
:client
62+
5863
# Find files collection documents matching a given selector.
5964
#
6065
# @example Find files collection documents by a filename.
@@ -395,7 +400,7 @@ def upload_from_stream(filename, io, opts = {})
395400
# @since 2.1.0
396401
def read_preference
397402
@read_preference ||= @options[:read] ?
398-
ServerSelector.get(Options::Redacted.new((@options[:read] || {}).merge(database.options))) :
403+
ServerSelector.get(client.options.merge(@options[:read] || {})) :
399404
database.read_preference
400405
end
401406

lib/mongo/options/redacted.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Redacted < BSON::Document
3838
#
3939
# @since 2.1.0
4040
def inspect
41-
redacted_string(__method__)
41+
redacted_string(:inspect)
4242
end
4343

4444
# Get a string representation of the options.
@@ -47,7 +47,7 @@ def inspect
4747
#
4848
# @since 2.1.0
4949
def to_s
50-
redacted_string(__method__)
50+
redacted_string(:to_s)
5151
end
5252

5353
# Whether these options contain a given key.
@@ -63,6 +63,7 @@ def to_s
6363
def has_key?(key)
6464
super(convert_key(key))
6565
end
66+
alias_method :key?, :has_key?
6667

6768
private
6869

lib/mongo/server_selector.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ module ServerSelector
3838
# @since 2.0.0
3939
SERVER_SELECTION_TIMEOUT = 30.freeze
4040

41+
# Primary read preference.
42+
#
43+
# @since 2.1.0
44+
PRIMARY = Options::Redacted.new(mode: :primary)
45+
4146
# Hash lookup for the selector classes based off the symbols
4247
# provided in configuration.
4348
#

lib/mongo/uri.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def split_creds_hosts(string)
268268

269269
def parse_db_opts!(string)
270270
auth_db, d, uri_opts = string.partition(URI_OPTS_DELIM)
271-
@uri_options = parse_uri_options!(uri_opts)
271+
@uri_options = Options::Redacted.new(parse_uri_options!(uri_opts))
272272
@database = parse_database!(auth_db)
273273
end
274274

spec/mongo/uri_spec.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@
365365

366366
context 'numerical w value' do
367367
let(:options) { 'w=1' }
368-
let(:concern) { { :w => 1 } }
368+
let(:concern) { Mongo::Options::Redacted.new(:w => 1)}
369369

370370
it 'sets the write concern options' do
371371
expect(uri.uri_options[:write]).to eq(concern)
@@ -374,7 +374,7 @@
374374

375375
context 'w=majority' do
376376
let(:options) { 'w=majority' }
377-
let(:concern) { { :w => :majority } }
377+
let(:concern) { Mongo::Options::Redacted.new(:w => :majority) }
378378

379379
it 'sets the write concern options' do
380380
expect(uri.uri_options[:write]).to eq(concern)
@@ -383,7 +383,7 @@
383383

384384
context 'journal' do
385385
let(:options) { 'journal=true' }
386-
let(:concern) { { :j => true } }
386+
let(:concern) { Mongo::Options::Redacted.new(:j => true) }
387387

388388
it 'sets the write concern options' do
389389
expect(uri.uri_options[:write]).to eq(concern)
@@ -392,7 +392,7 @@
392392

393393
context 'fsync' do
394394
let(:options) { 'fsync=true' }
395-
let(:concern) { { :fsync => true } }
395+
let(:concern) { Mongo::Options::Redacted.new(:fsync => true) }
396396

397397
it 'sets the write concern options' do
398398
expect(uri.uri_options[:write]).to eq(concern)
@@ -402,7 +402,7 @@
402402
context 'wtimeoutMS' do
403403
let(:timeout) { 1234 }
404404
let(:options) { "w=2&wtimeoutMS=#{timeout}" }
405-
let(:concern) { { :w => 2, :timeout => timeout } }
405+
let(:concern) { Mongo::Options::Redacted.new(:w => 2, :timeout => timeout) }
406406

407407
it 'sets the write concern options' do
408408
expect(uri.uri_options[:write]).to eq(concern)
@@ -415,7 +415,7 @@
415415

416416
context 'primary' do
417417
let(:mode) { 'primary' }
418-
let(:read) { { :mode => :primary } }
418+
let(:read) { Mongo::Options::Redacted.new(:mode => :primary) }
419419

420420
it 'sets the read preference' do
421421
expect(uri.uri_options[:read]).to eq(read)
@@ -424,7 +424,7 @@
424424

425425
context 'primaryPreferred' do
426426
let(:mode) { 'primaryPreferred' }
427-
let(:read) { { :mode => :primary_preferred } }
427+
let(:read) { Mongo::Options::Redacted.new(:mode => :primary_preferred) }
428428

429429
it 'sets the read preference' do
430430
expect(uri.uri_options[:read]).to eq(read)
@@ -433,7 +433,7 @@
433433

434434
context 'secondary' do
435435
let(:mode) { 'secondary' }
436-
let(:read) { { :mode => :secondary } }
436+
let(:read) { Mongo::Options::Redacted.new(:mode => :secondary) }
437437

438438
it 'sets the read preference' do
439439
expect(uri.uri_options[:read]).to eq(read)
@@ -442,7 +442,7 @@
442442

443443
context 'secondaryPreferred' do
444444
let(:mode) { 'secondaryPreferred' }
445-
let(:read) { { :mode => :secondary_preferred } }
445+
let(:read) { Mongo::Options::Redacted.new(:mode => :secondary_preferred) }
446446

447447
it 'sets the read preference' do
448448
expect(uri.uri_options[:read]).to eq(read)
@@ -451,7 +451,7 @@
451451

452452
context 'nearest' do
453453
let(:mode) { 'nearest' }
454-
let(:read) { { :mode => :nearest } }
454+
let(:read) { Mongo::Options::Redacted.new(:mode => :nearest) }
455455

456456
it 'sets the read preference' do
457457
expect(uri.uri_options[:read]).to eq(read)
@@ -467,7 +467,7 @@
467467
end
468468

469469
let(:read) do
470-
{ :tag_sets => [{ :dc => 'ny', :rack => '1' }] }
470+
Mongo::Options::Redacted.new(:tag_sets => [{ 'dc' => 'ny', 'rack' => '1' }])
471471
end
472472

473473
it 'sets the read preference tag set' do
@@ -481,7 +481,7 @@
481481
end
482482

483483
let(:read) do
484-
{ :tag_sets => [{ :dc => 'ny' }, { :dc => 'bos' }] }
484+
Mongo::Options::Redacted.new(:tag_sets => [{ 'dc' => 'ny' }, { 'dc' => 'bos' }])
485485
end
486486

487487
it 'sets the read preference tag sets' do
@@ -536,7 +536,7 @@
536536

537537
context 'regular db' do
538538
let(:source) { 'foo' }
539-
let(:auth) { { :source => 'foo' } }
539+
let(:auth) { Mongo::Options::Redacted.new(:source => 'foo') }
540540

541541
it 'sets the auth source to the database' do
542542
expect(uri.uri_options[:auth]).to eq(auth)
@@ -545,7 +545,7 @@
545545

546546
context '$external' do
547547
let(:source) { '$external' }
548-
let(:auth) { { :source => :external } }
548+
let(:auth) { Mongo::Options::Redacted.new(:source => :external) }
549549

550550
it 'sets the auth source to :external' do
551551
expect(uri.uri_options[:auth]).to eq(auth)
@@ -562,7 +562,7 @@
562562

563563
let(:service_name) { 'foo' }
564564
let(:auth) do
565-
{ auth_mech_properties: { service_name: service_name } }
565+
Mongo::Options::Redacted.new(auth_mech_properties: { service_name: service_name })
566566
end
567567

568568
it 'sets the auth mechanism properties' do
@@ -577,7 +577,7 @@
577577

578578
let(:canonicalize_host_name) { 'true' }
579579
let(:auth) do
580-
{ auth_mech_properties: { canonicalize_host_name: true } }
580+
Mongo::Options::Redacted.new(auth_mech_properties: { canonicalize_host_name: true })
581581
end
582582

583583
it 'sets the auth mechanism properties' do
@@ -592,7 +592,7 @@
592592

593593
let(:service_realm) { 'dumdum' }
594594
let(:auth) do
595-
{ auth_mech_properties: { service_realm: service_realm } }
595+
Mongo::Options::Redacted.new(auth_mech_properties: { service_realm: service_realm })
596596
end
597597

598598
it 'sets the auth mechanism properties' do
@@ -612,9 +612,9 @@
612612
let(:service_realm) { 'dumdum' }
613613

614614
let(:auth) do
615-
{ auth_mech_properties: { service_name: service_name,
616-
canonicalize_host_name: true,
617-
service_realm: service_realm } }
615+
Mongo::Options::Redacted.new(auth_mech_properties: { service_name: service_name,
616+
canonicalize_host_name: true,
617+
service_realm: service_realm })
618618
end
619619

620620
it 'sets the auth mechanism properties' do

0 commit comments

Comments
 (0)