darthjee-core_ext 1.7.3 → 1.7.4
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 +19 -8
- data/.gitignore +1 -0
- data/.rubocop.yml +7 -0
- data/.rubocop_todo.yml +7 -3
- data/Dockerfile +9 -0
- data/README.md +5 -6
- data/Rakefile +2 -0
- data/config/yardstick.rb +13 -0
- data/config/yardstick.yml +35 -0
- data/core_ext.gemspec +9 -7
- data/docker-compose.yml +3 -6
- data/lib/darthjee/core_ext/array.rb +2 -2
- data/lib/darthjee/core_ext/class.rb +90 -4
- data/lib/darthjee/core_ext/enumerable.rb +5 -1
- data/lib/darthjee/core_ext/hash.rb +30 -0
- data/lib/darthjee/core_ext/hash/cameliazable.rb +91 -0
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +10 -0
- data/lib/darthjee/core_ext/hash/key_changeable.rb +85 -53
- data/lib/darthjee/core_ext/hash/key_changer.rb +41 -31
- data/lib/darthjee/core_ext/hash/value_changer.rb +128 -11
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/spec/integration/yard/{array → darthjee/core_ext/array}/hash_builder_spec.rb +3 -3
- data/spec/integration/yard/{array_spec.rb → darthjee/core_ext/array_spec.rb} +0 -0
- data/spec/integration/yard/darthjee/core_ext/class/default_value_spec.rb +143 -0
- data/spec/integration/yard/{date → darthjee/core_ext/date}/days_between_spec.rb +6 -6
- data/spec/integration/yard/{enumerable_spec.rb → darthjee/core_ext/enumerable_spec.rb} +0 -0
- data/spec/integration/yard/darthjee/core_ext/hash/cameliazable_spec.rb +34 -0
- data/spec/integration/yard/darthjee/core_ext/hash/chain_fetcher_spec.rb +51 -0
- data/spec/integration/yard/darthjee/core_ext/hash/key_changeable_spec.rb +48 -0
- data/spec/integration/yard/{hash → darthjee/core_ext/hash}/transformable_spec.rb +7 -3
- data/spec/integration/yard/darthjee/core_ext/hash/value_changer_spec.rb +66 -0
- data/spec/integration/yard/darthjee/core_ext/hash_spec.rb +42 -0
- data/spec/lib/array_spec.rb +27 -17
- data/spec/lib/class_spec.rb +108 -7
- data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +1 -1
- data/spec/lib/darthjee/core_ext/hash/key_changer_spec.rb +8 -8
- data/spec/lib/darthjee/core_ext/hash/keys_sorter_spec.rb +1 -0
- data/spec/lib/darthjee/core_ext/hash/to_hash_mapper_spec.rb +1 -0
- data/spec/lib/darthjee/core_ext/hash/value_changer_spec.rb +246 -0
- data/spec/lib/date_spec.rb +5 -4
- data/spec/lib/hash_spec.rb +45 -32
- data/spec/lib/math_spec.rb +1 -0
- data/spec/lib/numeric_spec.rb +39 -17
- data/spec/lib/object_spec.rb +7 -7
- data/spec/lib/symbol_spec.rb +2 -2
- data/spec/lib/time_spec.rb +5 -4
- data/spec/support/models/default_reader_model.rb +8 -0
- data/spec/support/models/default_value_model.rb +2 -0
- data/spec/support/models/dummy_iterator.rb +15 -0
- data/spec/support/models/dummy_transformer.rb +15 -0
- data/spec/support/shared_examples/array/array_random.rb +2 -1
- data/spec/support/shared_examples/clean.rb +20 -20
- data/spec/support/shared_examples/date.rb +18 -13
- data/spec/support/shared_examples/hash/chain_fetch.rb +4 -3
- data/spec/support/shared_examples/hash/chain_hash_keys_changer.rb +14 -7
- data/spec/support/shared_examples/hash/hash_keys_changer.rb +10 -4
- data/spec/support/shared_examples/hash/hash_transpose.rb +1 -1
- data/spec/support/shared_examples/hash/keys_appender.rb +14 -4
- data/spec/support/shared_examples/hash/keys_camelizer.rb +7 -7
- data/spec/support/shared_examples/hash/keys_sorter.rb +3 -3
- data/spec/support/shared_examples/hash/keys_underscorer.rb +2 -2
- data/spec/support/shared_examples/hash/map_to_hash.rb +14 -12
- data/spec/support/shared_examples/hash/remap.rb +4 -4
- data/spec/support/shared_examples/hash/value_changer.rb +40 -33
- metadata +70 -20
- data/spec/integration/yard/class/default_value_spec.rb +0 -58
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
shared_examples 'a hash clean method' do |method|
|
4
4
|
context 'when hash has one level' do
|
5
|
-
|
5
|
+
subject(:hash) do
|
6
6
|
{ a: 1, b: nil, c: '', d: {} }
|
7
7
|
end
|
8
8
|
|
@@ -11,12 +11,12 @@ shared_examples 'a hash clean method' do |method|
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'cleans the hash from empty and nil values' do
|
14
|
-
expect(
|
14
|
+
expect(hash.send(method)).to eq(expected)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
context 'when hash has two levels' do
|
19
|
-
|
19
|
+
subject(:hash) do
|
20
20
|
{ a: 1, c: '', d: { e: 1 }, f: { g: { b: nil } } }
|
21
21
|
end
|
22
22
|
|
@@ -25,12 +25,12 @@ shared_examples 'a hash clean method' do |method|
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'cleans the hash from empty and nil values' do
|
28
|
-
expect(
|
28
|
+
expect(hash.send(method)).to eq(expected)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
context 'when hash has many levels' do
|
33
|
-
|
33
|
+
subject(:hash) do
|
34
34
|
{
|
35
35
|
a: 1,
|
36
36
|
d: { e: { k: { l: { m: { n: 1 } } } } },
|
@@ -43,12 +43,12 @@ shared_examples 'a hash clean method' do |method|
|
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'cleans the hash from empty and nil values' do
|
46
|
-
expect(
|
46
|
+
expect(hash.send(method)).to eq(expected)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
context 'when hash has one nil value and one valid value' do
|
51
|
-
|
51
|
+
subject(:hash) do
|
52
52
|
{ a: { b: 1, c: nil } }
|
53
53
|
end
|
54
54
|
|
@@ -57,12 +57,12 @@ shared_examples 'a hash clean method' do |method|
|
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'cleans the hash from empty and nil values' do
|
60
|
-
expect(
|
60
|
+
expect(hash.send(method)).to eq(expected)
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
context 'when hash has arrays' do
|
65
|
-
|
65
|
+
subject(:hash) do
|
66
66
|
{ a: [] }
|
67
67
|
end
|
68
68
|
|
@@ -71,12 +71,12 @@ shared_examples 'a hash clean method' do |method|
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'cleans the hash from empty and nil values' do
|
74
|
-
expect(
|
74
|
+
expect(hash.send(method)).to eq(expected)
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
78
|
context 'when hash has arrays with hashes' do
|
79
|
-
|
79
|
+
subject(:hash) do
|
80
80
|
{ a: [{ c: nil }] }
|
81
81
|
end
|
82
82
|
|
@@ -85,12 +85,12 @@ shared_examples 'a hash clean method' do |method|
|
|
85
85
|
end
|
86
86
|
|
87
87
|
it 'cleans the hash from empty and nil values' do
|
88
|
-
expect(
|
88
|
+
expect(hash.send(method)).to eq(expected)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
92
|
context 'when hash has arrays with hashes with valid values' do
|
93
|
-
|
93
|
+
subject(:hash) do
|
94
94
|
{ a: [{ c: 1 }] }
|
95
95
|
end
|
96
96
|
|
@@ -99,12 +99,12 @@ shared_examples 'a hash clean method' do |method|
|
|
99
99
|
end
|
100
100
|
|
101
101
|
it 'cleans the hash from empty and nil values' do
|
102
|
-
expect(
|
102
|
+
expect(hash.send(method)).to eq(expected)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
106
|
context 'when hash has arrays with hashes with valid and invalid values' do
|
107
|
-
|
107
|
+
subject(:hash) do
|
108
108
|
{ a: [{ c: nil }, { d: 1 }] }
|
109
109
|
end
|
110
110
|
|
@@ -113,14 +113,14 @@ shared_examples 'a hash clean method' do |method|
|
|
113
113
|
end
|
114
114
|
|
115
115
|
it 'cleans the hash from empty and nil values' do
|
116
|
-
expect(
|
116
|
+
expect(hash.send(method)).to eq(expected)
|
117
117
|
end
|
118
118
|
end
|
119
119
|
end
|
120
120
|
|
121
121
|
shared_examples 'an array clean method' do |method|
|
122
122
|
context 'when array has one level' do
|
123
|
-
|
123
|
+
subject(:hash) do
|
124
124
|
[1, nil, '', {}, []]
|
125
125
|
end
|
126
126
|
|
@@ -129,12 +129,12 @@ shared_examples 'an array clean method' do |method|
|
|
129
129
|
end
|
130
130
|
|
131
131
|
it 'cleans the hash from empty and nil values' do
|
132
|
-
expect(
|
132
|
+
expect(hash.send(method)).to eq(expected)
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
136
136
|
context 'when array has many levels' do
|
137
|
-
|
137
|
+
subject(:hash) do
|
138
138
|
[
|
139
139
|
1,
|
140
140
|
nil,
|
@@ -149,7 +149,7 @@ shared_examples 'an array clean method' do |method|
|
|
149
149
|
end
|
150
150
|
|
151
151
|
it 'cleans the hash from empty and nil values' do
|
152
|
-
expect(
|
152
|
+
expect(hash.send(method)).to eq(expected)
|
153
153
|
end
|
154
154
|
end
|
155
155
|
end
|
@@ -1,31 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
shared_examples 'an object that knows how to calculate days between' do
|
4
|
-
let(:other_date) {
|
4
|
+
let(:other_date) { date.to_date + difference }
|
5
5
|
let(:difference) { 1.year }
|
6
6
|
|
7
7
|
context 'when other object is a date' do
|
8
8
|
context 'when other date is one year ahead' do
|
9
|
-
it { expect(
|
9
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
10
10
|
end
|
11
11
|
|
12
12
|
context 'when other date is one year behind' do
|
13
13
|
let(:difference) { - 1.year }
|
14
14
|
|
15
|
-
it { expect(
|
15
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
16
16
|
end
|
17
17
|
|
18
18
|
context 'when initial date is on a leap year' do
|
19
19
|
let(:year) { 2016 }
|
20
20
|
|
21
21
|
context 'when other date is one year ahead' do
|
22
|
-
it { expect(
|
22
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'when other date is one year behind' do
|
26
26
|
let(:difference) { - 1.year }
|
27
27
|
|
28
|
-
it { expect(
|
28
|
+
it { expect(date.days_between(other_date)).to eq(366) }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -33,43 +33,48 @@ shared_examples 'an object that knows how to calculate days between' do
|
|
33
33
|
let(:year) { 2015 }
|
34
34
|
|
35
35
|
context 'when other date is one year ahead' do
|
36
|
-
it { expect(
|
36
|
+
it { expect(date.days_between(other_date)).to eq(366) }
|
37
37
|
end
|
38
38
|
|
39
39
|
context 'when other date is one year behind' do
|
40
40
|
let(:difference) { - 1.year }
|
41
41
|
|
42
|
-
it { expect(
|
42
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context 'when other date is a time' do
|
48
|
-
let(:other_date) {
|
48
|
+
let(:other_date) { date.to_date.to_time + difference }
|
49
49
|
|
50
50
|
context 'when other date is one year and 10 hours ahead' do
|
51
51
|
let(:difference) { 1.year + 10.hours }
|
52
|
-
|
52
|
+
|
53
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
53
54
|
end
|
54
55
|
|
55
56
|
context 'when other date is one year and 25 hours ahead' do
|
56
57
|
let(:difference) { 1.year + 25.hours }
|
57
|
-
|
58
|
+
|
59
|
+
it { expect(date.days_between(other_date)).to eq(366) }
|
58
60
|
end
|
59
61
|
|
60
62
|
context 'when other date is one year and 23 hours ahead' do
|
61
63
|
let(:difference) { 1.year + 23.hours }
|
62
|
-
|
64
|
+
|
65
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
63
66
|
end
|
64
67
|
|
65
68
|
context 'when other date is one year and 10 hours behind' do
|
66
69
|
let(:difference) { - 1.year - 10.hours }
|
67
|
-
|
70
|
+
|
71
|
+
it { expect(date.days_between(other_date)).to eq(366) }
|
68
72
|
end
|
69
73
|
|
70
74
|
context 'when other date is almost one year behind (missing 10 hours)' do
|
71
75
|
let(:difference) { -1.year + 10.hours }
|
72
|
-
|
76
|
+
|
77
|
+
it { expect(date.days_between(other_date)).to eq(365) }
|
73
78
|
end
|
74
79
|
end
|
75
80
|
end
|
@@ -50,7 +50,7 @@ shared_examples 'an object with capable of performing chain fetch' do
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
context '
|
53
|
+
context 'when a default value block is given' do
|
54
54
|
let(:default_value) { 100 }
|
55
55
|
let(:block) { proc { default_value } }
|
56
56
|
|
@@ -58,7 +58,7 @@ shared_examples 'an object with capable of performing chain fetch' do
|
|
58
58
|
expect(result).to eq(default_value)
|
59
59
|
end
|
60
60
|
|
61
|
-
context '
|
61
|
+
context 'when the block logs the missing keys' do
|
62
62
|
it 'hnadles the missing keys' do
|
63
63
|
missing_keys = nil
|
64
64
|
hash.chain_fetch(*keys) do |_, keys|
|
@@ -69,8 +69,9 @@ shared_examples 'an object with capable of performing chain fetch' do
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
context '
|
72
|
+
context 'when the block uses the key for the return' do
|
73
73
|
let(:block) { proc { |k| "returned #{k}" } }
|
74
|
+
|
74
75
|
it 'hnadles the missing keys' do
|
75
76
|
expect(result).to eq('returned x')
|
76
77
|
end
|
@@ -5,7 +5,7 @@ shared_examples 'a class with chain_change_key method' do
|
|
5
5
|
{ 'a' => 1, b: 2, c: { d: 3, e: 4 }, f: [{ g: 5 }, { h: 6 }] }
|
6
6
|
end
|
7
7
|
|
8
|
-
describe
|
8
|
+
describe '#chain_change_keys' do
|
9
9
|
it_behaves_like 'a method that is able to chain change keys',
|
10
10
|
:chain_change_keys
|
11
11
|
it 'does not affects the original hash' do
|
@@ -15,7 +15,7 @@ shared_examples 'a class with chain_change_key method' do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
describe
|
18
|
+
describe '#ichain_change_keys!' do
|
19
19
|
it_behaves_like 'a method that is able to chain change keys',
|
20
20
|
:chain_change_keys!
|
21
21
|
|
@@ -28,8 +28,8 @@ shared_examples 'a class with chain_change_key method' do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
shared_examples 'a method that is able to chain change keys' do |method|
|
31
|
-
let(:result)
|
32
|
-
let(:options)
|
31
|
+
let(:result) { hash.public_send(method, *transformations, options) }
|
32
|
+
let(:options) { {} }
|
33
33
|
let(:transformations) { [:to_s] }
|
34
34
|
|
35
35
|
context 'with simple level hash' do
|
@@ -37,12 +37,14 @@ shared_examples 'a method that is able to chain change keys' do |method|
|
|
37
37
|
|
38
38
|
context 'with symbol transformation' do
|
39
39
|
let(:transformations) { [:to_sym] }
|
40
|
-
let(:expected)
|
40
|
+
let(:expected) { { a: 1, b: 2 } }
|
41
|
+
|
41
42
|
it_behaves_like 'result is as expected'
|
42
43
|
end
|
43
44
|
|
44
45
|
context 'with string transformation' do
|
45
46
|
let(:expected) { { 'a' => 1, 'b' => 2 } }
|
47
|
+
|
46
48
|
it_behaves_like 'result is as expected'
|
47
49
|
end
|
48
50
|
end
|
@@ -62,12 +64,14 @@ shared_examples 'a method that is able to chain change keys' do |method|
|
|
62
64
|
|
63
65
|
context 'with recursion' do
|
64
66
|
let(:recursive) { true }
|
67
|
+
|
65
68
|
it_behaves_like 'result is as expected'
|
66
69
|
end
|
67
70
|
|
68
71
|
context 'without recursion' do
|
69
72
|
let(:recursive) { false }
|
70
73
|
let(:expected) { { 'a' => 1, 'b' => { c: 3, 'd' => 4 } } }
|
74
|
+
|
71
75
|
it_behaves_like 'result is as expected'
|
72
76
|
end
|
73
77
|
end
|
@@ -78,14 +82,17 @@ shared_examples 'a method that is able to chain change keys' do |method|
|
|
78
82
|
let(:expected) do
|
79
83
|
{ 'a' => 1, 'b' => { 'c' => 2, 'd' => { 'e' => 3, 'f' => 4 } } }
|
80
84
|
end
|
85
|
+
|
81
86
|
it_behaves_like 'result is as expected'
|
82
87
|
end
|
83
88
|
|
84
|
-
context 'calling with chained transformations' do
|
89
|
+
context 'when calling with chained transformations' do
|
85
90
|
let(:transformations) { %i[to_s upcase to_sym] }
|
86
91
|
|
87
|
-
let(:
|
92
|
+
let(:expected) do
|
88
93
|
{ A: 1, B: 2, C: { D: 3, E: 4 }, F: [{ G: 5 }, { H: 6 }] }
|
89
94
|
end
|
95
|
+
|
96
|
+
it_behaves_like 'result is as expected'
|
90
97
|
end
|
91
98
|
end
|
@@ -5,7 +5,7 @@ shared_examples 'a class with change_key method' do
|
|
5
5
|
{ 'a' => 1, b: 2, c: { d: 3, e: 4 }, f: [{ g: 5 }, { h: 6 }] }
|
6
6
|
end
|
7
7
|
|
8
|
-
describe
|
8
|
+
describe '#change_keys' do
|
9
9
|
it_behaves_like 'a method that is able to change keys', :change_keys
|
10
10
|
it 'does not affects the original hash' do
|
11
11
|
expect do
|
@@ -14,7 +14,7 @@ shared_examples 'a class with change_key method' do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
describe
|
17
|
+
describe '#change_keys!' do
|
18
18
|
it_behaves_like 'a method that is able to change keys', :change_keys!
|
19
19
|
|
20
20
|
it 'affects the original hash' do
|
@@ -38,12 +38,14 @@ shared_examples 'a method that is able to change keys' do |method|
|
|
38
38
|
hash.public_send(method) { |k| "foo_#{k}" }
|
39
39
|
end
|
40
40
|
let(:expected) { { 'foo_a' => 1, 'foo_b' => 2 } }
|
41
|
+
|
41
42
|
it_behaves_like 'result is as expected'
|
42
43
|
end
|
43
44
|
|
44
45
|
context 'with symbol transformation' do
|
45
|
-
let(:result)
|
46
|
+
let(:result) { foo_sym_transformation }
|
46
47
|
let(:expected) { { foo_a: 1, foo_b: 2 } }
|
48
|
+
|
47
49
|
it_behaves_like 'result is as expected'
|
48
50
|
end
|
49
51
|
end
|
@@ -57,6 +59,7 @@ shared_examples 'a method that is able to change keys' do |method|
|
|
57
59
|
|
58
60
|
context 'when no options are given' do
|
59
61
|
let(:options) { {} }
|
62
|
+
|
60
63
|
it_behaves_like 'result is as expected'
|
61
64
|
end
|
62
65
|
|
@@ -65,12 +68,14 @@ shared_examples 'a method that is able to change keys' do |method|
|
|
65
68
|
|
66
69
|
context 'with recursion' do
|
67
70
|
let(:recursive) { true }
|
71
|
+
|
68
72
|
it_behaves_like 'result is as expected'
|
69
73
|
end
|
70
74
|
|
71
75
|
context 'without recursion' do
|
72
76
|
let(:recursive) { false }
|
73
|
-
let(:expected)
|
77
|
+
let(:expected) { { 'foo_a' => 1, 'foo_b' => { c: 3, 'd' => 4 } } }
|
78
|
+
|
74
79
|
it_behaves_like 'result is as expected'
|
75
80
|
end
|
76
81
|
end
|
@@ -82,6 +87,7 @@ shared_examples 'a method that is able to change keys' do |method|
|
|
82
87
|
{ foo_a: 1, foo_b: { foo_c: 2, foo_d: { foo_e: 3, foo_f: 4 } } }
|
83
88
|
end
|
84
89
|
let(:result) { foo_sym_transformation }
|
90
|
+
|
85
91
|
it_behaves_like 'result is as expected'
|
86
92
|
end
|
87
93
|
end
|
@@ -24,7 +24,7 @@ shared_examples 'a class with transpose method' do |method|
|
|
24
24
|
expect(hash.public_send(method).public_send(method)).to eq(hash)
|
25
25
|
end
|
26
26
|
|
27
|
-
context '
|
27
|
+
context 'when key is already a hash' do
|
28
28
|
let(:key) { { c: 2 } }
|
29
29
|
let(:hash) { { key => sub_hash } }
|
30
30
|
|
@@ -7,26 +7,31 @@ shared_examples 'a class with append_keys method' do
|
|
7
7
|
expected = { foo_a: 1, 'foo_b' => 2 }
|
8
8
|
expect(hash.prepend_to_keys('foo_')).to eq(expected)
|
9
9
|
end
|
10
|
+
|
10
11
|
it 'applies the block recursively' do
|
11
12
|
hash = { 'a' => 1, b: { c: 3, 'd' => 4 } }
|
12
13
|
expected = { 'foo_a' => 1, foo_b: { foo_c: 3, 'foo_d' => 4 } }
|
13
14
|
expect(hash.prepend_to_keys('foo_')).to eq(expected)
|
14
15
|
end
|
15
|
-
|
16
|
+
|
17
|
+
it 'changes type to string when type string option is passed' do
|
16
18
|
hash = { 'a' => 1, b: 2 }
|
17
19
|
expected = { 'foo_a' => 1, 'foo_b' => 2 }
|
18
20
|
expect(hash.prepend_to_keys('foo_', type: :string)).to eq(expected)
|
19
21
|
end
|
20
|
-
|
22
|
+
|
23
|
+
it 'changes type to symbol when type symbol option is passed' do
|
21
24
|
hash = { 'a' => 1, b: 2 }
|
22
25
|
expected = { foo_a: 1, foo_b: 2 }
|
23
26
|
expect(hash.prepend_to_keys('foo_', type: :symbol)).to eq(expected)
|
24
27
|
end
|
28
|
+
|
25
29
|
it 'keep type when type option is passed as keep' do
|
26
30
|
hash = { 'a' => 1, b: 2 }
|
27
31
|
expected = { 'foo_a' => 1, foo_b: 2 }
|
28
32
|
expect(hash.prepend_to_keys('foo_', type: :keep)).to eq(expected)
|
29
33
|
end
|
34
|
+
|
30
35
|
it 'applies to array as well' do
|
31
36
|
hash = { 'a' => 1, b: [{ c: 2 }, { d: 3 }] }
|
32
37
|
expected = { 'foo_a' => 1, foo_b: [{ foo_c: 2 }, { foo_d: 3 }] }
|
@@ -40,26 +45,31 @@ shared_examples 'a class with append_keys method' do
|
|
40
45
|
expected = { a_bar: 1, 'b_bar' => 2 }
|
41
46
|
expect(hash.append_to_keys('_bar')).to eq(expected)
|
42
47
|
end
|
48
|
+
|
43
49
|
it 'applies the block recursively' do
|
44
50
|
hash = { 'a' => 1, b: { c: 3, 'd' => 4 } }
|
45
51
|
expected = { 'a_bar' => 1, b_bar: { c_bar: 3, 'd_bar' => 4 } }
|
46
52
|
expect(hash.append_to_keys('_bar')).to eq(expected)
|
47
53
|
end
|
48
|
-
|
54
|
+
|
55
|
+
it 'changes type to string when type string option is passed' do
|
49
56
|
hash = { 'a' => 1, b: 2 }
|
50
57
|
expected = { 'a_bar' => 1, 'b_bar' => 2 }
|
51
58
|
expect(hash.append_to_keys('_bar', type: :string)).to eq(expected)
|
52
59
|
end
|
53
|
-
|
60
|
+
|
61
|
+
it 'changes type to symbol when type symbol option is passed' do
|
54
62
|
hash = { 'a' => 1, b: 2 }
|
55
63
|
expected = { a_bar: 1, b_bar: 2 }
|
56
64
|
expect(hash.append_to_keys('_bar', type: :symbol)).to eq(expected)
|
57
65
|
end
|
66
|
+
|
58
67
|
it 'keep type when type option is passed as keep' do
|
59
68
|
hash = { 'a' => 1, b: 2 }
|
60
69
|
expected = { 'a_bar' => 1, b_bar: 2 }
|
61
70
|
expect(hash.append_to_keys('_bar', type: :keep)).to eq(expected)
|
62
71
|
end
|
72
|
+
|
63
73
|
it 'applies to array as well' do
|
64
74
|
hash = { 'a' => 1, b: [{ c: 2 }, { d: 3 }] }
|
65
75
|
expected = { 'a_bar' => 1, b_bar: [{ c_bar: 2 }, { d_bar: 3 }] }
|