darthjee-core_ext 2.0.0 → 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 +59 -11
- data/.rubocop.yml +18 -1
- data/.rubocop_todo.yml +15 -8
- data/Dockerfile +19 -3
- data/Gemfile +13 -0
- data/Makefile +7 -0
- data/README.md +5 -1
- data/config/check_specs.yml +20 -0
- data/core_ext.gemspec +3 -13
- data/docker-compose.yml +9 -0
- data/lib/darthjee/core_ext/array/hash_builder.rb +2 -1
- data/lib/darthjee/core_ext/array.rb +5 -2
- data/lib/darthjee/core_ext/class.rb +1 -0
- data/lib/darthjee/core_ext/enumerable.rb +6 -5
- data/lib/darthjee/core_ext/hash/cameliazable.rb +13 -13
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +1 -0
- data/lib/darthjee/core_ext/hash/changeable.rb +4 -4
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +3 -2
- data/lib/darthjee/core_ext/hash/key_changeable.rb +15 -15
- data/lib/darthjee/core_ext/hash/key_changer.rb +12 -11
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +1 -0
- data/lib/darthjee/core_ext/hash/transformable.rb +1 -1
- data/lib/darthjee/core_ext/hash.rb +2 -2
- data/lib/darthjee/core_ext/numeric.rb +3 -2
- data/lib/darthjee/core_ext/object.rb +0 -2
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/spec/integration/readme/class_spec.rb +3 -3
- data/spec/integration/readme/hash_spec.rb +10 -1
- 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/hash/deep_hash_constructor_spec.rb +10 -10
- data/spec/integration/yard/darthjee/core_ext/hash/squasher_spec.rb +3 -3
- data/spec/integration/yard/darthjee/core_ext/hash/transformable_spec.rb +3 -3
- data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +1 -1
- data/spec/integration/yard/darthjee/core_ext/hash/value_changer_spec.rb +1 -1
- data/spec/lib/array_spec.rb +2 -1
- data/spec/lib/darthjee/core_ext/hash/value_changer_spec.rb +1 -1
- data/spec/lib/hash_spec.rb +1 -7
- data/spec/lib/symbol_spec.rb +2 -2
- data/spec/spec_helper.rb +4 -2
- data/spec/support/models/client.rb +2 -1
- 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/hash_keys_changer.rb +3 -3
- 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 +12 -222
@@ -55,8 +55,8 @@ module Darthjee
|
|
55
55
|
# hash.chain_fetch(:a, :c, :d) { 10 } # returns 10
|
56
56
|
# hash.chain_fetch(:a, :c, :d) { |key, _| key } # returns :c
|
57
57
|
# hash.chain_fetch(:a, :c, :d) { |_, missing| missing } # returns [:d]
|
58
|
-
def chain_fetch(*keys, &
|
59
|
-
::Hash::ChainFetcher.new(self, *keys, &
|
58
|
+
def chain_fetch(*keys, &)
|
59
|
+
::Hash::ChainFetcher.new(self, *keys, &).fetch
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -14,8 +14,9 @@ module Darthjee
|
|
14
14
|
# @example
|
15
15
|
# 10.percent_of(30) # returns 33.33333333333333
|
16
16
|
def percent_of(total)
|
17
|
-
return Float::INFINITY if total&.zero?
|
18
|
-
|
17
|
+
return Float::INFINITY if total.to_f&.zero?
|
18
|
+
|
19
|
+
(to_f / total) * 100.0
|
19
20
|
end
|
20
21
|
end
|
21
22
|
end
|
@@ -19,7 +19,7 @@ describe Class do
|
|
19
19
|
|
20
20
|
it 'returns the same instance' do
|
21
21
|
expect(instance.name)
|
22
|
-
.to
|
22
|
+
.to equal(other.name)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -44,7 +44,7 @@ describe Class do
|
|
44
44
|
|
45
45
|
it 'returns the same instance' do
|
46
46
|
expect(instance.name)
|
47
|
-
.to
|
47
|
+
.to equal(other.name)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -98,7 +98,7 @@ describe Class do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it 'returns the same instance for both methods' do
|
101
|
-
expect(instance.cars).to
|
101
|
+
expect(instance.cars).to equal(instance.houses)
|
102
102
|
end
|
103
103
|
|
104
104
|
context 'when setting cars' do
|
@@ -1,5 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
# frozen_string_literal: true
|
6
|
+
|
3
7
|
describe Hash do
|
4
8
|
describe 'readme' do
|
5
9
|
describe '#chain_fetch' do
|
@@ -357,9 +361,14 @@ describe Hash do
|
|
357
361
|
expect(hash.sort_keys.keys)
|
358
362
|
.to eq(%i[a b])
|
359
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
|
360
369
|
end
|
361
370
|
|
362
|
-
describe '#sort_keys' do
|
371
|
+
describe '#sort_keys!' do
|
363
372
|
subject(:hash) { { b: 1, a: 2 } }
|
364
373
|
|
365
374
|
it 'sort hash keys' do
|
@@ -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
|
@@ -10,10 +10,10 @@ describe Darthjee::CoreExt::Hash::DeepHashConstructor do
|
|
10
10
|
{
|
11
11
|
'account.person.name[0]' => 'John',
|
12
12
|
'account.person.name[1]' => 'Wick',
|
13
|
-
'account.person.age'
|
14
|
-
'account.number'
|
15
|
-
:'house.number'
|
16
|
-
:'house.zip'
|
13
|
+
'account.person.age' => 20,
|
14
|
+
'account.number' => '102030',
|
15
|
+
:'house.number' => 67,
|
16
|
+
:'house.zip' => 12_345
|
17
17
|
}
|
18
18
|
end
|
19
19
|
|
@@ -22,14 +22,14 @@ describe Darthjee::CoreExt::Hash::DeepHashConstructor do
|
|
22
22
|
{
|
23
23
|
'account' => {
|
24
24
|
'person' => {
|
25
|
-
'name'
|
26
|
-
'age'
|
25
|
+
'name' => %w[John Wick],
|
26
|
+
'age' => 20
|
27
27
|
},
|
28
28
|
'number' => '102030'
|
29
29
|
},
|
30
30
|
'house' => {
|
31
31
|
'number' => 67,
|
32
|
-
'zip'
|
32
|
+
'zip' => 12_345
|
33
33
|
}
|
34
34
|
}
|
35
35
|
end
|
@@ -46,12 +46,12 @@ describe Darthjee::CoreExt::Hash::DeepHashConstructor do
|
|
46
46
|
'account' => {
|
47
47
|
%w[person name[0]] => 'John',
|
48
48
|
%w[person name[1]] => 'Wick',
|
49
|
-
%w[person age]
|
50
|
-
%w[number]
|
49
|
+
%w[person age] => 20,
|
50
|
+
%w[number] => '102030'
|
51
51
|
},
|
52
52
|
'house' => {
|
53
53
|
%w[number] => 67,
|
54
|
-
%w[zip]
|
54
|
+
%w[zip] => 12_345
|
55
55
|
}
|
56
56
|
}
|
57
57
|
end
|
@@ -21,10 +21,10 @@ describe Darthjee::CoreExt::Hash::Squasher do
|
|
21
21
|
{
|
22
22
|
'person[0].name[0]' => 'John',
|
23
23
|
'person[0].name[1]' => 'Wick',
|
24
|
-
'person[0].age'
|
24
|
+
'person[0].age' => 22,
|
25
25
|
'person[1].name[0]' => 'John',
|
26
26
|
'person[1].name[1]' => 'Constantine',
|
27
|
-
'person[1].age'
|
27
|
+
'person[1].age' => 25
|
28
28
|
}
|
29
29
|
end
|
30
30
|
|
@@ -48,7 +48,7 @@ describe Darthjee::CoreExt::Hash::Squasher do
|
|
48
48
|
let(:expected) do
|
49
49
|
{
|
50
50
|
'person> name' => 'John',
|
51
|
-
'person> age'
|
51
|
+
'person> age' => 22
|
52
52
|
}
|
53
53
|
end
|
54
54
|
|
@@ -21,7 +21,7 @@ describe Hash do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
describe '#exclusive_merge' do
|
24
|
+
describe '#exclusive_merge!' do
|
25
25
|
subject(:hash) { { a: 1, b: 2, c: 3 } }
|
26
26
|
|
27
27
|
let(:other) { { b: 4, 'c' => 5, e: 6 } }
|
@@ -86,9 +86,9 @@ describe Hash do
|
|
86
86
|
subject(:hash) do
|
87
87
|
{
|
88
88
|
'person[0]_name_first' => 'John',
|
89
|
-
'person[0]_name_last'
|
89
|
+
'person[0]_name_last' => 'Doe',
|
90
90
|
'person[1]_name_first' => 'John',
|
91
|
-
'person[1]_name_last'
|
91
|
+
'person[1]_name_last' => 'Wick'
|
92
92
|
}
|
93
93
|
end
|
94
94
|
|
@@ -4,7 +4,7 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Darthjee::CoreExt::Hash::ValueChanger do
|
6
6
|
describe 'yard' do
|
7
|
-
subject(:changer) { described_class.new(options, &block) }
|
7
|
+
subject(:changer) { described_class.new(**options, &block) }
|
8
8
|
|
9
9
|
describe '#initialize' do
|
10
10
|
let(:options) { { recursive: false, skip_inner: false } }
|
data/spec/lib/array_spec.rb
CHANGED
@@ -201,6 +201,7 @@ describe Array do
|
|
201
201
|
end
|
202
202
|
|
203
203
|
it { expect(value).to eq('2') }
|
204
|
+
|
204
205
|
it 'calls the mapping only until it returns a valid value' do
|
205
206
|
expect(transformer).to have_received(:transform).exactly(2)
|
206
207
|
end
|
@@ -267,7 +268,7 @@ describe Array do
|
|
267
268
|
filtered
|
268
269
|
end
|
269
270
|
|
270
|
-
it { expect(filtered).to eq(array[1
|
271
|
+
it { expect(filtered).to eq(array[1..].map(&:to_s)) }
|
271
272
|
|
272
273
|
it 'calls the mapping only once per element' do
|
273
274
|
expect(transformer).to have_received(:transform).exactly(4)
|
data/spec/lib/hash_spec.rb
CHANGED
@@ -216,12 +216,6 @@ describe Hash do
|
|
216
216
|
it { expect(list).not_to be_empty }
|
217
217
|
end
|
218
218
|
|
219
|
-
context 'when mapping returns empty arrays' do
|
220
|
-
let(:block) { proc { [] } }
|
221
|
-
|
222
|
-
it { expect(list).not_to be_empty }
|
223
|
-
end
|
224
|
-
|
225
219
|
context 'when mapping returns empty strings' do
|
226
220
|
let(:block) { proc { '' } }
|
227
221
|
|
@@ -259,7 +253,7 @@ describe Hash do
|
|
259
253
|
list
|
260
254
|
end
|
261
255
|
|
262
|
-
it { expect(list).to eq(hash.values[1
|
256
|
+
it { expect(list).to eq(hash.values[1..].map(&:to_s)) }
|
263
257
|
|
264
258
|
it 'calls the mapping only once for each value' do
|
265
259
|
expect(transformer).to have_received(:transform).exactly(4)
|
data/spec/lib/symbol_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Symbol do
|
6
6
|
describe '#camelize' do
|
7
|
-
it { expect(:sym.camelize).to
|
7
|
+
it { expect(:sym.camelize).to be_a(described_class) }
|
8
8
|
|
9
9
|
context 'when called with upper option' do
|
10
10
|
it 'camelize the symbol' do
|
@@ -26,7 +26,7 @@ describe Symbol do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
describe '#underscore' do
|
29
|
-
it { expect(:symBol.underscore).to
|
29
|
+
it { expect(:symBol.underscore).to be_a(described_class) }
|
30
30
|
|
31
31
|
context 'when called with upper option' do
|
32
32
|
it 'underscore the symbol' do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'simplecov'
|
4
|
+
require 'simplecov_json_formatter'
|
4
5
|
|
5
|
-
SimpleCov.
|
6
|
+
SimpleCov.start do
|
6
7
|
add_filter '/spec/'
|
8
|
+
|
9
|
+
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter if ENV['COVERAGE_JSON'] == 'true'
|
7
10
|
end
|
8
11
|
|
9
|
-
SimpleCov.start 'gem'
|
10
12
|
require 'pry-nav'
|
11
13
|
require 'darthjee/core_ext'
|
12
14
|
|
@@ -5,11 +5,11 @@ class DummyIterator
|
|
5
5
|
@array = array
|
6
6
|
end
|
7
7
|
|
8
|
-
def map(
|
9
|
-
@array.map(
|
8
|
+
def map(*, &)
|
9
|
+
@array.map(*, &)
|
10
10
|
end
|
11
11
|
|
12
|
-
def each(
|
13
|
-
@array.each(
|
12
|
+
def each(*, &)
|
13
|
+
@array.each(*, &)
|
14
14
|
end
|
15
15
|
end
|
@@ -3,13 +3,12 @@
|
|
3
3
|
shared_examples 'a method that returns a random element' do |method|
|
4
4
|
let(:array) { [7, 5, 3] }
|
5
5
|
|
6
|
-
|
6
|
+
3.times do |index|
|
7
7
|
context "when random returns #{index}" do
|
8
8
|
let!(:expected) { array[index] }
|
9
9
|
|
10
10
|
before do
|
11
|
-
allow(Random).to receive(:rand)
|
12
|
-
.with(array.size) { index }
|
11
|
+
allow(Random).to receive(:rand).and_return(index)
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'returns the randomized index of the array' do
|
@@ -60,7 +60,7 @@ shared_examples 'a method that is able to chain change keys' do |method|
|
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'when options are given' do
|
63
|
-
let(:options) { { recursive:
|
63
|
+
let(:options) { { recursive: } }
|
64
64
|
|
65
65
|
context 'with recursion' do
|
66
66
|
let(:recursive) { true }
|
@@ -27,7 +27,7 @@ end
|
|
27
27
|
|
28
28
|
shared_examples 'a method that is able to change keys' do |method|
|
29
29
|
let(:foo_sym_transformation) do
|
30
|
-
hash.public_send(method) { |k| "foo_#{k}"
|
30
|
+
hash.public_send(method) { |k| :"foo_#{k}" }
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'with simple level hash' do
|
@@ -52,7 +52,7 @@ shared_examples 'a method that is able to change keys' do |method|
|
|
52
52
|
|
53
53
|
context 'with recursive hash' do
|
54
54
|
let(:hash) { { 'a' => 1, b: { c: 3, 'd' => 4 } } }
|
55
|
-
let(:result) { hash.public_send(method, options) { |k| "foo_#{k}" } }
|
55
|
+
let(:result) { hash.public_send(method, **options) { |k| "foo_#{k}" } }
|
56
56
|
let(:expected) do
|
57
57
|
{ 'foo_a' => 1, 'foo_b' => { 'foo_c' => 3, 'foo_d' => 4 } }
|
58
58
|
end
|
@@ -64,7 +64,7 @@ shared_examples 'a method that is able to change keys' do |method|
|
|
64
64
|
end
|
65
65
|
|
66
66
|
context 'when options are given' do
|
67
|
-
let(:options) { { recursive:
|
67
|
+
let(:options) { { recursive: } }
|
68
68
|
|
69
69
|
context 'with recursion' do
|
70
70
|
let(:recursive) { true }
|
@@ -4,7 +4,7 @@ shared_examples 'an array with map_to_hash method' do
|
|
4
4
|
describe '#map_to_hash' do
|
5
5
|
subject(:hash) { %w[word1 wooord2] }
|
6
6
|
|
7
|
-
let(:mapping_block) { proc
|
7
|
+
let(:mapping_block) { proc(&:length) }
|
8
8
|
let(:mapped) { hash.map_to_hash(&mapping_block) }
|
9
9
|
let(:expected) { { 'word1' => 5, 'wooord2' => 7 } }
|
10
10
|
|
@@ -57,18 +57,18 @@ shared_examples 'a method that remaps the keys' do |method|
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
context 'when remap
|
60
|
+
context 'when remap swap keys' do
|
61
61
|
let(:remap) { { b: :a, a: :b } }
|
62
62
|
|
63
|
-
it '
|
63
|
+
it 'swap keys' do
|
64
64
|
expect(result).to eq(b: 1, a: 2)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
-
context 'when remap has
|
68
|
+
context 'when remap has one of the keys swapped' do
|
69
69
|
let(:remap) { { a: :b, b: :c } }
|
70
70
|
|
71
|
-
it '
|
71
|
+
it 'swap just that key' do
|
72
72
|
expect(result).to eq(b: 1, c: 2)
|
73
73
|
end
|
74
74
|
end
|
@@ -103,7 +103,7 @@ shared_examples 'a method that change the hash values' do |method|
|
|
103
103
|
|
104
104
|
it 'ignore hash and does not work recursively when option is passed' do
|
105
105
|
options = { skip_inner: false, recursive: false }
|
106
|
-
result = hash.public_send(method, options) do |value|
|
106
|
+
result = hash.public_send(method, **options) do |value|
|
107
107
|
value.is_a?(Hash) ? value : value + 1
|
108
108
|
end
|
109
109
|
expect(result).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
|
@@ -142,7 +142,7 @@ shared_examples 'a method that change the hash values' do |method|
|
|
142
142
|
|
143
143
|
it 'ignore hash and does not work recursively when option is passed' do
|
144
144
|
options = { skip_inner: false, recursive: false }
|
145
|
-
result = hash.public_send(method, options) do |value|
|
145
|
+
result = hash.public_send(method, **options) do |value|
|
146
146
|
value.is_a?(Array) ? value : value + 1
|
147
147
|
end
|
148
148
|
expect(result).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }, 5])
|