darthjee-core_ext 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +5 -0
  5. data/Gemfile.lock +62 -0
  6. data/LICENSE +22 -0
  7. data/README.md +188 -0
  8. data/Rakefile +7 -0
  9. data/core_ext.gemspec +27 -0
  10. data/lib/array.rb +23 -0
  11. data/lib/array/hash_builder.rb +20 -0
  12. data/lib/darthjee.rb +4 -0
  13. data/lib/darthjee/core_ext.rb +12 -0
  14. data/lib/darthjee/core_ext/version.rb +5 -0
  15. data/lib/enumerable.rb +45 -0
  16. data/lib/hash.rb +195 -0
  17. data/lib/hash/deep_hash_constructor.rb +83 -0
  18. data/lib/hash/key_changer.rb +76 -0
  19. data/lib/hash/value_changer.rb +61 -0
  20. data/lib/numeric.rb +6 -0
  21. data/lib/symbol.rb +9 -0
  22. data/spec/lib/array_spec.rb +229 -0
  23. data/spec/lib/enumerable_spec.rb +31 -0
  24. data/spec/lib/hash/deep_hash_constructor_spec.rb +167 -0
  25. data/spec/lib/hash/key_changer_spec.rb +55 -0
  26. data/spec/lib/hash_spec.rb +347 -0
  27. data/spec/lib/numeric_spec.rb +61 -0
  28. data/spec/lib/symbol_spec.rb +35 -0
  29. data/spec/spec_helper.rb +32 -0
  30. data/spec/support/models/hash/value_changer/dummy.rb +15 -0
  31. data/spec/support/models/hash/value_changer/dummy_iteractor.rb +12 -0
  32. data/spec/support/shared_examples/array_random.rb +16 -0
  33. data/spec/support/shared_examples/chain_fetch.rb +88 -0
  34. data/spec/support/shared_examples/chain_hash_keys_changer.rb +88 -0
  35. data/spec/support/shared_examples/clean.rb +143 -0
  36. data/spec/support/shared_examples/expected.rb +6 -0
  37. data/spec/support/shared_examples/hash_keys_changer.rb +85 -0
  38. data/spec/support/shared_examples/keys_appender.rb +43 -0
  39. data/spec/support/shared_examples/keys_camelizer.rb +285 -0
  40. data/spec/support/shared_examples/keys_underscorer.rb +82 -0
  41. data/spec/support/shared_examples/remap.rb +89 -0
  42. data/spec/support/shared_examples/value_changer.rb +92 -0
  43. metadata +217 -0
@@ -0,0 +1,89 @@
1
+ shared_examples 'a class with remap method' do
2
+ let(:subject) { { a: 1, b: 2 } }
3
+
4
+ describe :remap_keys do
5
+ it_behaves_like 'a method that remaps the keys', :remap_keys
6
+
7
+ it 'does not change the original hash' do
8
+ expect { subject.remap_keys(a: :e) }.not_to change { subject }
9
+ end
10
+ end
11
+
12
+ describe :remap_keys! do
13
+ it_behaves_like 'a method that remaps the keys', :remap_keys!
14
+
15
+ it 'changes the original hash' do
16
+ expect { subject.remap_keys!(a: :e) }.to change { subject }
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ shared_examples 'a method that remaps the keys' do |method|
23
+ let(:result) { subject.public_send(method, remap) }
24
+
25
+ context 'when remap and hash keys match' do
26
+ let(:remap) { { a: :e, b: :f } }
27
+
28
+ it 'remaps the keys' do
29
+ expect(result).to eq(e: 1, f: 2)
30
+ end
31
+ end
32
+
33
+ context 'when remap and hash keys do not match' do
34
+ let(:remap) { { b: :f } }
35
+
36
+ it 'remap only the keys that match' do
37
+ expect(result).to eq(a: 1, f: 2)
38
+ end
39
+ end
40
+
41
+ context 'when there are keys out of the keys list' do
42
+ let(:remap) { { c: :g } }
43
+
44
+ it 'creates a nil valued key' do
45
+ expect(result).to eq(a: 1, b: 2, g: nil)
46
+ end
47
+ end
48
+
49
+ context 'when remap has no keys' do
50
+ let(:remap) { {} }
51
+
52
+ it 'does not remap the keys' do
53
+ expect(result).to eq(a: 1, b: 2)
54
+ end
55
+ end
56
+
57
+ context 'when remap has original array keys' do
58
+ let(:remap) { { b: :a, a: :b } }
59
+
60
+ it 'does not remap the keys' do
61
+ expect(result).to eq(b: 1, a: 2)
62
+ end
63
+ end
64
+
65
+ context 'when remap has original array keys' do
66
+ let(:remap) { { a: :b, b: :c } }
67
+
68
+ it 'does not remap the keys' do
69
+ expect(result).to eq(b: 1, c: 2)
70
+ end
71
+ end
72
+
73
+ context 'when changing the type of the key' do
74
+ let(:remap) { { a: 'a' } }
75
+
76
+ it 'does not remap the keys' do
77
+ expect(result).to eq('a' => 1, b: 2)
78
+ end
79
+
80
+ context 'and the original key is an string' do
81
+ let(:subject) { { 'a' => 1, 'b' => 2 } }
82
+ let(:remap) { { 'a' => :a } }
83
+
84
+ it 'does not remap the keys' do
85
+ expect(result).to eq(a: 1, 'b' => 2)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,92 @@
1
+ shared_examples 'a class with change_values method' do
2
+ let(:subject) { { a: 1, b: 2, c: { d: 3, e: 4 } } }
3
+
4
+ describe :change_values do
5
+ it_behaves_like 'a method that change the hash values', :change_values
6
+
7
+ it 'does not change original hash' do
8
+ expect do
9
+ subject.change_values { |value| value + 1 }
10
+ end.not_to change { subject }
11
+ end
12
+ end
13
+
14
+ describe :change_values! do
15
+ it_behaves_like 'a method that change the hash values', :change_values!
16
+
17
+ it 'changes original hash' do
18
+ expect do
19
+ subject.change_values! { |value| value + 1 }
20
+ end.to change { subject }
21
+ end
22
+ end
23
+ end
24
+
25
+ shared_examples 'a method that change the hash values' do |method|
26
+ context 'when using deeply nested hashes' do
27
+ it 'updates values of hash' do
28
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
29
+ end
30
+
31
+ it 'works recursively when parameter is passed' do
32
+ expect(subject.public_send(method, recursive: true) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
33
+ end
34
+
35
+ it 'does not work recursively when parameter is passed as false' do
36
+ expect(subject.public_send(method, recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
37
+ end
38
+
39
+ it 'does not ignore hash when option is passed' do
40
+ expect(subject.public_send(method, skip_inner: false) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 12)
41
+ end
42
+
43
+ it 'ignore hash and work recursively when option is passed' do
44
+ expect(subject.public_send(method, skip_inner: true) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
45
+ end
46
+
47
+ it 'ignore hash and does not work recursively when option is passed' do
48
+ expect(subject.public_send(method, skip_inner: false, recursive: false) { |value| value.is_a?(Hash) ? value : value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
49
+ end
50
+ end
51
+
52
+ context 'when using deeply nested arrays' do
53
+ let(:subject) { { a: 1, b: 2, c: [{ d: 3 }, { e: { f: 4 } } ] } }
54
+
55
+ it 'goes recursivly true arrays' do
56
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
57
+ end
58
+
59
+ it 'does not work recursively when parameter is passed as false' do
60
+ expect(subject.public_send(method, recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }])
61
+ end
62
+
63
+ it 'does not ignore array when option is passed' do
64
+ expect(subject.public_send(method, skip_inner: false) { |value| value.is_a?(Array) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 12)
65
+ end
66
+
67
+ it 'ignores array when option is passed' do
68
+ expect(subject.public_send(method, skip_inner: true) { |value| value.is_a?(Array) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
69
+ end
70
+
71
+ it 'ignore hash and does not work recursively when option is passed' do
72
+ expect(subject.public_send(method, skip_inner: false, recursive: false) { |value| value.is_a?(Array) ? value : value + 1 }).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }])
73
+ end
74
+ end
75
+
76
+ context 'when using a nested extra class' do
77
+ let(:subject) { { a: 1, b: 2, c: Hash::ValueChanger::Dummy.new(3) } }
78
+
79
+ it 'goes perform the mapping with the extra class' do
80
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: 4)
81
+ end
82
+
83
+ context 'when class is an interactor' do
84
+ let(:subject) { { a: 1, b: 2, c: Hash::ValueChanger::DummyIteractor.new({ d: 3 }, { e: { f: 4 } }) } }
85
+
86
+ it 'goes through the iteractor' do
87
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
88
+ end
89
+ end
90
+
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: darthjee-core_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.6
5
+ platform: ruby
6
+ authors:
7
+ - Darthjee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 11.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 11.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-mocks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.99.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.99.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-nav
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.2.4
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.2.4
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.14.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.14.1
125
+ description:
126
+ email:
127
+ - darthjee@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - core_ext.gemspec
140
+ - lib/array.rb
141
+ - lib/array/hash_builder.rb
142
+ - lib/darthjee.rb
143
+ - lib/darthjee/core_ext.rb
144
+ - lib/darthjee/core_ext/version.rb
145
+ - lib/enumerable.rb
146
+ - lib/hash.rb
147
+ - lib/hash/deep_hash_constructor.rb
148
+ - lib/hash/key_changer.rb
149
+ - lib/hash/value_changer.rb
150
+ - lib/numeric.rb
151
+ - lib/symbol.rb
152
+ - spec/lib/array_spec.rb
153
+ - spec/lib/enumerable_spec.rb
154
+ - spec/lib/hash/deep_hash_constructor_spec.rb
155
+ - spec/lib/hash/key_changer_spec.rb
156
+ - spec/lib/hash_spec.rb
157
+ - spec/lib/numeric_spec.rb
158
+ - spec/lib/symbol_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/models/hash/value_changer/dummy.rb
161
+ - spec/support/models/hash/value_changer/dummy_iteractor.rb
162
+ - spec/support/shared_examples/array_random.rb
163
+ - spec/support/shared_examples/chain_fetch.rb
164
+ - spec/support/shared_examples/chain_hash_keys_changer.rb
165
+ - spec/support/shared_examples/clean.rb
166
+ - spec/support/shared_examples/expected.rb
167
+ - spec/support/shared_examples/hash_keys_changer.rb
168
+ - spec/support/shared_examples/keys_appender.rb
169
+ - spec/support/shared_examples/keys_camelizer.rb
170
+ - spec/support/shared_examples/keys_underscorer.rb
171
+ - spec/support/shared_examples/remap.rb
172
+ - spec/support/shared_examples/value_changer.rb
173
+ homepage:
174
+ licenses: []
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.6.8
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Core Extensions
196
+ test_files:
197
+ - spec/lib/array_spec.rb
198
+ - spec/lib/enumerable_spec.rb
199
+ - spec/lib/hash/deep_hash_constructor_spec.rb
200
+ - spec/lib/hash/key_changer_spec.rb
201
+ - spec/lib/hash_spec.rb
202
+ - spec/lib/numeric_spec.rb
203
+ - spec/lib/symbol_spec.rb
204
+ - spec/spec_helper.rb
205
+ - spec/support/models/hash/value_changer/dummy.rb
206
+ - spec/support/models/hash/value_changer/dummy_iteractor.rb
207
+ - spec/support/shared_examples/array_random.rb
208
+ - spec/support/shared_examples/chain_fetch.rb
209
+ - spec/support/shared_examples/chain_hash_keys_changer.rb
210
+ - spec/support/shared_examples/clean.rb
211
+ - spec/support/shared_examples/expected.rb
212
+ - spec/support/shared_examples/hash_keys_changer.rb
213
+ - spec/support/shared_examples/keys_appender.rb
214
+ - spec/support/shared_examples/keys_camelizer.rb
215
+ - spec/support/shared_examples/keys_underscorer.rb
216
+ - spec/support/shared_examples/remap.rb
217
+ - spec/support/shared_examples/value_changer.rb