hashie 3.4.3 → 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.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +516 -129
  3. data/CONTRIBUTING.md +24 -7
  4. data/LICENSE +1 -1
  5. data/README.md +408 -50
  6. data/Rakefile +18 -1
  7. data/UPGRADING.md +157 -7
  8. data/hashie.gemspec +14 -8
  9. data/lib/hashie/array.rb +21 -0
  10. data/lib/hashie/clash.rb +24 -12
  11. data/lib/hashie/dash.rb +56 -31
  12. data/lib/hashie/extensions/active_support/core_ext/hash.rb +14 -0
  13. data/lib/hashie/extensions/array/pretty_inspect.rb +19 -0
  14. data/lib/hashie/extensions/coercion.rb +30 -17
  15. data/lib/hashie/extensions/dash/indifferent_access.rb +30 -1
  16. data/lib/hashie/extensions/dash/predefined_values.rb +88 -0
  17. data/lib/hashie/extensions/dash/property_translation.rb +59 -28
  18. data/lib/hashie/extensions/deep_fetch.rb +5 -3
  19. data/lib/hashie/extensions/deep_find.rb +14 -5
  20. data/lib/hashie/extensions/deep_locate.rb +40 -21
  21. data/lib/hashie/extensions/deep_merge.rb +26 -10
  22. data/lib/hashie/extensions/ignore_undeclared.rb +6 -4
  23. data/lib/hashie/extensions/indifferent_access.rb +49 -8
  24. data/lib/hashie/extensions/key_conflict_warning.rb +55 -0
  25. data/lib/hashie/extensions/mash/define_accessors.rb +90 -0
  26. data/lib/hashie/extensions/mash/keep_original_keys.rb +53 -0
  27. data/lib/hashie/extensions/mash/permissive_respond_to.rb +61 -0
  28. data/lib/hashie/extensions/mash/safe_assignment.rb +3 -1
  29. data/lib/hashie/extensions/mash/symbolize_keys.rb +38 -0
  30. data/lib/hashie/extensions/method_access.rb +77 -19
  31. data/lib/hashie/extensions/parsers/yaml_erb_parser.rb +29 -5
  32. data/lib/hashie/extensions/ruby_version.rb +60 -0
  33. data/lib/hashie/extensions/ruby_version_check.rb +21 -0
  34. data/lib/hashie/extensions/strict_key_access.rb +16 -13
  35. data/lib/hashie/extensions/stringify_keys.rb +1 -1
  36. data/lib/hashie/extensions/symbolize_keys.rb +13 -2
  37. data/lib/hashie/hash.rb +18 -11
  38. data/lib/hashie/logger.rb +18 -0
  39. data/lib/hashie/mash.rb +177 -43
  40. data/lib/hashie/railtie.rb +21 -0
  41. data/lib/hashie/rash.rb +7 -7
  42. data/lib/hashie/utils.rb +44 -0
  43. data/lib/hashie/version.rb +1 -1
  44. data/lib/hashie.rb +33 -17
  45. metadata +28 -95
  46. data/spec/hashie/clash_spec.rb +0 -48
  47. data/spec/hashie/dash_spec.rb +0 -513
  48. data/spec/hashie/extensions/autoload_spec.rb +0 -24
  49. data/spec/hashie/extensions/coercion_spec.rb +0 -625
  50. data/spec/hashie/extensions/dash/coercion_spec.rb +0 -13
  51. data/spec/hashie/extensions/dash/indifferent_access_spec.rb +0 -84
  52. data/spec/hashie/extensions/deep_fetch_spec.rb +0 -97
  53. data/spec/hashie/extensions/deep_find_spec.rb +0 -45
  54. data/spec/hashie/extensions/deep_locate_spec.rb +0 -124
  55. data/spec/hashie/extensions/deep_merge_spec.rb +0 -65
  56. data/spec/hashie/extensions/ignore_undeclared_spec.rb +0 -46
  57. data/spec/hashie/extensions/indifferent_access_spec.rb +0 -219
  58. data/spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb +0 -208
  59. data/spec/hashie/extensions/key_conversion_spec.rb +0 -12
  60. data/spec/hashie/extensions/mash/safe_assignment_spec.rb +0 -50
  61. data/spec/hashie/extensions/merge_initializer_spec.rb +0 -23
  62. data/spec/hashie/extensions/method_access_spec.rb +0 -184
  63. data/spec/hashie/extensions/strict_key_access_spec.rb +0 -110
  64. data/spec/hashie/extensions/stringify_keys_spec.rb +0 -124
  65. data/spec/hashie/extensions/symbolize_keys_spec.rb +0 -129
  66. data/spec/hashie/hash_spec.rb +0 -84
  67. data/spec/hashie/mash_spec.rb +0 -680
  68. data/spec/hashie/parsers/yaml_erb_parser_spec.rb +0 -29
  69. data/spec/hashie/rash_spec.rb +0 -77
  70. data/spec/hashie/trash_spec.rb +0 -268
  71. data/spec/hashie/version_spec.rb +0 -7
  72. data/spec/spec_helper.rb +0 -16
  73. data/spec/support/module_context.rb +0 -11
@@ -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
@@ -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