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.
- checksums.yaml +4 -4
- data/.circleci/config.yml +7 -1
- data/.gitignore +2 -0
- data/.reek.yml +3 -0
- data/ARRAY_README.md +72 -15
- data/CLASS_README.md +154 -0
- data/DATE_README.md +19 -9
- data/Dockerfile +2 -5
- data/ENUMERABLE_README.md +154 -4
- data/HASH_README.md +276 -135
- data/MATH_README.md +14 -10
- data/OBJECT_README.md +18 -35
- data/README.md +8 -4
- data/Rakefile +1 -0
- data/SYMBOL_README.md +13 -18
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.yml +45 -3
- data/core_ext.gemspec +10 -8
- data/docker-compose.yml +15 -7
- data/lib/darthjee/core_ext/array.rb +17 -12
- data/lib/darthjee/core_ext/array/hash_builder.rb +20 -4
- data/lib/darthjee/core_ext/class.rb +28 -10
- data/lib/darthjee/core_ext/date.rb +1 -0
- data/lib/darthjee/core_ext/enumerable.rb +67 -30
- data/lib/darthjee/core_ext/hash.rb +4 -2
- data/lib/darthjee/core_ext/hash/cameliazable.rb +39 -8
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +17 -1
- data/lib/darthjee/core_ext/hash/changeable.rb +88 -0
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +127 -63
- data/lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb +112 -0
- data/lib/darthjee/core_ext/hash/key_changeable.rb +126 -54
- data/lib/darthjee/core_ext/hash/key_changer.rb +140 -40
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +42 -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/math.rb +37 -6
- data/lib/darthjee/core_ext/numeric.rb +10 -0
- data/lib/darthjee/core_ext/object.rb +29 -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 +391 -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/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 +66 -36
- data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +35 -0
- 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/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 +4 -12
- data/spec/lib/hash_spec.rb +92 -146
- data/spec/lib/object_spec.rb +32 -0
- data/spec/support/models/client.rb +22 -0
- data/spec/support/shared_examples/hash/deep_hash.rb +166 -0
- data/spec/support/shared_examples/hash/hash_squasher.rb +54 -9
- data/spec/support/shared_examples/hash/keys_sorter.rb +266 -6
- metadata +70 -8
- 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,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Darthjee::CoreExt::Hash::Transposeable do
|
4
|
+
describe 'yard' do
|
5
|
+
subject(:hash) do
|
6
|
+
{
|
7
|
+
key1: :value1,
|
8
|
+
key2: :value2
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#transpose' do
|
13
|
+
it 'transpose rows and keys' do
|
14
|
+
expect(hash.transpose).to eq(
|
15
|
+
value1: :key1,
|
16
|
+
value2: :key2
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
it do
|
21
|
+
expect { hash.transpose }.not_to(change { hash })
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#transpose' do
|
26
|
+
it 'transpose rows and keys' do
|
27
|
+
expect { hash.transpose! }.to change { hash }
|
28
|
+
.to(
|
29
|
+
value1: :key1,
|
30
|
+
value2: :key2
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -22,13 +22,15 @@ describe Hash do
|
|
22
22
|
context 'when key is not found' do
|
23
23
|
context 'when no block is given' do
|
24
24
|
it do
|
25
|
-
expect { hash.chain_fetch(:a, :c, :d) }
|
25
|
+
expect { hash.chain_fetch(:a, :c, :d) }
|
26
|
+
.to raise_error(KeyError)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
context 'when a block is given' do
|
30
31
|
it do
|
31
|
-
expect { hash.chain_fetch(:a, :c, :d) { 10 } }
|
32
|
+
expect { hash.chain_fetch(:a, :c, :d) { 10 } }
|
33
|
+
.not_to raise_error
|
32
34
|
end
|
33
35
|
|
34
36
|
it 'returns the result of the block' do
|
@@ -37,6 +39,15 @@ describe Hash do
|
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
42
|
+
|
43
|
+
describe '#append_to_keys' do
|
44
|
+
let(:hash) { { :a => 1, 'b' => 2 } }
|
45
|
+
|
46
|
+
it 'appends string to keys' do
|
47
|
+
expect(hash.prepend_to_keys('foo_'))
|
48
|
+
.to eq(:foo_a => 1, 'foo_b' => 2)
|
49
|
+
end
|
50
|
+
end
|
40
51
|
end
|
41
52
|
end
|
42
53
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Darthjee::CoreExt::Math do
|
4
|
+
describe 'yard' do
|
5
|
+
describe '.average' do
|
6
|
+
describe 'Average of array' do
|
7
|
+
let(:values) { [1, 2, 3, 4] }
|
8
|
+
|
9
|
+
it 'calculates the average' do
|
10
|
+
expect(Math.average(values)).to eq(2.5)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'Average of hash' do
|
15
|
+
let(:values) do
|
16
|
+
{
|
17
|
+
1 => 3,
|
18
|
+
7 => 1
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'calculates the average' do
|
23
|
+
expect(Math.average(values)).to eq(2.5)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Darthjee::CoreExt::Object do
|
4
|
+
describe 'yard' do
|
5
|
+
describe '#is_any?' do
|
6
|
+
subject(:object) { [1, 2, 3] }
|
7
|
+
|
8
|
+
context 'when none match' do
|
9
|
+
it do
|
10
|
+
expect(object).not_to be_is_any(Hash, Class)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when at least one of them matches' do
|
15
|
+
it do
|
16
|
+
expect(object).to be_is_any(Hash, Array)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when a module matches' do
|
21
|
+
it do
|
22
|
+
expect(object).to be_is_any(Enumerable, Class)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# rubocop: disable RSpec/PredicateMatcher
|
28
|
+
describe '#trueful?' do
|
29
|
+
describe 'Calling on nil object' do
|
30
|
+
it { expect(nil.trueful?).not_to be_truthy }
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'Calling on empty array' do
|
34
|
+
it { expect([].trueful?).to be_truthy }
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'Calling on empty hash' do
|
38
|
+
it { expect({}.trueful?).to be_truthy }
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'Calling on empty string' do
|
42
|
+
it { expect(''.trueful?).to be_truthy }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
# rubocop: enable RSpec/PredicateMatcher
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Darthjee::CoreExt::Symbol do
|
4
|
+
describe 'yard' do
|
5
|
+
describe '#camelize' do
|
6
|
+
it 'camelizes symbol' do
|
7
|
+
expect(:the_symbol.camelize).to eq(:TheSymbol)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#underscore' do
|
12
|
+
it 'underscores symbol' do
|
13
|
+
expect(:theSymbol.underscore).to eq(:the_symbol)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Darthjee::CoreExt::Hash::DeepHashConstructor::Setter do
|
6
|
+
subject(:setter) { described_class.new(hash, base_key) }
|
7
|
+
|
8
|
+
let(:hash) { {} }
|
9
|
+
let(:value) { 21 }
|
10
|
+
|
11
|
+
describe '#set' do
|
12
|
+
context 'when base_key does not have index' do
|
13
|
+
let(:base_key) { 'person' }
|
14
|
+
|
15
|
+
context 'when key is nil' do
|
16
|
+
let(:key) { nil }
|
17
|
+
|
18
|
+
it 'set value on base key' do
|
19
|
+
expect { setter.set(key, value) }
|
20
|
+
.to change { hash }
|
21
|
+
.from({})
|
22
|
+
.to(base_key => value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when key is present' do
|
27
|
+
let(:key) { 'age' }
|
28
|
+
|
29
|
+
it 'set value on inner hash' do
|
30
|
+
expect { setter.set(key, value) }
|
31
|
+
.to change { hash }
|
32
|
+
.from({})
|
33
|
+
.to(base_key => { key => value })
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when base_key has index' do
|
39
|
+
let(:base_key) { 'person[0]' }
|
40
|
+
|
41
|
+
context 'when key is nil' do
|
42
|
+
let(:key) { nil }
|
43
|
+
|
44
|
+
it 'set value on base key array' do
|
45
|
+
expect { setter.set(key, value) }
|
46
|
+
.to change { hash }
|
47
|
+
.from({})
|
48
|
+
.to('person' => [value])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when key is present' do
|
53
|
+
let(:key) { 'age' }
|
54
|
+
|
55
|
+
it 'set value on inner hash inside array' do
|
56
|
+
expect { setter.set(key, value) }
|
57
|
+
.to change { hash }
|
58
|
+
.from({})
|
59
|
+
.to('person' => [{ key => value }])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -3,167 +3,27 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Darthjee::CoreExt::Hash::DeepHashConstructor do
|
6
|
-
|
7
|
-
let(:deep_hash) { subject.deep_hash(hash) }
|
6
|
+
subject(:constructor) { described_class.new('.') }
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
'vehicle.fuel' => 'GASOLINE',
|
16
|
-
'vehicle.doors' => 4
|
17
|
-
}
|
18
|
-
end
|
19
|
-
|
20
|
-
let(:expected) do
|
21
|
-
{
|
22
|
-
'person' => { 'name' => 'Some name', 'age' => 22 },
|
23
|
-
'vehicle' => { 'fuel' => 'GASOLINE', 'doors' => 4 },
|
24
|
-
'status' => :success
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'build a deep hash' do
|
29
|
-
expect(deep_hash).to eq(expected)
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'with indexed keys' do
|
33
|
-
let(:hash) do
|
34
|
-
{
|
35
|
-
'person[0].name' => 'First person',
|
36
|
-
'person[0].age' => 22,
|
37
|
-
'person[1].name' => 'Second person',
|
38
|
-
'person[1].age' => 27,
|
39
|
-
'device[0]' => 'GEAR_LOCK',
|
40
|
-
'device[1]' => 'GPS',
|
41
|
-
'zipCode' => '122345-123'
|
42
|
-
}
|
43
|
-
end
|
44
|
-
|
45
|
-
let(:expected) do
|
46
|
-
{
|
47
|
-
'person' => [
|
48
|
-
{ 'name' => 'First person', 'age' => 22 },
|
49
|
-
{ 'name' => 'Second person', 'age' => 27 }
|
50
|
-
],
|
51
|
-
'device' => %w[GEAR_LOCK GPS],
|
52
|
-
'zipCode' => '122345-123'
|
53
|
-
}
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'build a deep hash with arrays' do
|
57
|
-
expect(deep_hash).to eq(expected)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'with a n level hash' do
|
62
|
-
let(:hash) do
|
63
|
-
{
|
64
|
-
'quote_request.personal.person.name' => 'Some name',
|
65
|
-
'quote_request.personal.person.age' => 22,
|
66
|
-
'quote_request.insurance.vehicle.fuel' => 'GASOLINE',
|
67
|
-
'quote_request.insurance.vehicle.doors' => 4,
|
68
|
-
'request.status' => :success,
|
69
|
-
'trials' => 3
|
70
|
-
}
|
71
|
-
end
|
72
|
-
|
73
|
-
let(:expected) do
|
74
|
-
{
|
75
|
-
'quote_request' => {
|
76
|
-
'personal' => {
|
77
|
-
'person' => { 'name' => 'Some name', 'age' => 22 }
|
78
|
-
},
|
79
|
-
'insurance' => {
|
80
|
-
'vehicle' => { 'fuel' => 'GASOLINE', 'doors' => 4 }
|
81
|
-
}
|
82
|
-
},
|
83
|
-
'request' => { 'status' => :success },
|
84
|
-
'trials' => 3
|
85
|
-
}
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'build a deep hash with arrays' do
|
89
|
-
expect(deep_hash).to eq(expected)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
context 'with a n level hash and arrays' do
|
94
|
-
let(:hash) do
|
95
|
-
{
|
96
|
-
'quote_request.personal.person[0].name' => 'Some name 1',
|
97
|
-
'quote_request.personal.person[0].age' => 22,
|
98
|
-
'quote_request.personal.person[1].name' => 'Some name 2',
|
99
|
-
'quote_request.personal.person[1].age' => 23,
|
100
|
-
'request[0].status.clazz' => String,
|
101
|
-
'request[1].status.clazz' => Integer,
|
102
|
-
'request[2].status.clazz' => Date,
|
103
|
-
'trials' => 3
|
104
|
-
}
|
105
|
-
end
|
106
|
-
|
107
|
-
let(:expected) do
|
108
|
-
{
|
109
|
-
'quote_request' => {
|
110
|
-
'personal' => {
|
111
|
-
'person' => [
|
112
|
-
{ 'name' => 'Some name 1', 'age' => 22 },
|
113
|
-
{ 'name' => 'Some name 2', 'age' => 23 }
|
114
|
-
]
|
115
|
-
}
|
116
|
-
},
|
117
|
-
'request' => [
|
118
|
-
{ 'status' => { 'clazz' => String } },
|
119
|
-
{ 'status' => { 'clazz' => Integer } },
|
120
|
-
{ 'status' => { 'clazz' => Date } }
|
121
|
-
],
|
122
|
-
'trials' => 3
|
123
|
-
}
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'build a deep hash with arrays' do
|
127
|
-
expect(deep_hash).to eq(expected)
|
128
|
-
end
|
129
|
-
end
|
8
|
+
let(:hash) do
|
9
|
+
{
|
10
|
+
'person.name' => 'John',
|
11
|
+
'person.age' => '22'
|
12
|
+
}
|
13
|
+
end
|
130
14
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
{
|
135
|
-
'person_name' => 'Some name',
|
136
|
-
'person_age' => 22,
|
137
|
-
'status' => :success,
|
138
|
-
'vehicle_fuel' => 'GASOLINE',
|
139
|
-
'vehicle_doors' => 4
|
140
|
-
}
|
141
|
-
end
|
15
|
+
describe '#deep_hash' do
|
16
|
+
it_behaves_like 'a method that returns a deep hash' do
|
17
|
+
subject(:constructor) { described_class.new(*args) }
|
142
18
|
|
143
|
-
|
144
|
-
expect(deep_hash).to eq(expected)
|
145
|
-
end
|
19
|
+
let(:result) { constructor.deep_hash(hash) }
|
146
20
|
end
|
147
21
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
}
|
154
|
-
end
|
155
|
-
|
156
|
-
let(:expected) do
|
157
|
-
{
|
158
|
-
'person' => {
|
159
|
-
'name' => { 'clazz' => String }
|
160
|
-
}
|
161
|
-
}
|
162
|
-
end
|
163
|
-
|
164
|
-
it 'build a deep hash with arrays' do
|
165
|
-
expect(deep_hash).to eq(expected)
|
166
|
-
end
|
22
|
+
it 'does changes hash' do
|
23
|
+
expect { constructor.deep_hash(hash) }
|
24
|
+
.to change { hash }
|
25
|
+
.from('person.name' => 'John', 'person.age' => '22')
|
26
|
+
.to('person' => { 'name' => 'John', 'age' => '22' })
|
167
27
|
end
|
168
28
|
end
|
169
29
|
end
|
@@ -3,9 +3,10 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Darthjee::CoreExt::Hash::KeysSorter do
|
6
|
-
|
7
|
-
subject { described_class.new(hash, **options) }
|
6
|
+
subject { described_class.new(hash, **options) }
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
let(:result) { subject.sort }
|
9
|
+
|
10
|
+
it_behaves_like 'a class with a keys sort method'
|
11
|
+
it_behaves_like 'a class with a keys sort method that changes original'
|
11
12
|
end
|
@@ -3,7 +3,13 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Darthjee::CoreExt::Hash::Squasher do
|
6
|
-
|
7
|
-
|
6
|
+
describe '#squash' do
|
7
|
+
it_behaves_like 'a method to squash a hash' do
|
8
|
+
let(:squashed) { described_class.new.squash(hash) }
|
9
|
+
end
|
10
|
+
|
11
|
+
it_behaves_like 'a method to squash a hash', '/' do
|
12
|
+
let(:squashed) { described_class.new('/').squash(hash) }
|
13
|
+
end
|
8
14
|
end
|
9
15
|
end
|