darthjee-core_ext 1.7.4 → 2.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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +7 -1
  3. data/.gitignore +2 -0
  4. data/.reek.yml +3 -0
  5. data/ARRAY_README.md +72 -15
  6. data/CLASS_README.md +154 -0
  7. data/DATE_README.md +19 -9
  8. data/Dockerfile +2 -5
  9. data/ENUMERABLE_README.md +154 -4
  10. data/HASH_README.md +276 -135
  11. data/MATH_README.md +14 -10
  12. data/OBJECT_README.md +18 -35
  13. data/README.md +8 -4
  14. data/Rakefile +1 -0
  15. data/SYMBOL_README.md +13 -18
  16. data/config/rubycritc.rb +12 -0
  17. data/config/yardstick.yml +45 -3
  18. data/core_ext.gemspec +10 -8
  19. data/docker-compose.yml +15 -7
  20. data/lib/darthjee/core_ext/array.rb +17 -12
  21. data/lib/darthjee/core_ext/array/hash_builder.rb +20 -4
  22. data/lib/darthjee/core_ext/class.rb +28 -10
  23. data/lib/darthjee/core_ext/date.rb +1 -0
  24. data/lib/darthjee/core_ext/enumerable.rb +67 -30
  25. data/lib/darthjee/core_ext/hash.rb +4 -2
  26. data/lib/darthjee/core_ext/hash/cameliazable.rb +39 -8
  27. data/lib/darthjee/core_ext/hash/chain_fetcher.rb +17 -1
  28. data/lib/darthjee/core_ext/hash/changeable.rb +88 -0
  29. data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +127 -63
  30. data/lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb +112 -0
  31. data/lib/darthjee/core_ext/hash/key_changeable.rb +126 -54
  32. data/lib/darthjee/core_ext/hash/key_changer.rb +140 -40
  33. data/lib/darthjee/core_ext/hash/keys_sorter.rb +42 -6
  34. data/lib/darthjee/core_ext/hash/squasher.rb +131 -18
  35. data/lib/darthjee/core_ext/hash/transformable.rb +133 -41
  36. data/lib/darthjee/core_ext/hash/transposeable.rb +37 -8
  37. data/lib/darthjee/core_ext/hash/value_changer.rb +76 -36
  38. data/lib/darthjee/core_ext/math.rb +37 -6
  39. data/lib/darthjee/core_ext/numeric.rb +10 -0
  40. data/lib/darthjee/core_ext/object.rb +29 -0
  41. data/lib/darthjee/core_ext/symbol.rb +22 -2
  42. data/lib/darthjee/core_ext/version.rb +1 -1
  43. data/scripts/check_readme.sh +6 -0
  44. data/scripts/rubycritic.sh +10 -0
  45. data/spec/integration/readme/array_spec.rb +96 -0
  46. data/spec/integration/readme/class_spec.rb +123 -0
  47. data/spec/integration/readme/date_spec.rb +20 -0
  48. data/spec/integration/readme/enumerable_spec.rb +87 -0
  49. data/spec/integration/readme/hash_spec.rb +391 -0
  50. data/spec/integration/readme/math_spec.rb +31 -0
  51. data/spec/integration/readme/object_spec.rb +49 -0
  52. data/spec/integration/readme/symbol_spec.rb +29 -0
  53. data/spec/integration/yard/darthjee/core_ext/enumerable_spec.rb +24 -4
  54. data/spec/integration/yard/darthjee/core_ext/hash/cameliazable_spec.rb +11 -0
  55. data/spec/integration/yard/darthjee/core_ext/hash/changeable_spec.rb +68 -0
  56. data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +34 -0
  57. data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +65 -0
  58. data/spec/integration/yard/darthjee/core_ext/hash/key_changeable_spec.rb +8 -0
  59. data/spec/integration/yard/darthjee/core_ext/hash/key_changer_spec.rb +59 -0
  60. data/spec/integration/yard/darthjee/core_ext/hash/keys_sorter_spec.rb +25 -0
  61. data/spec/integration/yard/darthjee/core_ext/hash/squasher_spec.rb +60 -0
  62. data/spec/integration/yard/darthjee/core_ext/hash/transformable_spec.rb +66 -36
  63. data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +35 -0
  64. data/spec/integration/yard/darthjee/core_ext/hash_spec.rb +13 -2
  65. data/spec/integration/yard/darthjee/core_ext/math_spec.rb +28 -0
  66. data/spec/integration/yard/darthjee/core_ext/numeric_spec.rb +11 -0
  67. data/spec/integration/yard/darthjee/core_ext/object_spec.rb +47 -0
  68. data/spec/integration/yard/darthjee/core_ext/symbol_spec.rb +17 -0
  69. data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +64 -0
  70. data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +16 -156
  71. data/spec/lib/darthjee/core_ext/hash/keys_sorter_spec.rb +5 -4
  72. data/spec/lib/darthjee/core_ext/hash/squasher_spec.rb +8 -2
  73. data/spec/lib/darthjee/core_ext/hash/value_changer_spec.rb +4 -12
  74. data/spec/lib/hash_spec.rb +92 -146
  75. data/spec/lib/object_spec.rb +32 -0
  76. data/spec/support/models/client.rb +22 -0
  77. data/spec/support/shared_examples/hash/deep_hash.rb +166 -0
  78. data/spec/support/shared_examples/hash/hash_squasher.rb +54 -9
  79. data/spec/support/shared_examples/hash/keys_sorter.rb +266 -6
  80. metadata +70 -8
  81. data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +0 -25
  82. data/spec/lib/darthjee/core_ext/hash/to_hash_mapper_spec.rb +0 -11
@@ -0,0 +1,391 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Hash do
4
+ describe 'readme' do
5
+ describe '#chain_fetch' do
6
+ subject(:hash) { { a: { b: { c: { d: 10 } } } } }
7
+
8
+ it 'fetches value from hashes' do
9
+ expect(hash.chain_fetch(:a, :b, :c, :d)).to eq(10)
10
+ end
11
+
12
+ context 'when passing a block' do
13
+ let(:value) do
14
+ hash.chain_fetch(:a, :x, :y, :z) do |key, missed_keys|
15
+ "returned: #{key}\nmissed: [#{missed_keys.join(',')}]"
16
+ end
17
+ end
18
+
19
+ it 'return processing of the missed keys' do
20
+ expect(value).to eq("returned: x\nmissed: [y,z]")
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#change_values' do
26
+ subject(:hash) { { a: 1, b: [{ c: 2 }] } }
27
+
28
+ it 'changes the values of the hash' do
29
+ expect(hash.change_values { |v| (v + 1).to_s })
30
+ .to eq(a: '2', b: [{ c: '3' }])
31
+ end
32
+
33
+ context 'when setting recursive to false' do
34
+ it 'changes the values of the hash' do
35
+ expect(hash.change_values(recursive: false) { |v| (v + 1).to_s })
36
+ .to eq(a: '2', b: [{ c: 2 }])
37
+ end
38
+ end
39
+
40
+ context 'when skipping inner hash' do
41
+ subject(:hash) { { a: 1, b: [{ c: 2 }], d: { e: 3 } } }
42
+
43
+ let(:result) do
44
+ hash.change_values(skip_inner: false) do |value|
45
+ case value
46
+ when Integer
47
+ (value + 1).to_s
48
+ when described_class
49
+ value.to_s
50
+ else
51
+ value.class
52
+ end
53
+ end
54
+ end
55
+
56
+ it 'changes the values skipping inner' do
57
+ expect(result).to eq(a: '2', b: Array, d: '{:e=>3}')
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#change_values!' do
63
+ subject(:hash) { { a: 1, b: [{ c: 2 }] } }
64
+
65
+ it 'changes the values of the hash' do
66
+ expect { hash.change_values! { |v| (v + 1).to_s } }
67
+ .to change { hash }
68
+ .to(a: '2', b: [{ c: '3' }])
69
+ end
70
+
71
+ context 'when setting recursive to false' do
72
+ it 'changes the values of the hash' do
73
+ expect do
74
+ hash.change_values!(recursive: false) { |v| (v + 1).to_s }
75
+ end
76
+ .to change { hash }
77
+ .to(a: '2', b: [{ c: 2 }])
78
+ end
79
+ end
80
+
81
+ context 'when skipping inner hash' do
82
+ subject(:hash) { { a: 1, b: [{ c: 2 }], d: { e: 3 } } }
83
+
84
+ let(:result) do
85
+ hash.change_values!(skip_inner: false) do |value|
86
+ case value
87
+ when Integer
88
+ (value + 1).to_s
89
+ when described_class
90
+ value.to_s
91
+ else
92
+ value.class
93
+ end
94
+ end
95
+ end
96
+
97
+ it 'changes the values skipping inner' do
98
+ expect { result }
99
+ .to change { hash }
100
+ .to(a: '2', b: Array, d: '{:e=>3}')
101
+ end
102
+ end
103
+ end
104
+
105
+ describe '#camelize_keys' do
106
+ subject(:hash) { { ca_b: 1, k: [{ a_b: 1 }] } }
107
+
108
+ it 'camelize all keys' do
109
+ expect(hash.camelize_keys)
110
+ .to eq(CaB: 1, K: [{ AB: 1 }])
111
+ end
112
+
113
+ context 'when passing recursive false' do
114
+ it 'camelize all keys without recursion' do
115
+ expect(hash.camelize_keys(recursive: false))
116
+ .to eq(CaB: 1, K: [{ a_b: 1 }])
117
+ end
118
+ end
119
+
120
+ context 'when not upcasing first letter' do
121
+ it 'camelize all keys' do
122
+ expect(hash.camelize_keys(uppercase_first_letter: false))
123
+ .to eq(caB: 1, k: [{ aB: 1 }])
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '#camelize_keys!' do
129
+ subject(:hash) { { ca_b: 1, k: [{ a_b: 1 }] } }
130
+
131
+ it 'camelize all keys' do
132
+ expect { hash.camelize_keys! }
133
+ .to change { hash }
134
+ .to(CaB: 1, K: [{ AB: 1 }])
135
+ end
136
+
137
+ context 'when passing recursive false' do
138
+ it 'camelize all keys without recursion' do
139
+ expect { hash.camelize_keys!(recursive: false) }
140
+ .to change { hash }
141
+ .to(CaB: 1, K: [{ a_b: 1 }])
142
+ end
143
+ end
144
+
145
+ context 'when not upcasing first letter' do
146
+ it 'camelize all keys' do
147
+ expect { hash.camelize_keys!(uppercase_first_letter: false) }
148
+ .to change { hash }
149
+ .to(caB: 1, k: [{ aB: 1 }])
150
+ end
151
+ end
152
+ end
153
+
154
+ describe '#lower_camelize_keys' do
155
+ subject(:hash) { { ca_b: 1, k: [{ a_b: 1 }] } }
156
+
157
+ it 'camelize with lower case' do
158
+ expect(hash.lower_camelize_keys)
159
+ .to eq(caB: 1, k: [{ aB: 1 }])
160
+ end
161
+ end
162
+
163
+ describe '#lower_camelize_keys!' do
164
+ subject(:hash) { { ca_b: 1, k: [{ a_b: 1 }] } }
165
+
166
+ it 'underscores keys' do
167
+ expect { hash.lower_camelize_keys! }
168
+ .to change { hash }
169
+ .to(caB: 1, k: [{ aB: 1 }])
170
+ end
171
+ end
172
+
173
+ describe '#undescore_keys' do
174
+ subject(:hash) { { Ca_B: 1, 'kB' => [{ KeysHash: 1 }] } }
175
+
176
+ it 'underscores keys' do
177
+ expect(hash.underscore_keys)
178
+ .to eq(ca_b: 1, 'k_b' => [{ keys_hash: 1 }])
179
+ end
180
+ end
181
+
182
+ describe '#undescore_keys!' do
183
+ subject(:hash) { { Ca_B: 1, 'kB' => [{ KeysHash: 1 }] } }
184
+
185
+ it 'camelize with lower case' do
186
+ expect { hash.underscore_keys! }.to change { hash }
187
+ .to(ca_b: 1, 'k_b' => [{ keys_hash: 1 }])
188
+ end
189
+ end
190
+
191
+ describe '#chain_change_keys' do
192
+ subject(:hash) { { ca_b: 1 } }
193
+
194
+ it 'changes the keys in order' do
195
+ expect(hash.chain_change_keys(:to_s, :upcase, :to_sym))
196
+ .to eq(CA_B: 1)
197
+ end
198
+ end
199
+
200
+ describe '#chain_change_keys!' do
201
+ subject(:hash) { { ca_b: 1 } }
202
+
203
+ it 'changes the keys in order' do
204
+ expect do
205
+ hash.chain_change_keys!(:to_s, :upcase, :to_sym)
206
+ end
207
+ .to change { hash }
208
+ .to(CA_B: 1)
209
+ end
210
+ end
211
+
212
+ describe '#change_keys' do
213
+ subject(:hash) do
214
+ { ca_b: 1, k: [{ a_b: 1 }] }
215
+ end
216
+
217
+ it 'changes keys' do
218
+ expect(hash.change_keys { |k| k.to_s.upcase })
219
+ .to eq('CA_B' => 1, 'K' => [{ 'A_B' => 1 }])
220
+ end
221
+
222
+ context 'when passing recursive option false' do
223
+ it 'changes keys not recursively' do
224
+ expect(hash.change_keys(recursive: false) { |k| k.to_s.upcase })
225
+ .to eq('CA_B' => 1, 'K' => [{ a_b: 1 }])
226
+ end
227
+ end
228
+ end
229
+
230
+ describe '#change_keys!' do
231
+ subject(:hash) do
232
+ { ca_b: 1, k: [{ a_b: 1 }] }
233
+ end
234
+
235
+ it 'changes keys' do
236
+ expect { hash.change_keys! { |k| k.to_s.upcase } }
237
+ .to change { hash }
238
+ .to('CA_B' => 1, 'K' => [{ 'A_B' => 1 }])
239
+ end
240
+
241
+ context 'when passing recursive option false' do
242
+ it 'changes keys not recursively' do
243
+ expect do
244
+ hash.change_keys!(recursive: false) { |k| k.to_s.upcase }
245
+ end
246
+ .to change { hash }
247
+ .to('CA_B' => 1, 'K' => [{ a_b: 1 }])
248
+ end
249
+ end
250
+ end
251
+
252
+ describe 'prepend_to_keys' do
253
+ subject(:hash) { { key: 1 } }
254
+
255
+ it 'prepends string to keys' do
256
+ expect(hash.prepend_to_keys('scope:'))
257
+ .to eq('scope:key': 1)
258
+ end
259
+ end
260
+
261
+ describe 'append_to_keys' do
262
+ subject(:hash) { { key: 1 } }
263
+
264
+ it 'prepends string to keys' do
265
+ expect(hash.append_to_keys('s'))
266
+ .to eq(keys: 1)
267
+ end
268
+ end
269
+
270
+ describe 'map_to_hash' do
271
+ subject(:hash) { { a: 1, b: 2 } }
272
+
273
+ it 'mapscreating hash' do
274
+ expect(hash.map_to_hash { |k, v| "#{k}_#{v}" })
275
+ .to eq(a: 'a_1', b: 'b_2')
276
+ end
277
+ end
278
+
279
+ describe '#remap_keys' do
280
+ subject(:hash) { { a: 1, b: 2 } }
281
+
282
+ it 'remap the keys' do
283
+ expect(hash.remap_keys(a: :c, d: :e))
284
+ .to eq(c: 1, b: 2, e: nil)
285
+ end
286
+ end
287
+
288
+ describe '#remap_keys!' do
289
+ subject(:hash) { { a: 1, b: 2 } }
290
+
291
+ it 'remap the keys' do
292
+ expect { hash.remap_keys!(a: :c, d: :e) }
293
+ .to change { hash }
294
+ .to(c: 1, b: 2, e: nil)
295
+ end
296
+ end
297
+
298
+ describe 'exclusive_merge' do
299
+ subject(:hash) { { a: 1, b: 2 } }
300
+
301
+ it 'merges exclusively' do
302
+ expect(hash.exclusive_merge(b: 3, c: 4))
303
+ .to eq(a: 1, b: 3)
304
+ end
305
+ end
306
+
307
+ describe 'exclusive_merge!' do
308
+ subject(:hash) { { a: 1, b: 2 } }
309
+
310
+ it 'merges exclusively' do
311
+ expect { hash.exclusive_merge!(b: 3, c: 4) }
312
+ .to change { hash }
313
+ .to(a: 1, b: 3)
314
+ end
315
+ end
316
+
317
+ describe '#squash' do
318
+ subject(:hash) { { a: { b: [1, 2] } } }
319
+
320
+ it 'squash keys' do
321
+ expect(hash.squash)
322
+ .to eq('a.b[0]' => 1, 'a.b[1]' => 2)
323
+ end
324
+ end
325
+
326
+ describe '#squash!' do
327
+ subject(:hash) { { a: { b: [1, 2] } } }
328
+
329
+ it 'squash keys' do
330
+ expect { hash.squash! }.to change { hash }
331
+ .to('a.b[0]' => 1, 'a.b[1]' => 2)
332
+ end
333
+ end
334
+
335
+ describe '#to_deep_hash' do
336
+ subject(:hash) { { 'a.b[0]' => 1, 'a.b[1]' => 2 } }
337
+
338
+ it 'builds hash' do
339
+ expect(hash.to_deep_hash)
340
+ .to eq('a' => { 'b' => [1, 2] })
341
+ end
342
+ end
343
+
344
+ describe '#to_deep_hash!' do
345
+ subject(:hash) { { 'a.b[0]' => 1, 'a.b[1]' => 2 } }
346
+
347
+ it 'builds hash' do
348
+ expect { hash.to_deep_hash! }.to change { hash }
349
+ .to('a' => { 'b' => [1, 2] })
350
+ end
351
+ end
352
+
353
+ describe '#sort_keys' do
354
+ subject(:hash) { { b: 1, a: 2 } }
355
+
356
+ it 'sort hash keys' do
357
+ expect(hash.sort_keys.keys)
358
+ .to eq(%i[a b])
359
+ end
360
+ end
361
+
362
+ describe '#sort_keys' do
363
+ subject(:hash) { { b: 1, a: 2 } }
364
+
365
+ it 'sort hash keys' do
366
+ expect { hash.sort_keys! }
367
+ .to change(hash, :keys)
368
+ .to(%i[a b])
369
+ end
370
+ end
371
+
372
+ describe '#transpose' do
373
+ subject(:hash) { { a: 1, b: :a, c: [2, 3] } }
374
+
375
+ it 'transpose keys and values' do
376
+ expect(hash.transpose)
377
+ .to eq(1 => :a, a: :b, [2, 3] => :c)
378
+ end
379
+ end
380
+
381
+ describe '#transpose!' do
382
+ subject(:hash) { { a: 1, b: :a, c: [2, 3] } }
383
+
384
+ it 'transpose keys and values' do
385
+ expect { hash.transpose! }
386
+ .to change { hash }
387
+ .to(1 => :a, a: :b, [2, 3] => :c)
388
+ end
389
+ end
390
+ end
391
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Math do
6
+ describe 'readme' do
7
+ describe '.average' do
8
+ let(:values) { [0, 1, 2, 3, 4, 5] }
9
+
10
+ it 'calculates average' do
11
+ expect(described_class.average(values)).to eq(2.5)
12
+ end
13
+
14
+ context 'when giving a map' do
15
+ let(:values) do
16
+ {
17
+ 0 => 1,
18
+ 1 => 2,
19
+ 2 => 3,
20
+ 3 => 4,
21
+ 4 => 5
22
+ }
23
+ end
24
+
25
+ it 'calculates average' do
26
+ expect(described_class.average(values)).to eq(2.6666666666666666)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Object do
6
+ describe 'readme' do
7
+ # rubocop: disable RSpec/PredicateMatcher
8
+ describe '#is_any' do
9
+ context 'when is one of the classes' do
10
+ it do
11
+ expect(1.is_any?(String, Symbol, Numeric)).to be_truthy
12
+ end
13
+ end
14
+
15
+ context 'when is none of the classes' do
16
+ it do
17
+ expect(1.is_any?(String, Symbol)).to be_falsey
18
+ end
19
+ end
20
+ end
21
+
22
+ describe '#trueful?' do
23
+ context 'when it is a regular object' do
24
+ it { expect(described_class.new.trueful?).to be_truthy }
25
+ end
26
+
27
+ context 'when it is an empty string' do
28
+ it { expect(''.trueful?).to be_truthy }
29
+ end
30
+
31
+ context 'when it is an empty hash' do
32
+ it { expect({}.trueful?).to be_truthy }
33
+ end
34
+
35
+ context 'when it is an empty array' do
36
+ it { expect([].trueful?).to be_truthy }
37
+ end
38
+
39
+ context 'when it is 0' do
40
+ it { expect(0.trueful?).to be_truthy }
41
+ end
42
+
43
+ context 'when it is nil' do
44
+ it { expect(nil.trueful?).to be_falsey }
45
+ end
46
+ end
47
+ # rubocop: enable RSpec/PredicateMatcher
48
+ end
49
+ end