darthjee-core_ext 1.5.6 → 1.6.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 +14 -0
- data/.rubocop.yml +17 -0
- data/.rubocop_todo.yml +12 -0
- data/Gemfile +2 -1
- data/Rakefile +2 -0
- data/core_ext.gemspec +6 -3
- data/lib/darthjee/core_ext/array/hash_builder.rb +21 -13
- data/lib/darthjee/core_ext/array.rb +4 -2
- data/lib/darthjee/core_ext/date.rb +2 -0
- data/lib/darthjee/core_ext/enumerable.rb +5 -3
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +33 -0
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +62 -60
- data/lib/darthjee/core_ext/hash/key_changer.rb +64 -54
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +31 -0
- data/lib/darthjee/core_ext/hash/squasher.rb +31 -0
- data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +21 -0
- data/lib/darthjee/core_ext/hash/value_changer.rb +48 -44
- data/lib/darthjee/core_ext/hash.rb +28 -58
- data/lib/darthjee/core_ext/math.rb +4 -2
- data/lib/darthjee/core_ext/numeric.rb +5 -3
- data/lib/darthjee/core_ext/object/default_value.rb +2 -1
- data/lib/darthjee/core_ext/object.rb +2 -0
- data/lib/darthjee/core_ext/symbol.rb +2 -0
- data/lib/darthjee/core_ext/time.rb +2 -0
- data/lib/darthjee/core_ext/version.rb +3 -1
- data/lib/darthjee/core_ext.rb +2 -0
- data/lib/darthjee.rb +2 -1
- data/spec/lib/array_spec.rb +27 -19
- data/spec/lib/date_spec.rb +2 -0
- data/spec/lib/enumerable_spec.rb +10 -8
- data/spec/lib/hash/chain_fetcher_spec.rb +11 -0
- data/spec/lib/hash/deep_hash_constructor_spec.rb +3 -1
- data/spec/lib/hash/key_changer_spec.rb +19 -5
- data/spec/lib/hash/keys_sorter_spec.rb +10 -0
- data/spec/lib/hash/squasher_spec.rb +9 -0
- data/spec/lib/hash/to_hash_mapper_spec.rb +10 -0
- data/spec/lib/hash_spec.rb +16 -46
- data/spec/lib/math_spec.rb +2 -0
- data/spec/lib/numeric_spec.rb +2 -0
- data/spec/lib/object/default_value_spe.rb +2 -0
- data/spec/lib/object_spec.rb +2 -0
- data/spec/lib/symbol_spec.rb +2 -0
- data/spec/lib/time_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -1
- data/spec/support/models/default_value.rb +2 -0
- data/spec/support/models/hash/value_changer/dummy.rb +19 -13
- data/spec/support/models/hash/value_changer/dummy_iteractor.rb +13 -8
- data/spec/support/shared_examples/{array_random.rb → array/array_random.rb} +5 -2
- data/spec/support/shared_examples/clean.rb +14 -2
- data/spec/support/shared_examples/date.rb +2 -0
- data/spec/support/shared_examples/expected.rb +2 -1
- data/spec/support/shared_examples/{chain_fetch.rb → hash/chain_fetch.rb} +9 -7
- data/spec/support/shared_examples/{chain_hash_keys_changer.rb → hash/chain_hash_keys_changer.rb} +14 -11
- data/spec/support/shared_examples/{hash_keys_changer.rb → hash/hash_keys_changer.rb} +7 -5
- data/spec/support/shared_examples/hash/hash_squasher.rb +23 -0
- data/spec/support/shared_examples/{hash_transpose.rb → hash/hash_transpose.rb} +8 -6
- data/spec/support/shared_examples/hash/keys_appender.rb +69 -0
- data/spec/support/shared_examples/{keys_camelizer.rb → hash/keys_camelizer.rb} +6 -4
- data/spec/support/shared_examples/hash/keys_sorter.rb +67 -0
- data/spec/support/shared_examples/{keys_underscorer.rb → hash/keys_underscorer.rb} +4 -3
- data/spec/support/shared_examples/{map_to_hash.rb → hash/map_to_hash.rb} +16 -16
- data/spec/support/shared_examples/{remap.rb → hash/remap.rb} +16 -13
- data/spec/support/shared_examples/hash/value_changer.rb +192 -0
- metadata +76 -30
- data/circle.yml +0 -10
- data/spec/support/shared_examples/keys_appender.rb +0 -43
- data/spec/support/shared_examples/value_changer.rb +0 -147
@@ -1,62 +1,66 @@
|
|
1
|
-
|
2
|
-
attr_accessor :options, :block
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
skip_inner: true
|
8
|
-
}.merge(options)
|
3
|
+
class Hash
|
4
|
+
class ValueChanger
|
5
|
+
attr_accessor :options, :block
|
9
6
|
|
10
|
-
|
11
|
-
|
7
|
+
def initialize(options, &block)
|
8
|
+
@options = {
|
9
|
+
recursive: true,
|
10
|
+
skip_inner: true
|
11
|
+
}.merge(options)
|
12
12
|
|
13
|
-
|
14
|
-
if object.respond_to?(:change_values)
|
15
|
-
change_hash(object)
|
16
|
-
elsif is_iterable?(object)
|
17
|
-
change_array(object)
|
13
|
+
@block = block
|
18
14
|
end
|
19
|
-
end
|
20
15
|
|
21
|
-
|
16
|
+
def change(object)
|
17
|
+
if object.respond_to?(:change_values)
|
18
|
+
change_hash(object)
|
19
|
+
elsif iterable?(object)
|
20
|
+
change_array(object)
|
21
|
+
end
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
private
|
25
|
+
|
26
|
+
def change_hash(original_hash)
|
27
|
+
original_hash.tap do |hash|
|
28
|
+
original_hash.each do |key, value|
|
29
|
+
value = new_value(value)
|
30
|
+
hash[key] = value
|
31
|
+
end
|
28
32
|
end
|
29
33
|
end
|
30
|
-
end
|
31
34
|
|
32
|
-
|
33
|
-
|
35
|
+
def change_array(array)
|
36
|
+
method = %w[map! map].find { |m| array.respond_to? m }
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
array.public_send(method) do |value|
|
39
|
+
if value.respond_to?(:change_values)
|
40
|
+
value.change_values(options, &block)
|
41
|
+
elsif iterable?(value)
|
42
|
+
change_array(value)
|
43
|
+
else
|
44
|
+
new_value(value)
|
45
|
+
end
|
42
46
|
end
|
43
47
|
end
|
44
|
-
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
def change_value?(value)
|
50
|
+
!iterable?(value) || !options[:skip_inner]
|
51
|
+
end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
def iterable?(value)
|
54
|
+
value.respond_to?(:each)
|
55
|
+
end
|
53
56
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
def new_value(value)
|
58
|
+
value = block.call(value) if change_value?(value)
|
59
|
+
apply_recursion?(value) ? change(value) : value
|
60
|
+
end
|
58
61
|
|
59
|
-
|
60
|
-
|
62
|
+
def apply_recursion?(value)
|
63
|
+
iterable?(value) && options[:recursive]
|
64
|
+
end
|
61
65
|
end
|
62
66
|
end
|
@@ -1,57 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Hash
|
2
|
-
autoload :ValueChanger,
|
4
|
+
autoload :ValueChanger, 'darthjee/core_ext/hash/value_changer'
|
3
5
|
autoload :DeepHashConstructor, 'darthjee/core_ext/hash/deep_hash_constructor'
|
4
|
-
autoload :KeyChanger,
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
if block_given?
|
10
|
-
value = value.fetch(keys.shift) do |*args|
|
11
|
-
missed_keys = keys
|
12
|
-
keys = []
|
13
|
-
yield(*(args + [missed_keys]))
|
14
|
-
end until keys.empty?
|
15
|
-
else
|
16
|
-
value = value.fetch(keys.shift) until keys.empty?
|
17
|
-
end
|
6
|
+
autoload :KeyChanger, 'darthjee/core_ext/hash/key_changer'
|
7
|
+
autoload :ChainFetcher, 'darthjee/core_ext/hash/chain_fetcher'
|
8
|
+
autoload :Squasher, 'darthjee/core_ext/hash/squasher'
|
9
|
+
autoload :ToHashMapper, 'darthjee/core_ext/hash/to_hash_mapper'
|
10
|
+
autoload :KeysSorter, 'darthjee/core_ext/hash/keys_sorter'
|
18
11
|
|
19
|
-
|
12
|
+
def chain_fetch(*keys, &block)
|
13
|
+
ChainFetcher.new(self, *keys, &block).fetch
|
20
14
|
end
|
21
15
|
|
22
16
|
def squash
|
23
|
-
|
24
|
-
each do |key, value|
|
25
|
-
if value.is_a? Hash
|
26
|
-
value.squash.each do |k, v|
|
27
|
-
new_key = [key, k].join('.')
|
28
|
-
hash[new_key] = v
|
29
|
-
end
|
30
|
-
else
|
31
|
-
hash[key] = value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
17
|
+
Squasher.squash(self)
|
35
18
|
end
|
36
19
|
|
37
|
-
def map_to_hash
|
38
|
-
|
39
|
-
each do |k, v|
|
40
|
-
hash[k] = yield(k, v)
|
41
|
-
end
|
42
|
-
end
|
20
|
+
def map_to_hash(&block)
|
21
|
+
ToHashMapper.new(self).map(&block)
|
43
22
|
end
|
44
23
|
|
45
24
|
def remap_keys(remap)
|
46
25
|
dup.remap_keys!(remap)
|
47
26
|
end
|
48
27
|
|
49
|
-
def remap_keys!(
|
50
|
-
|
51
|
-
remap.each do |o, n|
|
52
|
-
new_hash[n] = delete o
|
53
|
-
end
|
54
|
-
merge! new_hash
|
28
|
+
def remap_keys!(keys_map)
|
29
|
+
KeyChanger.new(self).remap(keys_map)
|
55
30
|
end
|
56
31
|
|
57
32
|
def lower_camelize_keys(options = {})
|
@@ -59,7 +34,7 @@ class Hash
|
|
59
34
|
end
|
60
35
|
|
61
36
|
def lower_camelize_keys!(options = {})
|
62
|
-
options = options.merge(
|
37
|
+
options = options.merge(uppercase_first_letter: false)
|
63
38
|
|
64
39
|
camelize_keys!(options)
|
65
40
|
end
|
@@ -125,7 +100,8 @@ class Hash
|
|
125
100
|
# recursive: true,
|
126
101
|
# type: :keep [keep, string, symbol] (key type to be returned)
|
127
102
|
# }
|
128
|
-
# ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_")
|
103
|
+
# ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_")
|
104
|
+
# # returns { :foo_a => 1, "foo_b"=> 2 }
|
129
105
|
def prepend_to_keys(str, options = {})
|
130
106
|
change_key_text(options) do |key|
|
131
107
|
"#{str}#{key}"
|
@@ -137,7 +113,8 @@ class Hash
|
|
137
113
|
# recursive: true,
|
138
114
|
# type: :keep [keep, string, symbol] (key type to be returned)
|
139
115
|
# }
|
140
|
-
# ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar")
|
116
|
+
# ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar")
|
117
|
+
# # returns { :a_bar => 1, "b_bar"=> 2 }
|
141
118
|
def append_to_keys(str, options = {})
|
142
119
|
change_key_text(options) do |key|
|
143
120
|
"#{key}#{str}"
|
@@ -148,17 +125,7 @@ class Hash
|
|
148
125
|
# options: { recursive: true }
|
149
126
|
# ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
|
150
127
|
def sort_keys(options = {})
|
151
|
-
options
|
152
|
-
recursive: true
|
153
|
-
}.merge(options)
|
154
|
-
|
155
|
-
{}.tap do |hash|
|
156
|
-
keys.sort.each do |key|
|
157
|
-
value = self[key]
|
158
|
-
hash[key] = value unless value.is_a?(Hash) && options[:recursive]
|
159
|
-
hash[key] = value.sort_keys(options) if value.is_a?(Hash) && options[:recursive]
|
160
|
-
end
|
161
|
-
end
|
128
|
+
Hash::KeysSorter.new(self, **options).sort
|
162
129
|
end
|
163
130
|
|
164
131
|
# creates a new hash with changes in its values
|
@@ -167,8 +134,10 @@ class Hash
|
|
167
134
|
# skip_hash:true
|
168
135
|
# }
|
169
136
|
# ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 }
|
170
|
-
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s }
|
171
|
-
#
|
137
|
+
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s }
|
138
|
+
# # returns { a:"1", b:"{ c=>1 }
|
139
|
+
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s }
|
140
|
+
# # returns { a:"1", b:{ c=>"1" } }
|
172
141
|
def change_values(options = {}, &block)
|
173
142
|
deep_dup.change_values!(options, &block)
|
174
143
|
end
|
@@ -183,7 +152,7 @@ class Hash
|
|
183
152
|
|
184
153
|
def transpose!
|
185
154
|
aux = dup
|
186
|
-
keys.each { |k|
|
155
|
+
keys.each { |k| delete(k) }
|
187
156
|
aux.each do |k, v|
|
188
157
|
self[v] = k
|
189
158
|
end
|
@@ -205,7 +174,8 @@ class Hash
|
|
205
174
|
# recursive: true,
|
206
175
|
# type: :keep [keep, string, symbol] (key type to be returned)
|
207
176
|
# }
|
208
|
-
# ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase }
|
177
|
+
# ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase }
|
178
|
+
# # returns { :A => 1, "B"=> 2 }
|
209
179
|
def change_key_text(options = {}, &block)
|
210
180
|
Hash::KeyChanger.new(self).change_text(options, &block)
|
211
181
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Math
|
2
4
|
def self.average(values)
|
3
|
-
values = values.map { |v| [v,1] }.to_h unless values.is_a?(Hash)
|
5
|
+
values = values.map { |v| [v, 1] }.to_h unless values.is_a?(Hash)
|
4
6
|
|
5
7
|
values.inject(0) do |sum, vals|
|
6
|
-
sum + vals.inject { |a,b| a * b }
|
8
|
+
sum + vals.inject { |a, b| a * b }
|
7
9
|
end / values.values.sum.to_f
|
8
10
|
end
|
9
11
|
end
|
data/lib/darthjee/core_ext.rb
CHANGED
data/lib/darthjee.rb
CHANGED
data/spec/lib/array_spec.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Array do
|
4
6
|
it_behaves_like 'an array with map_to_hash method'
|
5
7
|
|
6
8
|
describe '#mapk' do
|
7
|
-
let(:array) { [{a: { b: 1 }, b: 2}, {a: { b: 3 }, b: 4}] }
|
9
|
+
let(:array) { [{ a: { b: 1 }, b: 2 }, { a: { b: 3 }, b: 4 }] }
|
8
10
|
|
9
11
|
it 'maps using the keys given as arguments' do
|
10
|
-
expect(array.mapk(:a, :b)).to eq([
|
12
|
+
expect(array.mapk(:a, :b)).to eq([1, 3])
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -27,7 +29,7 @@ describe Array do
|
|
27
29
|
it 'does not change the array' do
|
28
30
|
expect do
|
29
31
|
result
|
30
|
-
end.not_to
|
32
|
+
end.not_to(change { array })
|
31
33
|
end
|
32
34
|
|
33
35
|
context 'when array is empty' do
|
@@ -45,7 +47,7 @@ describe Array do
|
|
45
47
|
end
|
46
48
|
|
47
49
|
context 'when array has only one element' do
|
48
|
-
let(:array) { [
|
50
|
+
let(:array) { [2] }
|
49
51
|
|
50
52
|
it do
|
51
53
|
expect do
|
@@ -82,11 +84,11 @@ describe Array do
|
|
82
84
|
end
|
83
85
|
|
84
86
|
describe '#chain_map' do
|
85
|
-
let(:array) { [
|
87
|
+
let(:array) { %i[a long_name sym] }
|
86
88
|
let(:mapped) { array.chain_map(:to_s, :size, :to_s) }
|
87
89
|
|
88
90
|
it 'calls each argument as method of the mapped result' do
|
89
|
-
expect(mapped).to eq([
|
91
|
+
expect(mapped).to eq(%w[1 9 3])
|
90
92
|
end
|
91
93
|
|
92
94
|
context 'when an extra block is given' do
|
@@ -97,14 +99,14 @@ describe Array do
|
|
97
99
|
end
|
98
100
|
|
99
101
|
it 'calls each argument as method of the mapped result' do
|
100
|
-
expect(mapped).to eq([
|
102
|
+
expect(mapped).to eq(['final: 1', 'final: 9', 'final: 3'])
|
101
103
|
end
|
102
104
|
end
|
103
105
|
end
|
104
106
|
|
105
107
|
describe '#as_hash' do
|
106
108
|
let(:array) { [1, 2, 3] }
|
107
|
-
let(:keys) { %w
|
109
|
+
let(:keys) { %w[a b c] }
|
108
110
|
let(:expected) { { 'a' => 1, 'b' => 2, 'c' => 3 } }
|
109
111
|
|
110
112
|
it 'creates a hash using the array as value and the argument as keys' do
|
@@ -112,24 +114,30 @@ describe Array do
|
|
112
114
|
end
|
113
115
|
|
114
116
|
context 'when there are more keys than values' do
|
115
|
-
let(:keys) { %w
|
116
|
-
let(:expected)
|
117
|
+
let(:keys) { %w[a b c d e f] }
|
118
|
+
let(:expected) do
|
119
|
+
{
|
120
|
+
'a' => 1,
|
121
|
+
'b' => 2,
|
122
|
+
'c' => 3,
|
123
|
+
'd' => nil,
|
124
|
+
'e' => nil,
|
125
|
+
'f' => nil
|
126
|
+
}
|
127
|
+
end
|
117
128
|
|
118
129
|
it 'creates a hash with nil values for the extra keys' do
|
119
130
|
expect(array.as_hash(keys)).to eq(expected)
|
120
131
|
end
|
121
132
|
|
122
|
-
it { expect { array.as_hash(keys) }.not_to
|
123
|
-
it { expect { array.as_hash(keys) }.not_to
|
133
|
+
it { expect { array.as_hash(keys) }.not_to(change { keys }) }
|
134
|
+
it { expect { array.as_hash(keys) }.not_to(change { array }) }
|
124
135
|
end
|
125
136
|
|
126
137
|
context 'when there are more values than keys' do
|
127
138
|
let(:array) { [1, 2, 3, 4, 5, 6, 7] }
|
128
139
|
|
129
140
|
it { expect { array.as_hash(keys) }.to raise_error(IndexError) }
|
130
|
-
|
131
|
-
it { expect { array.as_hash(keys) rescue nil }.not_to change { keys } }
|
132
|
-
it { expect { array.as_hash(keys) rescue nil }.not_to change { array } }
|
133
141
|
end
|
134
142
|
end
|
135
143
|
|
@@ -174,19 +182,19 @@ describe Array do
|
|
174
182
|
describe '#random' do
|
175
183
|
it_behaves_like 'a method that returns a random element', :random
|
176
184
|
|
177
|
-
let(:array) { [
|
185
|
+
let(:array) { [8, 4, 2] }
|
178
186
|
|
179
187
|
it 'removes an the returned element' do
|
180
188
|
expect do
|
181
189
|
array.random
|
182
|
-
end.not_to
|
190
|
+
end.not_to(change { array })
|
183
191
|
end
|
184
192
|
end
|
185
193
|
|
186
194
|
describe '#random!' do
|
187
195
|
it_behaves_like 'a method that returns a random element', :random!
|
188
196
|
|
189
|
-
let(:array) { [
|
197
|
+
let(:array) { [8, 4, 2] }
|
190
198
|
|
191
199
|
it 'removes an the returned element' do
|
192
200
|
expect do
|
@@ -196,7 +204,7 @@ describe Array do
|
|
196
204
|
end
|
197
205
|
|
198
206
|
describe '#map_and_select' do
|
199
|
-
let(:array) { [1, 2, 3, 4].map { |i| { value: i} } }
|
207
|
+
let(:array) { [1, 2, 3, 4].map { |i| { value: i } } }
|
200
208
|
let(:filtered) { array.map_and_select(&block) }
|
201
209
|
|
202
210
|
context 'when block returns nil' do
|
data/spec/lib/date_spec.rb
CHANGED
data/spec/lib/enumerable_spec.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
describe Enumerable do
|
2
4
|
describe '#clean!' do
|
3
5
|
it_behaves_like 'an array clean method', :clean!
|
4
6
|
it_behaves_like 'a hash clean method', :clean!
|
5
7
|
|
6
8
|
it 'changes the original hash' do
|
7
|
-
hash = { a: nil}
|
8
|
-
expect { hash.clean! }.to
|
9
|
+
hash = { a: nil }
|
10
|
+
expect { hash.clean! }.to(change { hash })
|
9
11
|
end
|
10
12
|
|
11
13
|
it 'changes original array' do
|
12
|
-
array = [{ a: nil}]
|
13
|
-
expect { array.clean! }.to
|
14
|
+
array = [{ a: nil }]
|
15
|
+
expect { array.clean! }.to(change { array })
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
@@ -19,13 +21,13 @@ describe Enumerable do
|
|
19
21
|
it_behaves_like 'a hash clean method', :clean
|
20
22
|
|
21
23
|
it 'does not change the original hash' do
|
22
|
-
hash = { a: nil}
|
23
|
-
expect { hash.clean }.not_to
|
24
|
+
hash = { a: nil }
|
25
|
+
expect { hash.clean }.not_to(change { hash })
|
24
26
|
end
|
25
27
|
|
26
28
|
it 'does not change the original array' do
|
27
|
-
array = [{ a: nil}]
|
28
|
-
expect { array.clean }.not_to
|
29
|
+
array = [{ a: nil }]
|
30
|
+
expect { array.clean }.not_to(change { array })
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Hash::ChainFetcher do
|
6
|
+
subject { described_class.new(hash, *keys, &block) }
|
7
|
+
|
8
|
+
it_behaves_like 'an object with capable of performing chain fetch' do
|
9
|
+
let(:result) { subject.fetch }
|
10
|
+
end
|
11
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Hash::DeepHashConstructor do
|
@@ -46,7 +48,7 @@ describe Hash::DeepHashConstructor do
|
|
46
48
|
{ 'name' => 'First person', 'age' => 22 },
|
47
49
|
{ 'name' => 'Second person', 'age' => 27 }
|
48
50
|
],
|
49
|
-
'device' => %w
|
51
|
+
'device' => %w[GEAR_LOCK GPS],
|
50
52
|
'zipCode' => '122345-123'
|
51
53
|
}
|
52
54
|
end
|
@@ -1,8 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Hash::KeyChanger do
|
4
6
|
let(:subject) { described_class.new(hash) }
|
5
7
|
|
8
|
+
describe '#remap_keys!' do
|
9
|
+
it_behaves_like 'a method that remaps the keys', :remap do
|
10
|
+
it 'changes the original hash' do
|
11
|
+
expect { result }.to(change { hash })
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
6
16
|
describe '#underscore_keys' do
|
7
17
|
let(:hash) { { keyUnderscore: 1 } }
|
8
18
|
|
@@ -14,7 +24,8 @@ describe Hash::KeyChanger do
|
|
14
24
|
let(:hash) { { keyUnderscore: { anotherKey: 1 } } }
|
15
25
|
|
16
26
|
it 'underscore all the keys' do
|
17
|
-
|
27
|
+
result = subject.underscore_keys
|
28
|
+
expect(result).to eq(key_underscore: { another_key: 1 })
|
18
29
|
end
|
19
30
|
end
|
20
31
|
|
@@ -22,7 +33,8 @@ describe Hash::KeyChanger do
|
|
22
33
|
let(:hash) { { keyUnderscore: [{ anotherKey: 1 }] } }
|
23
34
|
|
24
35
|
it 'underscore all the keys' do
|
25
|
-
|
36
|
+
result = { key_underscore: [{ another_key: 1 }] }
|
37
|
+
expect(subject.underscore_keys).to eq(result)
|
26
38
|
end
|
27
39
|
end
|
28
40
|
|
@@ -30,7 +42,7 @@ describe Hash::KeyChanger do
|
|
30
42
|
it 'underscore all the keys' do
|
31
43
|
expect do
|
32
44
|
subject.underscore_keys
|
33
|
-
end.to
|
45
|
+
end.to(change { hash })
|
34
46
|
end
|
35
47
|
end
|
36
48
|
|
@@ -39,7 +51,8 @@ describe Hash::KeyChanger do
|
|
39
51
|
let(:hash) { { keyUnderscore: { anotherKey: 1 } } }
|
40
52
|
|
41
53
|
it 'underscore all the keys' do
|
42
|
-
|
54
|
+
result = subject.underscore_keys(recursive: false)
|
55
|
+
expect(result).to eq(key_underscore: { anotherKey: 1 })
|
43
56
|
end
|
44
57
|
end
|
45
58
|
|
@@ -47,7 +60,8 @@ describe Hash::KeyChanger do
|
|
47
60
|
let(:hash) { { keyUnderscore: [{ anotherKey: 1 }] } }
|
48
61
|
|
49
62
|
it 'underscore all the keys' do
|
50
|
-
|
63
|
+
result = subject.underscore_keys(recursive: false)
|
64
|
+
expect(result).to eq(key_underscore: [{ anotherKey: 1 }])
|
51
65
|
end
|
52
66
|
end
|
53
67
|
end
|