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