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,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Darthjee::CoreExt::Hash::DeepHashConstructor::Setter do
|
4
|
+
subject(:setter) { described_class.new(hash, base_key) }
|
5
|
+
|
6
|
+
let(:hash) { {} }
|
7
|
+
let(:value) { 21 }
|
8
|
+
|
9
|
+
describe 'yard' do
|
10
|
+
describe 'Simple usage' do
|
11
|
+
let(:base_key) { 'person' }
|
12
|
+
let(:key) { 'age' }
|
13
|
+
|
14
|
+
it 'set value on inner hash' do
|
15
|
+
expect { setter.set(key, value) }
|
16
|
+
.to change { hash }
|
17
|
+
.from({})
|
18
|
+
.to(base_key => { key => value })
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'with array index' do
|
23
|
+
let(:base_key) { 'person[0]' }
|
24
|
+
let(:key) { nil }
|
25
|
+
|
26
|
+
it 'set value on base key array' do
|
27
|
+
expect { setter.set(key, value) }
|
28
|
+
.to change { hash }
|
29
|
+
.from({})
|
30
|
+
.to('person' => [value])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Darthjee::CoreExt::Hash::DeepHashConstructor do
|
6
|
+
describe 'yard' do
|
7
|
+
subject(:constructor) { described_class.new('.') }
|
8
|
+
|
9
|
+
let(:hash) do
|
10
|
+
{
|
11
|
+
'account.person.name[0]' => 'John',
|
12
|
+
'account.person.name[1]' => 'Wick',
|
13
|
+
'account.person.age' => 20,
|
14
|
+
'account.number' => '102030',
|
15
|
+
:'house.number' => 67,
|
16
|
+
:'house.zip' => 12_345
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'general usage' do
|
21
|
+
let(:expected) do
|
22
|
+
{
|
23
|
+
'account' => {
|
24
|
+
'person' => {
|
25
|
+
'name' => %w[John Wick],
|
26
|
+
'age' => 20
|
27
|
+
},
|
28
|
+
'number' => '102030'
|
29
|
+
},
|
30
|
+
'house' => {
|
31
|
+
'number' => 67,
|
32
|
+
'zip' => 12_345
|
33
|
+
}
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'builds deep hash' do
|
38
|
+
expect(constructor.deep_hash(hash))
|
39
|
+
.to eq(expected)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#break_keys' do
|
44
|
+
let(:expected) do
|
45
|
+
{
|
46
|
+
'account' => {
|
47
|
+
%w[person name[0]] => 'John',
|
48
|
+
%w[person name[1]] => 'Wick',
|
49
|
+
%w[person age] => 20,
|
50
|
+
%w[number] => '102030'
|
51
|
+
},
|
52
|
+
'house' => {
|
53
|
+
%w[number] => 67,
|
54
|
+
%w[zip] => 12_345
|
55
|
+
}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'builds deep hash' do
|
60
|
+
expect(constructor.send(:break_keys, hash))
|
61
|
+
.to eq(expected)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -45,4 +45,12 @@ describe Darthjee::CoreExt::Hash::KeyChangeable do
|
|
45
45
|
expect(result).to eq('5': 1, '6': 2)
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
describe '#sort_keys' do
|
50
|
+
subject(:hash) { { b: 1, a: 2 } }
|
51
|
+
|
52
|
+
it 'sorts he keys' do
|
53
|
+
expect(hash.sort_keys).to eq(a: 2, b: 1)
|
54
|
+
end
|
55
|
+
end
|
48
56
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Darthjee::CoreExt::Hash::KeyChanger do
|
6
|
+
describe 'yard' do
|
7
|
+
subject(:changer) { described_class.new(hash) }
|
8
|
+
|
9
|
+
describe '#remap' do
|
10
|
+
let(:hash) { { a: 1, 'b' => 2 } }
|
11
|
+
|
12
|
+
let(:remap_map) { { a: :za, b: :zb, 'b' => 'yb' } }
|
13
|
+
|
14
|
+
it 'changes keys using the map' do
|
15
|
+
expect { changer.remap(remap_map) }
|
16
|
+
.to change { hash }.to(za: 1, 'yb' => 2, zb: nil)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#change_keys' do
|
21
|
+
let(:hash) { { a: 1, 'b' => { c: 3 } } }
|
22
|
+
let(:expected) do
|
23
|
+
{ 'key_a' => 1, 'key_b' => { 'key_c' => 3 } }
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'changes keys using block' do
|
27
|
+
expect { changer.change_keys { |k| "key_#{k}" } }
|
28
|
+
.to change { hash }.to(expected)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#camelize_keys' do
|
33
|
+
let(:hash) { { my_key: { inner_key: 10 } } }
|
34
|
+
|
35
|
+
it 'camelizes all keys' do
|
36
|
+
expect { changer.camelize_keys }
|
37
|
+
.to change { hash }.to(MyKey: { InnerKey: 10 })
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#underscore_keys' do
|
42
|
+
let(:hash) { { myKey: { InnerKey: 10 } } }
|
43
|
+
|
44
|
+
it 'camelizes all keys' do
|
45
|
+
expect { changer.underscore_keys }
|
46
|
+
.to change { hash }.to(my_key: { inner_key: 10 })
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#change_text' do
|
51
|
+
let(:hash) { { key: { inner_key: 10 } } }
|
52
|
+
|
53
|
+
it 'changes the text of the keys' do
|
54
|
+
expect { changer.change_text { |key| key.to_s.upcase } }
|
55
|
+
.to change { hash }.to(KEY: { INNER_KEY: 10 })
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Darthjee::CoreExt::Hash::KeysSorter do
|
6
|
+
subject(:sorter) { described_class.new(hash) }
|
7
|
+
|
8
|
+
describe 'yard' do
|
9
|
+
describe 'Simple Usage' do
|
10
|
+
let(:hash) { { key: 10, a_key: { z: 5, a: 10 } } }
|
11
|
+
|
12
|
+
it 'sorts the keys' do
|
13
|
+
expect(sorter.sort)
|
14
|
+
.to eq(a_key: { a: 10, z: 5 }, key: 10)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'changes the original hash' do
|
18
|
+
expect { sorter.sort }
|
19
|
+
.to change(hash, :keys)
|
20
|
+
.from(%i[key a_key])
|
21
|
+
.to(%i[a_key key])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe Darthjee::CoreExt::Hash::Squasher do
|
4
|
+
subject(:squasher) { described_class.new }
|
5
|
+
|
6
|
+
describe '#squash' do
|
7
|
+
describe 'Simple Usage' do
|
8
|
+
let(:hash) do
|
9
|
+
{
|
10
|
+
person: [{
|
11
|
+
name: %w[John Wick],
|
12
|
+
age: 22
|
13
|
+
}, {
|
14
|
+
name: %w[John Constantine],
|
15
|
+
age: 25
|
16
|
+
}]
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:expected) do
|
21
|
+
{
|
22
|
+
'person[0].name[0]' => 'John',
|
23
|
+
'person[0].name[1]' => 'Wick',
|
24
|
+
'person[0].age' => 22,
|
25
|
+
'person[1].name[0]' => 'John',
|
26
|
+
'person[1].name[1]' => 'Constantine',
|
27
|
+
'person[1].age' => 25
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'squashes the hash' do
|
32
|
+
expect(squasher.squash(hash)).to eq(expected)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'Passing custom joiner' do
|
37
|
+
subject(:squasher) { described_class.new('> ') }
|
38
|
+
|
39
|
+
let(:hash) do
|
40
|
+
{
|
41
|
+
person: {
|
42
|
+
name: 'John',
|
43
|
+
age: 22
|
44
|
+
}
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
let(:expected) do
|
49
|
+
{
|
50
|
+
'person> name' => 'John',
|
51
|
+
'person> age' => 22
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'squashes the hash' do
|
56
|
+
expect(squasher.squash(hash)).to eq(expected)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -10,7 +10,8 @@ describe Hash do
|
|
10
10
|
let(:other) { { b: 4, 'c' => 5, e: 6 } }
|
11
11
|
|
12
12
|
it 'merges only the existing keys' do
|
13
|
-
expect(hash.exclusive_merge(other))
|
13
|
+
expect(hash.exclusive_merge(other))
|
14
|
+
.to eq(a: 1, b: 4, c: 3)
|
14
15
|
end
|
15
16
|
|
16
17
|
it 'does not change original hash' do
|
@@ -20,13 +21,14 @@ describe Hash do
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
|
-
describe '#exclusive_merge' do
|
24
|
+
describe '#exclusive_merge!' do
|
24
25
|
subject(:hash) { { a: 1, b: 2, c: 3 } }
|
25
26
|
|
26
27
|
let(:other) { { b: 4, 'c' => 5, e: 6 } }
|
27
28
|
|
28
29
|
it 'merges only the existing keys' do
|
29
|
-
expect(hash.exclusive_merge!(other))
|
30
|
+
expect(hash.exclusive_merge!(other))
|
31
|
+
.to eq(a: 1, b: 4, c: 3)
|
30
32
|
end
|
31
33
|
|
32
34
|
it 'does not change original hash' do
|
@@ -37,63 +39,91 @@ describe Hash do
|
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
|
-
describe '#
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
hash.map_to_hash do |key, value|
|
45
|
-
"#{key}->#{value.size}"
|
42
|
+
describe '#squash' do
|
43
|
+
describe 'Simple Usage' do
|
44
|
+
subject(:hash) do
|
45
|
+
{ name: { first: 'John', last: 'Doe' } }
|
46
46
|
end
|
47
|
-
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
c: 'c->1'
|
54
|
-
)
|
48
|
+
it 'squash the hash into a one level hash' do
|
49
|
+
expect(hash.squash)
|
50
|
+
.to eq('name.first' => 'John', 'name.last' => 'Doe')
|
51
|
+
end
|
55
52
|
end
|
56
|
-
end
|
57
53
|
|
58
|
-
|
59
|
-
|
54
|
+
describe 'Reverting a #to_deep_hash call' do
|
55
|
+
let(:person) { person_data.to_deep_hash }
|
56
|
+
let(:person_data) do
|
57
|
+
{ 'person.name' => 'John', 'person.age' => 23 }
|
58
|
+
end
|
60
59
|
|
61
|
-
|
62
|
-
|
60
|
+
it 'is the reverse operation' do
|
61
|
+
expect(person.squash).to eq(person_data)
|
62
|
+
end
|
63
63
|
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
describe 'Giving a custom joiner' do
|
66
|
+
subject(:hash) do
|
67
|
+
{
|
68
|
+
links: {
|
69
|
+
home: '/',
|
70
|
+
products: '/products'
|
71
|
+
}
|
72
|
+
}
|
73
|
+
end
|
68
74
|
|
69
|
-
it '
|
70
|
-
expect(
|
75
|
+
it 'joins keys using custom joiner' do
|
76
|
+
expect(hash.squash('> ')).to eq(
|
77
|
+
'links> home' => '/',
|
78
|
+
'links> products' => '/products'
|
79
|
+
)
|
71
80
|
end
|
72
81
|
end
|
73
82
|
end
|
74
83
|
|
75
84
|
describe '#to_deep_hash' do
|
76
|
-
|
85
|
+
describe 'With custom separator' do
|
86
|
+
subject(:hash) do
|
87
|
+
{
|
88
|
+
'person[0]_name_first' => 'John',
|
89
|
+
'person[0]_name_last' => 'Doe',
|
90
|
+
'person[1]_name_first' => 'John',
|
91
|
+
'person[1]_name_last' => 'Wick'
|
92
|
+
}
|
93
|
+
end
|
77
94
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
95
|
+
let(:expected) do
|
96
|
+
{
|
97
|
+
'person' => [{
|
98
|
+
'name' => { 'first' => 'John', 'last' => 'Doe' }
|
99
|
+
}, {
|
100
|
+
'name' => { 'first' => 'John', 'last' => 'Wick' }
|
101
|
+
}]
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'with custom separator' do
|
106
|
+
expect(hash.to_deep_hash('_')).to eq(expected)
|
107
|
+
end
|
82
108
|
end
|
83
109
|
|
84
|
-
|
110
|
+
describe 'Reverting the result of a squash' do
|
85
111
|
let(:person) do
|
86
112
|
{
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
}
|
113
|
+
person: [{
|
114
|
+
name: %w[John Wick],
|
115
|
+
age: 22
|
116
|
+
}, {
|
117
|
+
name: %w[John Constantine],
|
118
|
+
age: 25
|
119
|
+
}]
|
91
120
|
}
|
92
121
|
end
|
93
122
|
let(:person_data) { person.squash }
|
94
123
|
|
95
124
|
it 'is the reverse operation' do
|
96
|
-
expect(person_data.to_deep_hash)
|
125
|
+
expect(person_data.to_deep_hash)
|
126
|
+
.to eq(person.deep_stringify_keys)
|
97
127
|
end
|
98
128
|
end
|
99
129
|
end
|
@@ -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
|
@@ -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 } }
|
@@ -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
|
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)
|
@@ -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
|