hashie 3.5.7 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +240 -164
  3. data/CONTRIBUTING.md +13 -6
  4. data/README.md +210 -29
  5. data/Rakefile +2 -2
  6. data/UPGRADING.md +83 -7
  7. data/hashie.gemspec +13 -7
  8. data/lib/hashie.rb +21 -19
  9. data/lib/hashie/clash.rb +12 -1
  10. data/lib/hashie/dash.rb +42 -21
  11. data/lib/hashie/extensions/active_support/core_ext/hash.rb +14 -0
  12. data/lib/hashie/extensions/coercion.rb +26 -19
  13. data/lib/hashie/extensions/dash/indifferent_access.rb +20 -1
  14. data/lib/hashie/extensions/dash/property_translation.rb +54 -28
  15. data/lib/hashie/extensions/deep_fetch.rb +5 -3
  16. data/lib/hashie/extensions/deep_find.rb +14 -5
  17. data/lib/hashie/extensions/deep_locate.rb +22 -8
  18. data/lib/hashie/extensions/deep_merge.rb +26 -10
  19. data/lib/hashie/extensions/indifferent_access.rb +8 -8
  20. data/lib/hashie/extensions/key_conflict_warning.rb +55 -0
  21. data/lib/hashie/extensions/mash/define_accessors.rb +90 -0
  22. data/lib/hashie/extensions/mash/keep_original_keys.rb +4 -5
  23. data/lib/hashie/extensions/mash/permissive_respond_to.rb +61 -0
  24. data/lib/hashie/extensions/mash/safe_assignment.rb +3 -1
  25. data/lib/hashie/extensions/mash/symbolize_keys.rb +1 -1
  26. data/lib/hashie/extensions/method_access.rb +47 -14
  27. data/lib/hashie/extensions/parsers/yaml_erb_parser.rb +28 -4
  28. data/lib/hashie/extensions/ruby_version_check.rb +5 -1
  29. data/lib/hashie/extensions/strict_key_access.rb +16 -13
  30. data/lib/hashie/extensions/stringify_keys.rb +1 -1
  31. data/lib/hashie/extensions/symbolize_keys.rb +1 -1
  32. data/lib/hashie/hash.rb +18 -11
  33. data/lib/hashie/mash.rb +131 -70
  34. data/lib/hashie/railtie.rb +7 -0
  35. data/lib/hashie/rash.rb +6 -6
  36. data/lib/hashie/utils.rb +28 -0
  37. data/lib/hashie/version.rb +1 -1
  38. metadata +19 -128
  39. data/spec/hashie/array_spec.rb +0 -29
  40. data/spec/hashie/clash_spec.rb +0 -70
  41. data/spec/hashie/dash_spec.rb +0 -573
  42. data/spec/hashie/extensions/autoload_spec.rb +0 -24
  43. data/spec/hashie/extensions/coercion_spec.rb +0 -631
  44. data/spec/hashie/extensions/dash/coercion_spec.rb +0 -13
  45. data/spec/hashie/extensions/dash/indifferent_access_spec.rb +0 -84
  46. data/spec/hashie/extensions/deep_fetch_spec.rb +0 -97
  47. data/spec/hashie/extensions/deep_find_spec.rb +0 -138
  48. data/spec/hashie/extensions/deep_locate_spec.rb +0 -137
  49. data/spec/hashie/extensions/deep_merge_spec.rb +0 -70
  50. data/spec/hashie/extensions/ignore_undeclared_spec.rb +0 -47
  51. data/spec/hashie/extensions/indifferent_access_spec.rb +0 -282
  52. data/spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb +0 -208
  53. data/spec/hashie/extensions/key_conversion_spec.rb +0 -12
  54. data/spec/hashie/extensions/mash/keep_original_keys_spec.rb +0 -46
  55. data/spec/hashie/extensions/mash/safe_assignment_spec.rb +0 -50
  56. data/spec/hashie/extensions/mash/symbolize_keys_spec.rb +0 -39
  57. data/spec/hashie/extensions/merge_initializer_spec.rb +0 -23
  58. data/spec/hashie/extensions/method_access_spec.rb +0 -188
  59. data/spec/hashie/extensions/strict_key_access_spec.rb +0 -110
  60. data/spec/hashie/extensions/stringify_keys_spec.rb +0 -124
  61. data/spec/hashie/extensions/symbolize_keys_spec.rb +0 -129
  62. data/spec/hashie/hash_spec.rb +0 -84
  63. data/spec/hashie/mash_spec.rb +0 -763
  64. data/spec/hashie/parsers/yaml_erb_parser_spec.rb +0 -46
  65. data/spec/hashie/rash_spec.rb +0 -83
  66. data/spec/hashie/trash_spec.rb +0 -268
  67. data/spec/hashie/utils_spec.rb +0 -25
  68. data/spec/hashie/version_spec.rb +0 -7
  69. data/spec/hashie_spec.rb +0 -13
  70. data/spec/integration/omniauth-oauth2/app.rb +0 -53
  71. data/spec/integration/omniauth-oauth2/integration_spec.rb +0 -26
  72. data/spec/integration/omniauth-oauth2/some_site.rb +0 -38
  73. data/spec/integration/omniauth/app.rb +0 -11
  74. data/spec/integration/omniauth/integration_spec.rb +0 -38
  75. data/spec/integration/rails-without-dependency/integration_spec.rb +0 -15
  76. data/spec/integration/rails/app.rb +0 -48
  77. data/spec/integration/rails/integration_spec.rb +0 -26
  78. data/spec/spec_helper.rb +0 -23
  79. data/spec/support/integration_specs.rb +0 -36
  80. data/spec/support/logger.rb +0 -24
  81. data/spec/support/module_context.rb +0 -11
  82. data/spec/support/ruby_version_check.rb +0 -6
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hashie::Extensions::DeepMerge do
4
- class DeepMergeHash < Hash
5
- include Hashie::Extensions::DeepMerge
6
- end
7
-
8
- subject { DeepMergeHash }
9
-
10
- it 'should return initial hash for arguments that are not hash' do
11
- hash = subject.new.merge(a: 'a')
12
- expect(hash.deep_merge('abc')).to eq(hash)
13
- end
14
-
15
- context 'without &block' do
16
- let(:h1) { subject.new.merge(a: 'a', a1: 42, b: 'b', c: { c1: 'c1', c2: { a: 'b' }, c3: { d1: 'd1' } }) }
17
- let(:h2) { { a: 1, a1: 1, c: { c1: 2, c2: 'c2', c3: { d2: 'd2' } }, e: { e1: 1 } } }
18
- let(:expected_hash) { { a: 1, a1: 1, b: 'b', c: { c1: 2, c2: 'c2', c3: { d1: 'd1', d2: 'd2' } }, e: { e1: 1 } } }
19
-
20
- it 'deep merges two hashes' do
21
- expect(h1.deep_merge(h2)).to eq expected_hash
22
- end
23
-
24
- it 'deep merges another hash in place via bang method' do
25
- h1.deep_merge!(h2)
26
- expect(h1).to eq expected_hash
27
- end
28
-
29
- it 'merges new nested hash entries by value, not by reference' do
30
- h1.deep_merge!(h2)
31
- expect { h1[:e][:e1] = 'changed' }.not_to change { h2[:e][:e1] }
32
- end
33
- end
34
-
35
- context 'with &block' do
36
- let(:h1) { subject.new.merge(a: 100, b: 200, c: { c1: 100 }) }
37
- let(:h2) { { b: 250, c: { c1: 200 } } }
38
- let(:expected_hash) { { a: 100, b: 450, c: { c1: 300 } } }
39
- let(:block) { proc { |_, this_val, other_val| this_val + other_val } }
40
-
41
- it 'deep merges two hashes' do
42
- expect(h1.deep_merge(h2, &block)).to eq expected_hash
43
- end
44
-
45
- it 'deep merges another hash in place via bang method' do
46
- h1.deep_merge!(h2, &block)
47
- expect(h1).to eq expected_hash
48
- end
49
- end
50
-
51
- context 'from extended object' do
52
- subject { Hash }
53
- let(:h1) { subject.new.merge(a: 100, c: { c1: 100 }).extend(Hashie::Extensions::DeepMerge) }
54
- let(:h2) { { b: 250, c: { c1: 200 } } }
55
- let(:expected_hash) { { a: 100, b: 250, c: { c1: 200 } } }
56
-
57
- it 'does not raise error' do
58
- expect { h1.deep_merge(h2) } .not_to raise_error
59
- end
60
-
61
- it 'deep merges two hashes' do
62
- expect(h1.deep_merge(h2)).to eq expected_hash
63
- end
64
-
65
- it 'deep merges another hash in place via bang method' do
66
- h1.deep_merge!(h2)
67
- expect(h1).to eq expected_hash
68
- end
69
- end
70
- end
@@ -1,47 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hashie::Extensions::IgnoreUndeclared do
4
- context 'included in Trash' do
5
- class ForgivingTrash < Hashie::Trash
6
- include Hashie::Extensions::IgnoreUndeclared
7
- property :city
8
- property :state, from: :provence
9
- property :str_state, from: 'str_provence'
10
- end
11
-
12
- subject { ForgivingTrash }
13
-
14
- it 'silently ignores undeclared properties on initialization' do
15
- expect { subject.new(city: 'Toronto', provence: 'ON', country: 'Canada') }.to_not raise_error
16
- end
17
-
18
- it 'works with translated properties (with symbol keys)' do
19
- expect(subject.new(provence: 'Ontario').state).to eq('Ontario')
20
- end
21
-
22
- it 'works with translated properties (with string keys)' do
23
- expect(subject.new('str_provence' => 'Ontario').str_state).to eq('Ontario')
24
- end
25
-
26
- it 'requires properties to be declared on assignment' do
27
- hash = subject.new(city: 'Toronto')
28
- expect { hash.country = 'Canada' }.to raise_error(NoMethodError)
29
- end
30
- end
31
-
32
- context 'combined with DeepMerge' do
33
- class ForgivingTrashWithMerge < Hashie::Trash
34
- include Hashie::Extensions::DeepMerge
35
- include Hashie::Extensions::IgnoreUndeclared
36
- property :some_key
37
- end
38
-
39
- it 'deep merges' do
40
- class ForgivingTrashWithMergeAndProperty < ForgivingTrashWithMerge
41
- property :some_other_key
42
- end
43
- hash = ForgivingTrashWithMergeAndProperty.new(some_ignored_key: 17, some_key: 12)
44
- expect(hash.deep_merge(some_other_key: 55, some_ignored_key: 18)).to eq(some_key: 12, some_other_key: 55)
45
- end
46
- end
47
- end
@@ -1,282 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hashie::Extensions::IndifferentAccess do
4
- class IndifferentHashWithMergeInitializer < Hash
5
- include Hashie::Extensions::MergeInitializer
6
- include Hashie::Extensions::IndifferentAccess
7
-
8
- class << self
9
- alias_method :build, :new
10
- end
11
- end
12
-
13
- class IndifferentHashWithArrayInitializer < Hash
14
- include Hashie::Extensions::IndifferentAccess
15
-
16
- class << self
17
- alias_method :build, :[]
18
- end
19
- end
20
-
21
- class IndifferentHashWithTryConvertInitializer < Hash
22
- include Hashie::Extensions::IndifferentAccess
23
-
24
- class << self
25
- alias_method :build, :try_convert
26
- end
27
- end
28
-
29
- class IndifferentHashWithDash < Hashie::Dash
30
- include Hashie::Extensions::IndifferentAccess
31
- property :foo
32
- end
33
-
34
- class IndifferentHashWithIgnoreUndeclaredAndPropertyTranslation < Hashie::Dash
35
- include Hashie::Extensions::IgnoreUndeclared
36
- include Hashie::Extensions::Dash::PropertyTranslation
37
- include Hashie::Extensions::IndifferentAccess
38
- property :foo, from: :bar
39
- end
40
-
41
- describe '#merge' do
42
- it 'indifferently merges in a hash' do
43
- indifferent_hash = Class.new(::Hash) do
44
- include Hashie::Extensions::IndifferentAccess
45
- end.new
46
-
47
- merged_hash = indifferent_hash.merge(:cat => 'meow')
48
-
49
- expect(merged_hash[:cat]).to eq('meow')
50
- expect(merged_hash['cat']).to eq('meow')
51
- end
52
- end
53
-
54
- describe '#merge!' do
55
- it 'indifferently merges in a hash' do
56
- indifferent_hash = Class.new(::Hash) do
57
- include Hashie::Extensions::IndifferentAccess
58
- end.new
59
-
60
- indifferent_hash.merge!(:cat => 'meow')
61
-
62
- expect(indifferent_hash[:cat]).to eq('meow')
63
- expect(indifferent_hash['cat']).to eq('meow')
64
- end
65
- end
66
-
67
- describe 'when included in dash' do
68
- let(:params) { { foo: 'bar' } }
69
- subject { IndifferentHashWithDash.new(params) }
70
-
71
- it 'initialize with a symbol' do
72
- expect(subject.foo).to eq params[:foo]
73
- end
74
- end
75
-
76
- describe 'when translating properties and ignoring undeclared' do
77
- let(:value) { 'baz' }
78
-
79
- subject { IndifferentHashWithIgnoreUndeclaredAndPropertyTranslation.new(params) }
80
-
81
- context 'and the hash keys are strings' do
82
- let(:params) { { 'bar' => value } }
83
-
84
- it 'sets the property' do
85
- expect(subject[:foo]).to eq value
86
- end
87
- end
88
-
89
- context 'and the hash keys are symbols' do
90
- let(:params) { { bar: 'baz' } }
91
-
92
- it 'sets the property' do
93
- expect(subject[:foo]).to eq value
94
- end
95
- end
96
-
97
- context 'and there are undeclared keys' do
98
- let(:params) { { 'bar' => 'baz', 'fail' => false } }
99
-
100
- it 'sets the property' do
101
- expect(subject[:foo]).to eq value
102
- end
103
- end
104
- end
105
-
106
- shared_examples_for 'hash with indifferent access' do
107
- it 'is able to access via string or symbol' do
108
- h = subject.build(abc: 123)
109
- expect(h[:abc]).to eq 123
110
- expect(h['abc']).to eq 123
111
- end
112
-
113
- describe '#values_at' do
114
- it 'indifferently finds values' do
115
- h = subject.build(:foo => 'bar', 'baz' => 'qux')
116
- expect(h.values_at('foo', :baz)).to eq %w(bar qux)
117
- end
118
-
119
- it 'returns the same instance of the hash that was set' do
120
- hash = {}
121
- h = subject.build(foo: hash)
122
- expect(h.values_at(:foo)[0]).to be(hash)
123
- end
124
-
125
- it 'returns the same instance of the array that was set' do
126
- array = []
127
- h = subject.build(foo: array)
128
- expect(h.values_at(:foo)[0]).to be(array)
129
- end
130
-
131
- it 'returns the same instance of the string that was set' do
132
- str = 'my string'
133
- h = subject.build(foo: str)
134
- expect(h.values_at(:foo)[0]).to be(str)
135
- end
136
-
137
- it 'returns the same instance of the object that was set' do
138
- object = Object.new
139
- h = subject.build(foo: object)
140
- expect(h.values_at(:foo)[0]).to be(object)
141
- end
142
- end
143
-
144
- describe '#fetch' do
145
- it 'works like normal fetch, but indifferent' do
146
- h = subject.build(foo: 'bar')
147
- expect(h.fetch(:foo)).to eq h.fetch('foo')
148
- expect(h.fetch(:foo)).to eq 'bar'
149
- end
150
-
151
- it 'returns the same instance of the hash that was set' do
152
- hash = {}
153
- h = subject.build(foo: hash)
154
- expect(h.fetch(:foo)).to be(hash)
155
- end
156
-
157
- it 'returns the same instance of the array that was set' do
158
- array = []
159
- h = subject.build(foo: array)
160
- expect(h.fetch(:foo)).to be(array)
161
- end
162
-
163
- it 'returns the same instance of the string that was set' do
164
- str = 'my string'
165
- h = subject.build(foo: str)
166
- expect(h.fetch(:foo)).to be(str)
167
- end
168
-
169
- it 'returns the same instance of the object that was set' do
170
- object = Object.new
171
- h = subject.build(foo: object)
172
- expect(h.fetch(:foo)).to be(object)
173
- end
174
-
175
- it 'yields with key name if key does not exists' do
176
- h = subject.build(a: 0)
177
- expect(h.fetch(:foo) { |key| ['default for', key] }).to eq ['default for', 'foo']
178
- end
179
- end
180
-
181
- describe '#delete' do
182
- it 'deletes indifferently' do
183
- h = subject.build(:foo => 'bar', 'baz' => 'qux')
184
- h.delete('foo')
185
- h.delete(:baz)
186
- expect(h).to be_empty
187
- end
188
- end
189
-
190
- describe '#key?' do
191
- let(:h) { subject.build(foo: 'bar') }
192
-
193
- it 'finds it indifferently' do
194
- expect(h).to be_key(:foo)
195
- expect(h).to be_key('foo')
196
- end
197
-
198
- %w(include? member? has_key?).each do |key_alias|
199
- it "is aliased as #{key_alias}" do
200
- expect(h.send(key_alias.to_sym, :foo)).to be(true)
201
- expect(h.send(key_alias.to_sym, 'foo')).to be(true)
202
- end
203
- end
204
- end
205
-
206
- describe '#update' do
207
- let(:h) { subject.build(foo: 'bar') }
208
-
209
- it 'allows keys to be indifferent still' do
210
- h.update(baz: 'qux')
211
- expect(h['foo']).to eq 'bar'
212
- expect(h['baz']).to eq 'qux'
213
- end
214
-
215
- it 'recursively injects indifference into sub-hashes' do
216
- h.update(baz: { qux: 'abc' })
217
- expect(h['baz']['qux']).to eq 'abc'
218
- end
219
-
220
- it 'does not change the ancestors of the injected object class' do
221
- h.update(baz: { qux: 'abc' })
222
- expect({}).not_to be_respond_to(:indifferent_access?)
223
- end
224
- end
225
-
226
- describe '#replace' do
227
- let(:h) { subject.build(foo: 'bar').replace(bar: 'baz', hi: 'bye') }
228
-
229
- it 'returns self' do
230
- expect(h).to be_a(subject)
231
- end
232
-
233
- it 'removes old keys' do
234
- [:foo, 'foo'].each do |k|
235
- expect(h[k]).to be_nil
236
- expect(h.key?(k)).to be_falsy
237
- end
238
- end
239
-
240
- it 'creates new keys with indifferent access' do
241
- [:bar, 'bar', :hi, 'hi'].each { |k| expect(h.key?(k)).to be_truthy }
242
- expect(h[:bar]).to eq 'baz'
243
- expect(h['bar']).to eq 'baz'
244
- expect(h[:hi]).to eq 'bye'
245
- expect(h['hi']).to eq 'bye'
246
- end
247
- end
248
-
249
- describe '#try_convert' do
250
- describe 'with conversion' do
251
- let(:h) { subject.try_convert(foo: 'bar') }
252
-
253
- it 'is a subject' do
254
- expect(h).to be_a(subject)
255
- end
256
- end
257
-
258
- describe 'without conversion' do
259
- let(:h) { subject.try_convert('{ :foo => bar }') }
260
-
261
- it 'is nil' do
262
- expect(h).to be_nil
263
- end
264
- end
265
- end
266
- end
267
-
268
- describe 'with merge initializer' do
269
- subject { IndifferentHashWithMergeInitializer }
270
- it_should_behave_like 'hash with indifferent access'
271
- end
272
-
273
- describe 'with array initializer' do
274
- subject { IndifferentHashWithArrayInitializer }
275
- it_should_behave_like 'hash with indifferent access'
276
- end
277
-
278
- describe 'with try convert initializer' do
279
- subject { IndifferentHashWithTryConvertInitializer }
280
- it_should_behave_like 'hash with indifferent access'
281
- end
282
- end
@@ -1,208 +0,0 @@
1
- # This set of tests verifies that Hashie::Extensions::IndifferentAccess works with
2
- # ActiveSupport HashWithIndifferentAccess hashes. See #164 and #166 for details.
3
-
4
- require 'active_support/hash_with_indifferent_access'
5
- require 'active_support/core_ext/hash'
6
- require 'spec_helper'
7
-
8
- describe Hashie::Extensions::IndifferentAccess do
9
- class IndifferentHashWithMergeInitializer < Hash
10
- include Hashie::Extensions::MergeInitializer
11
- include Hashie::Extensions::IndifferentAccess
12
-
13
- class << self
14
- alias_method :build, :new
15
- end
16
- end
17
-
18
- class IndifferentHashWithArrayInitializer < Hash
19
- include Hashie::Extensions::IndifferentAccess
20
-
21
- class << self
22
- alias_method :build, :[]
23
- end
24
- end
25
-
26
- class IndifferentHashWithTryConvertInitializer < Hash
27
- include Hashie::Extensions::IndifferentAccess
28
-
29
- class << self
30
- alias_method :build, :try_convert
31
- end
32
- end
33
-
34
- class CoercableHash < Hash
35
- include Hashie::Extensions::Coercion
36
- include Hashie::Extensions::MergeInitializer
37
- end
38
-
39
- class MashWithIndifferentAccess < Hashie::Mash
40
- include Hashie::Extensions::IndifferentAccess
41
- end
42
-
43
- shared_examples_for 'hash with indifferent access' do
44
- it 'is able to access via string or symbol' do
45
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(abc: 123)
46
- h = subject.build(indifferent_hash)
47
- expect(h[:abc]).to eq 123
48
- expect(h['abc']).to eq 123
49
- end
50
-
51
- describe '#values_at' do
52
- it 'indifferently finds values' do
53
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(
54
- :foo => 'bar', 'baz' => 'qux'
55
- )
56
- h = subject.build(indifferent_hash)
57
- expect(h.values_at('foo', :baz)).to eq %w(bar qux)
58
- end
59
- end
60
-
61
- describe '#fetch' do
62
- it 'works like normal fetch, but indifferent' do
63
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
64
- h = subject.build(indifferent_hash)
65
- expect(h.fetch(:foo)).to eq h.fetch('foo')
66
- expect(h.fetch(:foo)).to eq 'bar'
67
- end
68
- end
69
-
70
- describe '#delete' do
71
- it 'deletes indifferently' do
72
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(
73
- :foo => 'bar',
74
- 'baz' => 'qux'
75
- )
76
- h = subject.build(indifferent_hash)
77
- h.delete('foo')
78
- h.delete(:baz)
79
- expect(h).to be_empty
80
- end
81
- end
82
-
83
- describe '#key?' do
84
- let(:h) do
85
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
86
- subject.build(indifferent_hash)
87
- end
88
-
89
- it 'finds it indifferently' do
90
- expect(h).to be_key(:foo)
91
- expect(h).to be_key('foo')
92
- end
93
-
94
- %w(include? member? has_key?).each do |key_alias|
95
- it "is aliased as #{key_alias}" do
96
- expect(h.send(key_alias.to_sym, :foo)).to be(true)
97
- expect(h.send(key_alias.to_sym, 'foo')).to be(true)
98
- end
99
- end
100
- end
101
-
102
- describe '#update' do
103
- let(:h) do
104
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
105
- subject.build(indifferent_hash)
106
- end
107
-
108
- it 'allows keys to be indifferent still' do
109
- h.update(baz: 'qux')
110
- expect(h['foo']).to eq 'bar'
111
- expect(h['baz']).to eq 'qux'
112
- end
113
-
114
- it 'recursively injects indifference into sub-hashes' do
115
- h.update(baz: { qux: 'abc' })
116
- expect(h['baz']['qux']).to eq 'abc'
117
- end
118
-
119
- it 'does not change the ancestors of the injected object class' do
120
- h.update(baz: { qux: 'abc' })
121
- expect({}).not_to be_respond_to(:indifferent_access?)
122
- end
123
- end
124
-
125
- describe '#replace' do
126
- let(:h) do
127
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
128
- subject.build(indifferent_hash).replace(bar: 'baz', hi: 'bye')
129
- end
130
-
131
- it 'returns self' do
132
- expect(h).to be_a(subject)
133
- end
134
-
135
- it 'removes old keys' do
136
- [:foo, 'foo'].each do |k|
137
- expect(h[k]).to be_nil
138
- expect(h.key?(k)).to be_falsy
139
- end
140
- end
141
-
142
- it 'creates new keys with indifferent access' do
143
- [:bar, 'bar', :hi, 'hi'].each { |k| expect(h.key?(k)).to be_truthy }
144
- expect(h[:bar]).to eq 'baz'
145
- expect(h['bar']).to eq 'baz'
146
- expect(h[:hi]).to eq 'bye'
147
- expect(h['hi']).to eq 'bye'
148
- end
149
- end
150
-
151
- describe '#try_convert' do
152
- describe 'with conversion' do
153
- let(:h) do
154
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
155
- subject.try_convert(indifferent_hash)
156
- end
157
-
158
- it 'is a subject' do
159
- expect(h).to be_a(subject)
160
- end
161
- end
162
-
163
- describe 'without conversion' do
164
- let(:h) { subject.try_convert('{ :foo => bar }') }
165
-
166
- it 'is nil' do
167
- expect(h).to be_nil
168
- end
169
- end
170
- end
171
- end
172
-
173
- describe 'with merge initializer' do
174
- subject { IndifferentHashWithMergeInitializer }
175
- it_should_behave_like 'hash with indifferent access'
176
- end
177
-
178
- describe 'with array initializer' do
179
- subject { IndifferentHashWithArrayInitializer }
180
- it_should_behave_like 'hash with indifferent access'
181
- end
182
-
183
- describe 'with try convert initializer' do
184
- subject { IndifferentHashWithTryConvertInitializer }
185
- it_should_behave_like 'hash with indifferent access'
186
- end
187
-
188
- describe 'with coercion' do
189
- subject { CoercableHash }
190
-
191
- let(:instance) { subject.new }
192
-
193
- it 'supports coercion for ActiveSupport::HashWithIndifferentAccess' do
194
- subject.coerce_key :foo, ActiveSupport::HashWithIndifferentAccess.new(Coercable => Coercable)
195
- instance[:foo] = { 'bar_key' => 'bar_value', 'bar2_key' => 'bar2_value' }
196
- expect(instance[:foo].keys).to all(be_coerced)
197
- expect(instance[:foo].values).to all(be_coerced)
198
- expect(instance[:foo]).to be_a(ActiveSupport::HashWithIndifferentAccess)
199
- end
200
- end
201
-
202
- describe 'Mash with indifferent access' do
203
- it 'is able to be created for a deep nested HashWithIndifferentAccess' do
204
- indifferent_hash = ActiveSupport::HashWithIndifferentAccess.new(abc: { def: 123 })
205
- MashWithIndifferentAccess.new(indifferent_hash)
206
- end
207
- end
208
- end