hashie 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/lib/hashie/hash.rb +13 -3
- data/lib/hashie/version.rb +1 -1
- data/spec/hashie/clash_spec.rb +11 -11
- data/spec/hashie/dash_spec.rb +69 -60
- data/spec/hashie/extensions/coercion_spec.rb +22 -22
- data/spec/hashie/extensions/deep_fetch_spec.rb +7 -7
- data/spec/hashie/extensions/deep_merge_spec.rb +2 -2
- data/spec/hashie/extensions/indifferent_access_spec.rb +24 -24
- data/spec/hashie/extensions/key_conversion_spec.rb +14 -14
- data/spec/hashie/extensions/merge_initializer_spec.rb +4 -4
- data/spec/hashie/extensions/method_access_spec.rb +22 -22
- data/spec/hashie/hash_spec.rb +19 -7
- data/spec/hashie/mash_spec.rb +122 -123
- data/spec/hashie/rash_spec.rb +13 -13
- data/spec/hashie/trash_spec.rb +30 -30
- data/spec/hashie/version_spec.rb +1 -1
- metadata +2 -2
@@ -29,22 +29,22 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
29
29
|
shared_examples_for 'hash with indifferent access' do
|
30
30
|
it 'is able to access via string or symbol' do
|
31
31
|
h = subject.build(abc: 123)
|
32
|
-
h[:abc].
|
33
|
-
h['abc'].
|
32
|
+
expect(h[:abc]).to eq 123
|
33
|
+
expect(h['abc']).to eq 123
|
34
34
|
end
|
35
35
|
|
36
36
|
describe '#values_at' do
|
37
37
|
it 'indifferently finds values' do
|
38
38
|
h = subject.build(:foo => 'bar', 'baz' => 'qux')
|
39
|
-
h.values_at('foo', :baz).
|
39
|
+
expect(h.values_at('foo', :baz)).to eq %w(bar qux)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
describe '#fetch' do
|
44
44
|
it 'works like normal fetch, but indifferent' do
|
45
45
|
h = subject.build(foo: 'bar')
|
46
|
-
h.fetch(:foo).
|
47
|
-
h.fetch(:foo).
|
46
|
+
expect(h.fetch(:foo)).to eq h.fetch('foo')
|
47
|
+
expect(h.fetch(:foo)).to eq 'bar'
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -53,7 +53,7 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
53
53
|
h = subject.build(:foo => 'bar', 'baz' => 'qux')
|
54
54
|
h.delete('foo')
|
55
55
|
h.delete(:baz)
|
56
|
-
h.
|
56
|
+
expect(h).to be_empty
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -61,14 +61,14 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
61
61
|
let(:h) { subject.build(foo: 'bar') }
|
62
62
|
|
63
63
|
it 'finds it indifferently' do
|
64
|
-
h.
|
65
|
-
h.
|
64
|
+
expect(h).to be_key(:foo)
|
65
|
+
expect(h).to be_key('foo')
|
66
66
|
end
|
67
67
|
|
68
68
|
%w(include? member? has_key?).each do |key_alias|
|
69
69
|
it "is aliased as #{key_alias}" do
|
70
|
-
h.send(key_alias.to_sym, :foo).
|
71
|
-
h.send(key_alias.to_sym, 'foo').
|
70
|
+
expect(h.send(key_alias.to_sym, :foo)).to be(true)
|
71
|
+
expect(h.send(key_alias.to_sym, 'foo')).to be(true)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
@@ -78,18 +78,18 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
78
78
|
|
79
79
|
it 'allows keys to be indifferent still' do
|
80
80
|
h.update(baz: 'qux')
|
81
|
-
h['foo'].
|
82
|
-
h['baz'].
|
81
|
+
expect(h['foo']).to eq 'bar'
|
82
|
+
expect(h['baz']).to eq 'qux'
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'recursively injects indifference into sub-hashes' do
|
86
86
|
h.update(baz: { qux: 'abc' })
|
87
|
-
h['baz']['qux'].
|
87
|
+
expect(h['baz']['qux']).to eq 'abc'
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'does not change the ancestors of the injected object class' do
|
91
91
|
h.update(baz: { qux: 'abc' })
|
92
|
-
Hash.new.
|
92
|
+
expect(Hash.new).not_to be_respond_to(:indifferent_access?)
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
@@ -97,22 +97,22 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
97
97
|
let(:h) { subject.build(foo: 'bar').replace(bar: 'baz', hi: 'bye') }
|
98
98
|
|
99
99
|
it 'returns self' do
|
100
|
-
h.
|
100
|
+
expect(h).to be_a(subject)
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'removes old keys' do
|
104
104
|
[:foo, 'foo'].each do |k|
|
105
|
-
h[k].
|
106
|
-
h.key?(k).
|
105
|
+
expect(h[k]).to be_nil
|
106
|
+
expect(h.key?(k)).to be_false
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
110
|
it 'creates new keys with indifferent access' do
|
111
|
-
[:bar, 'bar', :hi, 'hi'].each { |k| h.key?(k).
|
112
|
-
h[:bar].
|
113
|
-
h['bar'].
|
114
|
-
h[:hi].
|
115
|
-
h['hi'].
|
111
|
+
[:bar, 'bar', :hi, 'hi'].each { |k| expect(h.key?(k)).to be_true }
|
112
|
+
expect(h[:bar]).to eq 'baz'
|
113
|
+
expect(h['bar']).to eq 'baz'
|
114
|
+
expect(h[:hi]).to eq 'bye'
|
115
|
+
expect(h['hi']).to eq 'bye'
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
@@ -121,7 +121,7 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
121
121
|
let(:h) { subject.try_convert(foo: 'bar') }
|
122
122
|
|
123
123
|
it 'is a subject' do
|
124
|
-
h.
|
124
|
+
expect(h).to be_a(subject)
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
@@ -129,7 +129,7 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
129
129
|
let(:h) { subject.try_convert('{ :foo => bar }') }
|
130
130
|
|
131
131
|
it 'is nil' do
|
132
|
-
h.
|
132
|
+
expect(h).to be_nil
|
133
133
|
end
|
134
134
|
end
|
135
135
|
end
|
@@ -14,7 +14,7 @@ describe Hashie::Extensions::KeyConversion do
|
|
14
14
|
instance[:abc] = 'abc'
|
15
15
|
instance[123] = '123'
|
16
16
|
instance.stringify_keys!
|
17
|
-
(instance.keys & %w(abc 123)).size.
|
17
|
+
expect((instance.keys & %w(abc 123)).size).to eq 2
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'performs deep conversion within nested hashes' do
|
@@ -22,7 +22,7 @@ describe Hashie::Extensions::KeyConversion do
|
|
22
22
|
instance[:ab][:cd] = subject.new
|
23
23
|
instance[:ab][:cd][:ef] = 'abcdef'
|
24
24
|
instance.stringify_keys!
|
25
|
-
instance.
|
25
|
+
expect(instance).to eq('ab' => { 'cd' => { 'ef' => 'abcdef' } })
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'performs deep conversion within nested arrays' do
|
@@ -32,11 +32,11 @@ describe Hashie::Extensions::KeyConversion do
|
|
32
32
|
instance[:ab][0][:cd] = 'abcd'
|
33
33
|
instance[:ab][1][:ef] = 'abef'
|
34
34
|
instance.stringify_keys!
|
35
|
-
instance.
|
35
|
+
expect(instance).to eq('ab' => [{ 'cd' => 'abcd' }, { 'ef' => 'abef' }])
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'returns itself' do
|
39
|
-
instance.stringify_keys
|
39
|
+
expect(instance.stringify_keys!).to eq instance
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -44,14 +44,14 @@ describe Hashie::Extensions::KeyConversion do
|
|
44
44
|
it 'converts keys to strings' do
|
45
45
|
instance[:abc] = 'def'
|
46
46
|
copy = instance.stringify_keys
|
47
|
-
copy['abc'].
|
47
|
+
expect(copy['abc']).to eq 'def'
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'does not alter the original' do
|
51
51
|
instance[:abc] = 'def'
|
52
52
|
copy = instance.stringify_keys
|
53
|
-
instance.keys.
|
54
|
-
copy.keys.
|
53
|
+
expect(instance.keys).to eq [:abc]
|
54
|
+
expect(copy.keys).to eq %w(abc)
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -60,7 +60,7 @@ describe Hashie::Extensions::KeyConversion do
|
|
60
60
|
instance['abc'] = 'abc'
|
61
61
|
instance['def'] = 'def'
|
62
62
|
instance.symbolize_keys!
|
63
|
-
(instance.keys & [:abc, :def]).size.
|
63
|
+
expect((instance.keys & [:abc, :def]).size).to eq 2
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'performs deep conversion within nested hashes' do
|
@@ -68,7 +68,7 @@ describe Hashie::Extensions::KeyConversion do
|
|
68
68
|
instance['ab']['cd'] = subject.new
|
69
69
|
instance['ab']['cd']['ef'] = 'abcdef'
|
70
70
|
instance.symbolize_keys!
|
71
|
-
instance.
|
71
|
+
expect(instance).to eq(ab: { cd: { ef: 'abcdef' } })
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'performs deep conversion within nested arrays' do
|
@@ -78,11 +78,11 @@ describe Hashie::Extensions::KeyConversion do
|
|
78
78
|
instance['ab'][0]['cd'] = 'abcd'
|
79
79
|
instance['ab'][1]['ef'] = 'abef'
|
80
80
|
instance.symbolize_keys!
|
81
|
-
instance.
|
81
|
+
expect(instance).to eq(ab: [{ cd: 'abcd' }, { ef: 'abef' }])
|
82
82
|
end
|
83
83
|
|
84
84
|
it 'returns itself' do
|
85
|
-
instance.symbolize_keys
|
85
|
+
expect(instance.symbolize_keys!).to eq instance
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -90,14 +90,14 @@ describe Hashie::Extensions::KeyConversion do
|
|
90
90
|
it 'converts keys to symbols' do
|
91
91
|
instance['abc'] = 'def'
|
92
92
|
copy = instance.symbolize_keys
|
93
|
-
copy[:abc].
|
93
|
+
expect(copy[:abc]).to eq 'def'
|
94
94
|
end
|
95
95
|
|
96
96
|
it 'does not alter the original' do
|
97
97
|
instance['abc'] = 'def'
|
98
98
|
copy = instance.symbolize_keys
|
99
|
-
instance.keys.
|
100
|
-
copy.keys.
|
99
|
+
expect(instance.keys).to eq ['abc']
|
100
|
+
expect(copy.keys).to eq [:abc]
|
101
101
|
end
|
102
102
|
end
|
103
103
|
end
|
@@ -8,16 +8,16 @@ describe Hashie::Extensions::MergeInitializer do
|
|
8
8
|
subject { MergeInitializerHash }
|
9
9
|
|
10
10
|
it 'initializes with no arguments' do
|
11
|
-
subject.new.
|
11
|
+
expect(subject.new).to eq({})
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'initializes with a hash' do
|
15
|
-
subject.new(abc: 'def').
|
15
|
+
expect(subject.new(abc: 'def')).to eq(abc: 'def')
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'initializes with a hash and a default' do
|
19
19
|
h = subject.new({ abc: 'def' }, 'bar')
|
20
|
-
h[:foo].
|
21
|
-
h[:abc].
|
20
|
+
expect(h[:foo]).to eq 'bar'
|
21
|
+
expect(h[:abc]).to eq 'def'
|
22
22
|
end
|
23
23
|
end
|
@@ -12,34 +12,34 @@ describe Hashie::Extensions::MethodReader do
|
|
12
12
|
subject { ReaderHash }
|
13
13
|
|
14
14
|
it 'reads string keys from the method' do
|
15
|
-
subject.new('awesome' => 'sauce').awesome.
|
15
|
+
expect(subject.new('awesome' => 'sauce').awesome).to eq 'sauce'
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'reads symbol keys from the method' do
|
19
|
-
subject.new(awesome: 'sauce').awesome.
|
19
|
+
expect(subject.new(awesome: 'sauce').awesome).to eq 'sauce'
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'reads nil and false values out properly' do
|
23
23
|
h = subject.new(nil: nil, false: false)
|
24
|
-
h.nil.
|
25
|
-
h.false.
|
24
|
+
expect(h.nil).to eq nil
|
25
|
+
expect(h.false).to eq false
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'raises a NoMethodError for undefined keys' do
|
29
|
-
|
29
|
+
expect { subject.new.awesome }.to raise_error(NoMethodError)
|
30
30
|
end
|
31
31
|
|
32
32
|
describe '#respond_to?' do
|
33
33
|
it 'is true for string keys' do
|
34
|
-
subject.new('awesome' => 'sauce').
|
34
|
+
expect(subject.new('awesome' => 'sauce')).to be_respond_to(:awesome)
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'is true for symbol keys' do
|
38
|
-
subject.new(awesome: 'sauce').
|
38
|
+
expect(subject.new(awesome: 'sauce')).to be_respond_to(:awesome)
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'is false for non-keys' do
|
42
|
-
subject.new.
|
42
|
+
expect(subject.new).not_to be_respond_to(:awesome)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -53,22 +53,22 @@ describe Hashie::Extensions::MethodWriter do
|
|
53
53
|
|
54
54
|
it 'writes from a method call' do
|
55
55
|
subject.awesome = 'sauce'
|
56
|
-
subject['awesome'].
|
56
|
+
expect(subject['awesome']).to eq 'sauce'
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'converts the key using the #convert_key method' do
|
60
|
-
subject.
|
60
|
+
allow(subject).to receive(:convert_key).and_return(:awesome)
|
61
61
|
subject.awesome = 'sauce'
|
62
|
-
subject[:awesome].
|
62
|
+
expect(subject[:awesome]).to eq 'sauce'
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'raises NoMethodError on non equals-ending methods' do
|
66
|
-
|
66
|
+
expect { subject.awesome }.to raise_error(NoMethodError)
|
67
67
|
end
|
68
68
|
|
69
69
|
it '#respond_to? correctly' do
|
70
|
-
subject.
|
71
|
-
subject.
|
70
|
+
expect(subject).to be_respond_to(:abc=)
|
71
|
+
expect(subject).not_to be_respond_to(:abc)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
@@ -84,31 +84,31 @@ describe Hashie::Extensions::MethodQuery do
|
|
84
84
|
subject { QueryHash }
|
85
85
|
|
86
86
|
it 'is true for non-nil string key values' do
|
87
|
-
subject.new('abc' => 123).
|
87
|
+
expect(subject.new('abc' => 123)).to be_abc
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'is true for non-nil symbol key values' do
|
91
|
-
subject.new(abc: 123).
|
91
|
+
expect(subject.new(abc: 123)).to be_abc
|
92
92
|
end
|
93
93
|
|
94
94
|
it 'is false for nil key values' do
|
95
|
-
subject.new(abc: false).
|
95
|
+
expect(subject.new(abc: false)).not_to be_abc
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'raises a NoMethodError for non-set keys' do
|
99
|
-
|
99
|
+
expect { subject.new.abc? }.to raise_error(NoMethodError)
|
100
100
|
end
|
101
101
|
|
102
102
|
it '#respond_to? for existing string keys' do
|
103
|
-
subject.new('abc' => 'def').
|
103
|
+
expect(subject.new('abc' => 'def')).to be_respond_to('abc?')
|
104
104
|
end
|
105
105
|
|
106
106
|
it '#respond_to? for existing symbol keys' do
|
107
|
-
subject.new(abc: 'def').
|
107
|
+
expect(subject.new(abc: 'def')).to be_respond_to(:abc?)
|
108
108
|
end
|
109
109
|
|
110
110
|
it 'does not #respond_to? for non-existent keys' do
|
111
|
-
subject.new.
|
111
|
+
expect(subject.new).not_to be_respond_to('abc?')
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
@@ -116,6 +116,6 @@ describe Hashie::Extensions::MethodAccess do
|
|
116
116
|
it 'includes all of the other method mixins' do
|
117
117
|
klass = Class.new(Hash)
|
118
118
|
klass.send :include, Hashie::Extensions::MethodAccess
|
119
|
-
(klass.ancestors & [Hashie::Extensions::MethodReader, Hashie::Extensions::MethodWriter, Hashie::Extensions::MethodQuery]).size.
|
119
|
+
expect((klass.ancestors & [Hashie::Extensions::MethodReader, Hashie::Extensions::MethodWriter, Hashie::Extensions::MethodQuery]).size).to eq 3
|
120
120
|
end
|
121
121
|
end
|
data/spec/hashie/hash_spec.rb
CHANGED
@@ -3,32 +3,44 @@ require 'spec_helper'
|
|
3
3
|
describe Hash do
|
4
4
|
it 'is convertible to a Hashie::Mash' do
|
5
5
|
mash = Hashie::Hash[some: 'hash'].to_mash
|
6
|
-
mash.is_a?(Hashie::Mash).
|
7
|
-
mash.some.
|
6
|
+
expect(mash.is_a?(Hashie::Mash)).to be_true
|
7
|
+
expect(mash.some).to eq 'hash'
|
8
8
|
end
|
9
9
|
|
10
10
|
it '#stringify_keys! turns all keys into strings' do
|
11
11
|
hash = Hashie::Hash[:a => 'hey', 123 => 'bob']
|
12
12
|
hash.stringify_keys!
|
13
|
-
hash.
|
13
|
+
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
|
14
14
|
end
|
15
15
|
|
16
16
|
it '#stringify_keys returns a hash with stringified keys' do
|
17
17
|
hash = Hashie::Hash[:a => 'hey', 123 => 'bob']
|
18
18
|
stringified_hash = hash.stringify_keys
|
19
|
-
hash.
|
20
|
-
stringified_hash.
|
19
|
+
expect(hash).to eq Hashie::Hash[:a => 'hey', 123 => 'bob']
|
20
|
+
expect(stringified_hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
|
21
21
|
end
|
22
22
|
|
23
23
|
it '#to_hash returns a hash with stringified keys' do
|
24
24
|
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
|
25
25
|
stringified_hash = hash.to_hash
|
26
|
-
stringified_hash.
|
26
|
+
expect(stringified_hash).to eq('a' => 'hey', '123' => 'bob', 'array' => [1, 2, 3])
|
27
27
|
end
|
28
28
|
|
29
29
|
it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do
|
30
30
|
hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
|
31
31
|
symbolized_hash = hash.to_hash(symbolize_keys: true)
|
32
|
-
symbolized_hash.
|
32
|
+
expect(symbolized_hash).to eq(:a => 'hey', :"123" => 'bob', :array => [1, 2, 3])
|
33
|
+
end
|
34
|
+
|
35
|
+
it "#to_hash should not blow up when #to_hash doesn't accept arguments" do
|
36
|
+
class BareCustomMash < Hashie::Mash
|
37
|
+
def to_hash
|
38
|
+
{}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
h = Hashie::Hash.new
|
43
|
+
h[:key] = BareCustomMash.new
|
44
|
+
expect { h.to_hash }.not_to raise_error
|
33
45
|
end
|
34
46
|
end
|