hashie 3.5.7 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +281 -195
- data/CONTRIBUTING.md +13 -6
- data/LICENSE +1 -1
- data/README.md +320 -60
- data/Rakefile +2 -2
- data/UPGRADING.md +121 -7
- data/hashie.gemspec +13 -7
- data/lib/hashie/clash.rb +12 -1
- data/lib/hashie/dash.rb +56 -35
- data/lib/hashie/extensions/active_support/core_ext/hash.rb +14 -0
- data/lib/hashie/extensions/coercion.rb +26 -19
- data/lib/hashie/extensions/dash/indifferent_access.rb +29 -1
- data/lib/hashie/extensions/dash/predefined_values.rb +88 -0
- data/lib/hashie/extensions/dash/property_translation.rb +59 -28
- data/lib/hashie/extensions/deep_fetch.rb +5 -3
- data/lib/hashie/extensions/deep_find.rb +14 -5
- data/lib/hashie/extensions/deep_locate.rb +22 -8
- data/lib/hashie/extensions/deep_merge.rb +26 -10
- data/lib/hashie/extensions/ignore_undeclared.rb +4 -5
- data/lib/hashie/extensions/indifferent_access.rb +43 -10
- data/lib/hashie/extensions/key_conflict_warning.rb +55 -0
- data/lib/hashie/extensions/mash/define_accessors.rb +90 -0
- data/lib/hashie/extensions/mash/keep_original_keys.rb +4 -5
- data/lib/hashie/extensions/mash/permissive_respond_to.rb +61 -0
- data/lib/hashie/extensions/mash/safe_assignment.rb +3 -1
- data/lib/hashie/extensions/mash/symbolize_keys.rb +6 -6
- data/lib/hashie/extensions/method_access.rb +47 -14
- data/lib/hashie/extensions/parsers/yaml_erb_parser.rb +28 -4
- data/lib/hashie/extensions/ruby_version_check.rb +5 -1
- data/lib/hashie/extensions/strict_key_access.rb +16 -13
- data/lib/hashie/extensions/stringify_keys.rb +1 -1
- data/lib/hashie/extensions/symbolize_keys.rb +13 -2
- data/lib/hashie/hash.rb +18 -11
- data/lib/hashie/mash.rb +147 -81
- data/lib/hashie/railtie.rb +7 -0
- data/lib/hashie/rash.rb +6 -6
- data/lib/hashie/utils.rb +28 -0
- data/lib/hashie/version.rb +1 -1
- data/lib/hashie.rb +22 -19
- metadata +23 -131
- data/spec/hashie/array_spec.rb +0 -29
- data/spec/hashie/clash_spec.rb +0 -70
- data/spec/hashie/dash_spec.rb +0 -573
- data/spec/hashie/extensions/autoload_spec.rb +0 -24
- data/spec/hashie/extensions/coercion_spec.rb +0 -631
- data/spec/hashie/extensions/dash/coercion_spec.rb +0 -13
- data/spec/hashie/extensions/dash/indifferent_access_spec.rb +0 -84
- data/spec/hashie/extensions/deep_fetch_spec.rb +0 -97
- data/spec/hashie/extensions/deep_find_spec.rb +0 -138
- data/spec/hashie/extensions/deep_locate_spec.rb +0 -137
- data/spec/hashie/extensions/deep_merge_spec.rb +0 -70
- data/spec/hashie/extensions/ignore_undeclared_spec.rb +0 -47
- data/spec/hashie/extensions/indifferent_access_spec.rb +0 -282
- data/spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb +0 -208
- data/spec/hashie/extensions/key_conversion_spec.rb +0 -12
- data/spec/hashie/extensions/mash/keep_original_keys_spec.rb +0 -46
- data/spec/hashie/extensions/mash/safe_assignment_spec.rb +0 -50
- data/spec/hashie/extensions/mash/symbolize_keys_spec.rb +0 -39
- data/spec/hashie/extensions/merge_initializer_spec.rb +0 -23
- data/spec/hashie/extensions/method_access_spec.rb +0 -188
- data/spec/hashie/extensions/strict_key_access_spec.rb +0 -110
- data/spec/hashie/extensions/stringify_keys_spec.rb +0 -124
- data/spec/hashie/extensions/symbolize_keys_spec.rb +0 -129
- data/spec/hashie/hash_spec.rb +0 -84
- data/spec/hashie/mash_spec.rb +0 -763
- data/spec/hashie/parsers/yaml_erb_parser_spec.rb +0 -46
- data/spec/hashie/rash_spec.rb +0 -83
- data/spec/hashie/trash_spec.rb +0 -268
- data/spec/hashie/utils_spec.rb +0 -25
- data/spec/hashie/version_spec.rb +0 -7
- data/spec/hashie_spec.rb +0 -13
- data/spec/integration/omniauth/app.rb +0 -11
- data/spec/integration/omniauth/integration_spec.rb +0 -38
- data/spec/integration/omniauth-oauth2/app.rb +0 -53
- data/spec/integration/omniauth-oauth2/integration_spec.rb +0 -26
- data/spec/integration/omniauth-oauth2/some_site.rb +0 -38
- data/spec/integration/rails/app.rb +0 -48
- data/spec/integration/rails/integration_spec.rb +0 -26
- data/spec/integration/rails-without-dependency/integration_spec.rb +0 -15
- data/spec/spec_helper.rb +0 -23
- data/spec/support/integration_specs.rb +0 -36
- data/spec/support/logger.rb +0 -24
- data/spec/support/module_context.rb +0 -11
- data/spec/support/ruby_version_check.rb +0 -6
@@ -1,188 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Hashie::Extensions::MethodReader do
|
4
|
-
class ReaderHash < Hash
|
5
|
-
include Hashie::Extensions::MethodReader
|
6
|
-
|
7
|
-
def initialize(hash = {})
|
8
|
-
update(hash)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
subject { ReaderHash }
|
13
|
-
|
14
|
-
it 'reads string keys from the method' do
|
15
|
-
expect(subject.new('awesome' => 'sauce').awesome).to eq 'sauce'
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'reads symbol keys from the method' do
|
19
|
-
expect(subject.new(awesome: 'sauce').awesome).to eq 'sauce'
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'reads nil and false values out properly' do
|
23
|
-
h = subject.new(nil: nil, false: false)
|
24
|
-
expect(h.nil).to eq nil
|
25
|
-
expect(h.false).to eq false
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'raises a NoMethodError for undefined keys' do
|
29
|
-
expect { subject.new.awesome }.to raise_error(NoMethodError)
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'returns false for undefined keys if key with question has been called ' do
|
33
|
-
expect(subject.new.awesome?).to eq false
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'returns true for defined keys if key with question has been called' do
|
37
|
-
expect(subject.new(awesome: 'sauce').awesome?).to eq true
|
38
|
-
end
|
39
|
-
|
40
|
-
describe '#respond_to?' do
|
41
|
-
it 'is true for string keys' do
|
42
|
-
expect(subject.new('awesome' => 'sauce')).to be_respond_to(:awesome)
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'is true for symbol keys' do
|
46
|
-
expect(subject.new(awesome: 'sauce')).to be_respond_to(:awesome)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'is false for non-keys' do
|
50
|
-
expect(subject.new).not_to be_respond_to(:awesome)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe Hashie::Extensions::MethodWriter do
|
56
|
-
class WriterHash < Hash
|
57
|
-
include Hashie::Extensions::MethodWriter
|
58
|
-
end
|
59
|
-
|
60
|
-
subject { WriterHash.new }
|
61
|
-
|
62
|
-
it 'writes from a method call' do
|
63
|
-
subject.awesome = 'sauce'
|
64
|
-
expect(subject['awesome']).to eq 'sauce'
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'converts the key using the #convert_key method' do
|
68
|
-
allow(subject).to receive(:convert_key).and_return(:awesome)
|
69
|
-
subject.awesome = 'sauce'
|
70
|
-
expect(subject[:awesome]).to eq 'sauce'
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'raises NoMethodError on non equals-ending methods' do
|
74
|
-
expect { subject.awesome }.to raise_error(NoMethodError)
|
75
|
-
end
|
76
|
-
|
77
|
-
it '#respond_to? correctly' do
|
78
|
-
expect(subject).to be_respond_to(:abc=)
|
79
|
-
expect(subject).not_to be_respond_to(:abc)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
describe Hashie::Extensions::MethodQuery do
|
84
|
-
class QueryHash < Hash
|
85
|
-
include Hashie::Extensions::MethodQuery
|
86
|
-
|
87
|
-
def initialize(hash = {})
|
88
|
-
update(hash)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
subject { QueryHash }
|
93
|
-
|
94
|
-
it 'is true for non-nil string key values' do
|
95
|
-
expect(subject.new('abc' => 123).abc?).to eq true
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'is true for non-nil symbol key values' do
|
99
|
-
expect(subject.new(abc: 123).abc?).to eq true
|
100
|
-
end
|
101
|
-
|
102
|
-
it 'is false for false key values' do
|
103
|
-
expect(subject.new(abc: false).abc?).to eq false
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'is false for nil key values' do
|
107
|
-
expect(subject.new(abc: nil).abc?).to eq false
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'raises a NoMethodError for non-set keys' do
|
111
|
-
expect { subject.new.abc? }.to raise_error(NoMethodError)
|
112
|
-
end
|
113
|
-
|
114
|
-
it '#respond_to? for existing string keys' do
|
115
|
-
expect(subject.new('abc' => 'def')).to be_respond_to('abc?')
|
116
|
-
end
|
117
|
-
|
118
|
-
it '#respond_to? for existing symbol keys' do
|
119
|
-
expect(subject.new(abc: 'def')).to be_respond_to(:abc?)
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'does not #respond_to? for non-existent keys' do
|
123
|
-
expect(subject.new).not_to be_respond_to('abc?')
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe Hashie::Extensions::MethodAccess do
|
128
|
-
it 'includes all of the other method mixins' do
|
129
|
-
klass = Class.new(Hash)
|
130
|
-
klass.send :include, Hashie::Extensions::MethodAccess
|
131
|
-
expect((klass.ancestors & [Hashie::Extensions::MethodReader, Hashie::Extensions::MethodWriter, Hashie::Extensions::MethodQuery]).size).to eq 3
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
describe Hashie::Extensions::MethodOverridingWriter do
|
136
|
-
class OverridingHash < Hash
|
137
|
-
include Hashie::Extensions::MethodOverridingWriter
|
138
|
-
end
|
139
|
-
|
140
|
-
subject { OverridingHash.new }
|
141
|
-
|
142
|
-
it 'writes from a method call' do
|
143
|
-
subject.awesome = 'sauce'
|
144
|
-
expect(subject['awesome']).to eq 'sauce'
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'convertes the key using the #convert_key method' do
|
148
|
-
allow(subject).to receive(:convert_key).and_return(:awesome)
|
149
|
-
subject.awesome = 'sauce'
|
150
|
-
expect(subject[:awesome]).to eq 'sauce'
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'raises NoMethodError on non equals-ending methods' do
|
154
|
-
expect { subject.awesome }.to raise_error(NoMethodError)
|
155
|
-
end
|
156
|
-
|
157
|
-
it '#respond_to_missing? correctly' do
|
158
|
-
expect(subject).to respond_to(:abc=)
|
159
|
-
expect(subject).not_to respond_to(:abc)
|
160
|
-
expect(subject.method(:abc=)).not_to be_nil
|
161
|
-
end
|
162
|
-
|
163
|
-
context 'when writing a Hash method' do
|
164
|
-
before { subject.zip = 'a-dee-doo-dah' }
|
165
|
-
|
166
|
-
it 'overrides the original method' do
|
167
|
-
expect(subject.zip).to eq 'a-dee-doo-dah'
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'aliases the method with two leading underscores' do
|
171
|
-
expect(subject.__zip).to eq [[%w(zip a-dee-doo-dah)]]
|
172
|
-
end
|
173
|
-
|
174
|
-
it 'does not re-alias when overriding an already overridden method' do
|
175
|
-
subject.zip = 'test'
|
176
|
-
expect(subject.zip).to eq 'test'
|
177
|
-
expect(subject.__zip).to eq [[%w(zip test)]]
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
describe Hashie::Extensions::MethodAccessWithOverride do
|
183
|
-
it 'includes all of the other method mixins' do
|
184
|
-
klass = Class.new(Hash)
|
185
|
-
klass.send :include, Hashie::Extensions::MethodAccessWithOverride
|
186
|
-
expect((klass.ancestors & [Hashie::Extensions::MethodReader, Hashie::Extensions::MethodOverridingWriter, Hashie::Extensions::MethodQuery]).size).to eq 3
|
187
|
-
end
|
188
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Hashie::Extensions::StrictKeyAccess do
|
4
|
-
class StrictKeyAccessHash < Hash
|
5
|
-
include Hashie::Extensions::StrictKeyAccess
|
6
|
-
end
|
7
|
-
|
8
|
-
shared_examples_for 'StrictKeyAccess with valid key' do |options = {}|
|
9
|
-
before { pending_for(options[:pending]) } if options[:pending]
|
10
|
-
context 'set' do
|
11
|
-
let(:new_value) { 42 }
|
12
|
-
it('returns value') do
|
13
|
-
expect(instance.send(:[]=, valid_key, new_value)).to eq new_value
|
14
|
-
end
|
15
|
-
end
|
16
|
-
context 'access' do
|
17
|
-
it('returns value') do
|
18
|
-
expect(instance[valid_key]).to eq valid_value
|
19
|
-
end
|
20
|
-
end
|
21
|
-
context 'lookup' do
|
22
|
-
it('returns key') do
|
23
|
-
expect(instance.key(valid_value)).to eq valid_key
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
shared_examples_for 'StrictKeyAccess with invalid key' do |options = {}|
|
28
|
-
before { pending_for(options[:pending]) } if options[:pending]
|
29
|
-
context 'access' do
|
30
|
-
it('raises an error') do
|
31
|
-
# Formatting of the error message varies on Rubinius and ruby-head
|
32
|
-
expect { instance[invalid_key] }.to raise_error KeyError
|
33
|
-
end
|
34
|
-
end
|
35
|
-
context 'lookup' do
|
36
|
-
it('raises an error') do
|
37
|
-
# Formatting of the error message does not vary here because raised by StrictKeyAccess
|
38
|
-
expect { instance.key(invalid_value) }.to raise_error KeyError,
|
39
|
-
%(key not found with value of #{invalid_value.inspect})
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
shared_examples_for 'StrictKeyAccess raises KeyError instead of allowing defaults' do
|
44
|
-
context '#default' do
|
45
|
-
it 'raises an error' do
|
46
|
-
expect { instance.default(invalid_key) }.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError,
|
47
|
-
'Setting or using a default with Hashie::Extensions::StrictKeyAccess does not make sense'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
context '#default=' do
|
51
|
-
it 'raises an error' do
|
52
|
-
expect { instance.default = invalid_key }.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError,
|
53
|
-
'Setting or using a default with Hashie::Extensions::StrictKeyAccess does not make sense'
|
54
|
-
end
|
55
|
-
end
|
56
|
-
context '#default_proc' do
|
57
|
-
it 'raises an error' do
|
58
|
-
expect { instance.default_proc }.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError,
|
59
|
-
'Setting or using a default with Hashie::Extensions::StrictKeyAccess does not make sense'
|
60
|
-
end
|
61
|
-
end
|
62
|
-
context '#default_proc=' do
|
63
|
-
it 'raises an error' do
|
64
|
-
expect { instance.default_proc = proc {} }.to raise_error Hashie::Extensions::StrictKeyAccess::DefaultError,
|
65
|
-
'Setting or using a default with Hashie::Extensions::StrictKeyAccess does not make sense'
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
let(:klass) { StrictKeyAccessHash }
|
71
|
-
let(:instance) { StrictKeyAccessHash.new(*initialization_args) }
|
72
|
-
let(:initialization_args) do
|
73
|
-
[
|
74
|
-
{ valid_key => valid_value }
|
75
|
-
]
|
76
|
-
end
|
77
|
-
let(:valid_key) { :abc }
|
78
|
-
let(:valid_value) { 'def' }
|
79
|
-
let(:invalid_key) { :mega }
|
80
|
-
let(:invalid_value) { 'death' }
|
81
|
-
|
82
|
-
context '.new' do
|
83
|
-
context 'no defaults at initialization' do
|
84
|
-
let(:initialization_args) { [] }
|
85
|
-
before do
|
86
|
-
instance.merge!(valid_key => valid_value)
|
87
|
-
end
|
88
|
-
it_behaves_like 'StrictKeyAccess with valid key'
|
89
|
-
it_behaves_like 'StrictKeyAccess with invalid key'
|
90
|
-
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
|
91
|
-
end
|
92
|
-
context 'with defaults at initialization' do
|
93
|
-
before do
|
94
|
-
instance.merge!(valid_key => valid_value)
|
95
|
-
end
|
96
|
-
it_behaves_like 'StrictKeyAccess with valid key'
|
97
|
-
it_behaves_like 'StrictKeyAccess with invalid key'
|
98
|
-
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
|
99
|
-
end
|
100
|
-
it_behaves_like 'StrictKeyAccess with invalid key'
|
101
|
-
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
|
102
|
-
end
|
103
|
-
|
104
|
-
context '.[]' do
|
105
|
-
let(:instance) { StrictKeyAccessHash[*initialization_args] }
|
106
|
-
it_behaves_like 'StrictKeyAccess with valid key', pending: { engine: 'rbx' }
|
107
|
-
it_behaves_like 'StrictKeyAccess with invalid key', pending: { engine: 'rbx' }
|
108
|
-
it_behaves_like 'StrictKeyAccess raises KeyError instead of allowing defaults'
|
109
|
-
end
|
110
|
-
end
|
@@ -1,124 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/module_context'
|
3
|
-
|
4
|
-
def invoke(method)
|
5
|
-
if subject == object
|
6
|
-
subject.public_send(method)
|
7
|
-
else
|
8
|
-
subject.public_send(method, object)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
shared_examples 'stringify_keys!' do
|
13
|
-
it 'converts keys to strings' do
|
14
|
-
object[:abc] = 'abc'
|
15
|
-
object[123] = '123'
|
16
|
-
invoke :stringify_keys!
|
17
|
-
expect((object.keys & %w(abc 123)).size).to eq 2
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'converts nested instances of the same class' do
|
21
|
-
object[:ab] = dummy_class.new
|
22
|
-
object[:ab][:cd] = dummy_class.new
|
23
|
-
object[:ab][:cd][:ef] = 'abcdef'
|
24
|
-
invoke :stringify_keys!
|
25
|
-
expect(object).to eq('ab' => { 'cd' => { 'ef' => 'abcdef' } })
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'converts nested hashes' do
|
29
|
-
object[:ab] = { cd: { ef: 'abcdef' } }
|
30
|
-
invoke :stringify_keys!
|
31
|
-
expect(object).to eq('ab' => { 'cd' => { 'ef' => 'abcdef' } })
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'converts nested arrays' do
|
35
|
-
object[:ab] = []
|
36
|
-
object[:ab] << dummy_class.new
|
37
|
-
object[:ab] << dummy_class.new
|
38
|
-
object[:ab][0][:cd] = 'abcd'
|
39
|
-
object[:ab][1][:ef] = 'abef'
|
40
|
-
invoke :stringify_keys!
|
41
|
-
expect(object).to eq('ab' => [{ 'cd' => 'abcd' }, { 'ef' => 'abef' }])
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
shared_examples 'stringify_keys' do
|
46
|
-
it 'converts keys to strings' do
|
47
|
-
object[:abc] = 'def'
|
48
|
-
copy = invoke :stringify_keys
|
49
|
-
expect(copy['abc']).to eq 'def'
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'does not alter the original' do
|
53
|
-
object[:abc] = 'def'
|
54
|
-
copy = invoke :stringify_keys
|
55
|
-
expect(object.keys).to eq [:abc]
|
56
|
-
expect(copy.keys).to eq %w(abc)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe Hashie::Extensions::StringifyKeys do
|
61
|
-
include_context 'included hash module'
|
62
|
-
let(:object) { subject }
|
63
|
-
|
64
|
-
describe '#stringify_keys!' do
|
65
|
-
include_examples 'stringify_keys!'
|
66
|
-
|
67
|
-
it 'returns itself' do
|
68
|
-
expect(subject.stringify_keys!).to eq subject
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
context 'class methods' do
|
73
|
-
subject { described_class }
|
74
|
-
let(:object) { Hash.new }
|
75
|
-
|
76
|
-
describe '.stringify_keys' do
|
77
|
-
include_examples 'stringify_keys'
|
78
|
-
end
|
79
|
-
describe '.stringify_keys!' do
|
80
|
-
include_examples 'stringify_keys!'
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
context 'singleton methods' do
|
85
|
-
subject { Hash }
|
86
|
-
let(:object) { subject.new.merge(a: 1, b: { c: 2 }).extend(Hashie::Extensions::StringifyKeys) }
|
87
|
-
let(:expected_hash) { { 'a' => 1, 'b' => { 'c' => 2 } } }
|
88
|
-
|
89
|
-
describe '.stringify_keys' do
|
90
|
-
it 'does not raise error' do
|
91
|
-
expect { object.stringify_keys } .not_to raise_error
|
92
|
-
end
|
93
|
-
it 'produces expected stringified hash' do
|
94
|
-
expect(object.stringify_keys).to eq(expected_hash)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
describe '.stringify_keys!' do
|
98
|
-
it 'does not raise error' do
|
99
|
-
expect { object.stringify_keys! } .not_to raise_error
|
100
|
-
end
|
101
|
-
it 'produces expected stringified hash' do
|
102
|
-
expect(object.stringify_keys!).to eq(expected_hash)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe Hashie do
|
109
|
-
let!(:dummy_class) do
|
110
|
-
klass = Class.new(::Hash)
|
111
|
-
klass.send :include, Hashie::Extensions::StringifyKeys
|
112
|
-
klass
|
113
|
-
end
|
114
|
-
|
115
|
-
subject { described_class }
|
116
|
-
let(:object) { Hash.new }
|
117
|
-
|
118
|
-
describe '.stringify_keys' do
|
119
|
-
include_examples 'stringify_keys'
|
120
|
-
end
|
121
|
-
describe '.stringify_keys!' do
|
122
|
-
include_examples 'stringify_keys!'
|
123
|
-
end
|
124
|
-
end
|
@@ -1,129 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/module_context'
|
3
|
-
|
4
|
-
def invoke(method)
|
5
|
-
if subject == object
|
6
|
-
subject.public_send(method)
|
7
|
-
else
|
8
|
-
subject.public_send(method, object)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
shared_examples 'symbolize_keys!' do
|
13
|
-
it 'converts keys to symbols' do
|
14
|
-
object['abc'] = 'abc'
|
15
|
-
object['def'] = 'def'
|
16
|
-
invoke :symbolize_keys!
|
17
|
-
expect((object.keys & [:abc, :def]).size).to eq 2
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'converts nested instances of the same class' do
|
21
|
-
object['ab'] = dummy_class.new
|
22
|
-
object['ab']['cd'] = dummy_class.new
|
23
|
-
object['ab']['cd']['ef'] = 'abcdef'
|
24
|
-
invoke :symbolize_keys!
|
25
|
-
expect(object).to eq(ab: { cd: { ef: 'abcdef' } })
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'converts nested hashes' do
|
29
|
-
object['ab'] = { 'cd' => { 'ef' => 'abcdef' } }
|
30
|
-
invoke :symbolize_keys!
|
31
|
-
expect(object).to eq(ab: { cd: { ef: 'abcdef' } })
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'performs deep conversion within nested arrays' do
|
35
|
-
object['ab'] = []
|
36
|
-
object['ab'] << dummy_class.new
|
37
|
-
object['ab'] << dummy_class.new
|
38
|
-
object['ab'][0]['cd'] = 'abcd'
|
39
|
-
object['ab'][1]['ef'] = 'abef'
|
40
|
-
new_object = invoke :symbolize_keys
|
41
|
-
expect(new_object).to eq(ab: [{ cd: 'abcd' }, { ef: 'abef' }])
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
shared_examples 'symbolize_keys' do
|
46
|
-
it 'converts keys to symbols' do
|
47
|
-
object['abc'] = 'def'
|
48
|
-
copy = invoke :symbolize_keys
|
49
|
-
expect(copy[:abc]).to eq 'def'
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'does not alter the original' do
|
53
|
-
object['abc'] = 'def'
|
54
|
-
copy = invoke :symbolize_keys
|
55
|
-
expect(object.keys).to eq ['abc']
|
56
|
-
expect(copy.keys).to eq [:abc]
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe Hashie::Extensions::SymbolizeKeys do
|
61
|
-
include_context 'included hash module'
|
62
|
-
let(:object) { subject }
|
63
|
-
|
64
|
-
describe '#symbolize_keys!' do
|
65
|
-
include_examples 'symbolize_keys!'
|
66
|
-
let(:object) { subject }
|
67
|
-
|
68
|
-
it 'returns itself' do
|
69
|
-
expect(subject.symbolize_keys!).to eq subject
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
describe '#symbolize_keys' do
|
74
|
-
include_examples 'symbolize_keys'
|
75
|
-
end
|
76
|
-
|
77
|
-
context 'class methods' do
|
78
|
-
subject { described_class }
|
79
|
-
let(:object) { Hash.new }
|
80
|
-
|
81
|
-
describe '.symbolize_keys' do
|
82
|
-
include_examples 'symbolize_keys'
|
83
|
-
end
|
84
|
-
describe '.symbolize_keys!' do
|
85
|
-
include_examples 'symbolize_keys!'
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'singleton methods' do
|
90
|
-
subject { Hash }
|
91
|
-
let(:object) { subject.new.merge('a' => 1, 'b' => { 'c' => 2 }).extend(Hashie::Extensions::SymbolizeKeys) }
|
92
|
-
let(:expected_hash) { { a: 1, b: { c: 2 } } }
|
93
|
-
|
94
|
-
describe '.symbolize_keys' do
|
95
|
-
it 'does not raise error' do
|
96
|
-
expect { object.symbolize_keys }.not_to raise_error
|
97
|
-
end
|
98
|
-
it 'produces expected symbolized hash' do
|
99
|
-
expect(object.symbolize_keys).to eq(expected_hash)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
describe '.symbolize_keys!' do
|
103
|
-
it 'does not raise error' do
|
104
|
-
expect { object.symbolize_keys! }.not_to raise_error
|
105
|
-
end
|
106
|
-
it 'produces expected symbolized hash' do
|
107
|
-
expect(object.symbolize_keys!).to eq(expected_hash)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
describe Hashie do
|
114
|
-
let!(:dummy_class) do
|
115
|
-
klass = Class.new(::Hash)
|
116
|
-
klass.send :include, Hashie::Extensions::StringifyKeys
|
117
|
-
klass
|
118
|
-
end
|
119
|
-
|
120
|
-
subject { described_class }
|
121
|
-
let(:object) { Hash.new }
|
122
|
-
|
123
|
-
describe '.symbolize_keys' do
|
124
|
-
include_examples 'symbolize_keys'
|
125
|
-
end
|
126
|
-
describe '.symbolize_keys!' do
|
127
|
-
include_examples 'symbolize_keys!'
|
128
|
-
end
|
129
|
-
end
|
data/spec/hashie/hash_spec.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Hash do
|
4
|
-
it 'is convertible to a Hashie::Mash' do
|
5
|
-
mash = Hashie::Hash[some: 'hash'].to_mash
|
6
|
-
expect(mash.is_a?(Hashie::Mash)).to be_truthy
|
7
|
-
expect(mash.some).to eq 'hash'
|
8
|
-
end
|
9
|
-
|
10
|
-
it '#stringify_keys! turns all keys into strings' do
|
11
|
-
hash = Hashie::Hash[a: 'hey', 123 => 'bob']
|
12
|
-
hash.stringify_keys!
|
13
|
-
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
|
14
|
-
end
|
15
|
-
|
16
|
-
it '#stringify_keys! turns all keys into strings recursively' do
|
17
|
-
hash = Hashie::Hash[a: 'hey', 123 => { 345 => 'hey' }]
|
18
|
-
hash.stringify_keys!
|
19
|
-
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { '345' => 'hey' }]
|
20
|
-
end
|
21
|
-
|
22
|
-
it '#stringify_keys returns a hash with stringified keys' do
|
23
|
-
hash = Hashie::Hash[a: 'hey', 123 => 'bob']
|
24
|
-
stringified_hash = hash.stringify_keys
|
25
|
-
expect(hash).to eq Hashie::Hash[a: 'hey', 123 => 'bob']
|
26
|
-
expect(stringified_hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
|
27
|
-
end
|
28
|
-
|
29
|
-
it '#to_hash returns a hash with same keys' do
|
30
|
-
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
|
31
|
-
stringified_hash = hash.to_hash
|
32
|
-
expect(stringified_hash).to eq('a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3])
|
33
|
-
end
|
34
|
-
|
35
|
-
it '#to_hash with stringify_keys set to true returns a hash with stringified_keys' do
|
36
|
-
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
|
37
|
-
symbolized_hash = hash.to_hash(stringify_keys: true)
|
38
|
-
expect(symbolized_hash).to eq('a' => 'hey', '123' => 'bob', 'array' => [1, 2, 3])
|
39
|
-
end
|
40
|
-
|
41
|
-
it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do
|
42
|
-
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
|
43
|
-
symbolized_hash = hash.to_hash(symbolize_keys: true)
|
44
|
-
expect(symbolized_hash).to eq(a: 'hey', :"123" => 'bob', array: [1, 2, 3])
|
45
|
-
end
|
46
|
-
|
47
|
-
it "#to_hash should not blow up when #to_hash doesn't accept arguments" do
|
48
|
-
class BareCustomMash < Hashie::Mash
|
49
|
-
def to_hash
|
50
|
-
{}
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
h = Hashie::Hash.new
|
55
|
-
h[:key] = BareCustomMash.new
|
56
|
-
expect { h.to_hash }.not_to raise_error
|
57
|
-
end
|
58
|
-
|
59
|
-
describe 'when the value is an object that respond_to to_hash' do
|
60
|
-
class ClassRespondsToHash
|
61
|
-
def to_hash(options = {})
|
62
|
-
Hashie::Hash['a' => 'hey', b: 'bar', 123 => 'bob', 'array' => [1, 2, 3]].to_hash(options)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
it '#to_hash returns a hash with same keys' do
|
67
|
-
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: ClassRespondsToHash.new]
|
68
|
-
stringified_hash = hash.to_hash
|
69
|
-
expect(stringified_hash).to eq('a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: { 'a' => 'hey', b: 'bar', 123 => 'bob', 'array' => [1, 2, 3] })
|
70
|
-
end
|
71
|
-
|
72
|
-
it '#to_hash with stringify_keys set to true returns a hash with stringified_keys' do
|
73
|
-
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: ClassRespondsToHash.new]
|
74
|
-
symbolized_hash = hash.to_hash(stringify_keys: true)
|
75
|
-
expect(symbolized_hash).to eq('a' => 'hey', '123' => 'bob', 'array' => [1, 2, 3], 'subhash' => { 'a' => 'hey', 'b' => 'bar', '123' => 'bob', 'array' => [1, 2, 3] })
|
76
|
-
end
|
77
|
-
|
78
|
-
it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do
|
79
|
-
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3], subhash: ClassRespondsToHash.new]
|
80
|
-
symbolized_hash = hash.to_hash(symbolize_keys: true)
|
81
|
-
expect(symbolized_hash).to eq(a: 'hey', :"123" => 'bob', array: [1, 2, 3], subhash: { a: 'hey', b: 'bar', :'123' => 'bob', array: [1, 2, 3] })
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|