Skip to content

Commit 080c2f9

Browse files
committed
Merge pull request #743 from estolfo/RUBY-1081-write-concern-opts
RUBY-1081 Represent write concern options keys as symbols and values as ints or strings
2 parents 66e8546 + 4b47a5c commit 080c2f9

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

lib/mongo/grid/stream/write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def with_write_concern(collection)
160160
collection.write_concern.options == write_concern.options)
161161
collection
162162
else
163-
collection.client.with(write: write_concern.options)[collection.name]
163+
collection.with(write: write_concern.options)
164164
end
165165
end
166166

lib/mongo/options/mapper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ def transform_keys_to_strings(options)
8181
end
8282
end
8383

84+
# Coverts all the keys of the options to symbols.
85+
#
86+
# @example Convert all option keys to symbols.
87+
# Mapper.transform({ 'name' => 1 })
88+
#
89+
# @param [ Hash ] options The options to transform.
90+
#
91+
# @return [ Hash ] The transformed options.
92+
#
93+
# @since 2.2.2
94+
def transform_keys_to_symbols(options)
95+
options.reduce({}) do |transformed, (key, value)|
96+
transformed[key.to_sym] = value
97+
transformed
98+
end
99+
end
100+
84101
# Coverts all the symbol values to strings.
85102
#
86103
# @example Convert all option symbol values to strings.

lib/mongo/write_concern/normalizable.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ module Normalizable
4444
#
4545
# @since 2.0.0
4646
def initialize(options)
47-
@options = options.freeze
47+
opts = Options::Mapper.transform_keys_to_symbols(options)
48+
@options = Options::Mapper.transform_values_to_strings(opts).freeze
4849
end
4950
end
5051
end

spec/mongo/client_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@
688688
end
689689

690690
it 'returns a acknowledged write concern' do
691-
expect(concern.get_last_error).to eq(:getlasterror => 1, 'j' => true)
691+
expect(concern.get_last_error).to eq(:getlasterror => 1, :j => true)
692692
end
693693
end
694694

spec/mongo/write_concern_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,20 @@
122122
expect(Mongo::WriteConcern.get(options).options).to eq(options)
123123
end
124124
end
125+
126+
context 'when w is a symbol' do
127+
128+
let(:options) do
129+
{ w: :majority, journal: true }
130+
end
131+
132+
it 'returns an Acknowledged write concern object' do
133+
expect(Mongo::WriteConcern.get(options)).to be_a(Mongo::WriteConcern::Acknowledged)
134+
end
135+
136+
it 'sets w to a string' do
137+
expect(Mongo::WriteConcern.get(options).options[:w]).to eq('majority')
138+
end
139+
end
125140
end
126141
end

spec/support/command_monitoring.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def data_matches?(actual, expected)
129129
#
130130
# @since 2.1.0
131131
def hash_matches?(actual, expected)
132+
if expected['writeConcern']
133+
expected['writeConcern'] = Options::Mapper.transform_keys_to_symbols(expected['writeConcern'])
134+
end
132135
if expected.keys.first == '$numberLong'
133136
converted = expected.values.first.to_i
134137
(actual == converted) || actual >= 0

0 commit comments

Comments
 (0)