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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +14 -0
  3. data/.rubocop.yml +17 -0
  4. data/.rubocop_todo.yml +12 -0
  5. data/Gemfile +2 -1
  6. data/Rakefile +2 -0
  7. data/core_ext.gemspec +6 -3
  8. data/lib/darthjee/core_ext/array/hash_builder.rb +21 -13
  9. data/lib/darthjee/core_ext/array.rb +4 -2
  10. data/lib/darthjee/core_ext/date.rb +2 -0
  11. data/lib/darthjee/core_ext/enumerable.rb +5 -3
  12. data/lib/darthjee/core_ext/hash/chain_fetcher.rb +33 -0
  13. data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +62 -60
  14. data/lib/darthjee/core_ext/hash/key_changer.rb +64 -54
  15. data/lib/darthjee/core_ext/hash/keys_sorter.rb +31 -0
  16. data/lib/darthjee/core_ext/hash/squasher.rb +31 -0
  17. data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +21 -0
  18. data/lib/darthjee/core_ext/hash/value_changer.rb +48 -44
  19. data/lib/darthjee/core_ext/hash.rb +28 -58
  20. data/lib/darthjee/core_ext/math.rb +4 -2
  21. data/lib/darthjee/core_ext/numeric.rb +5 -3
  22. data/lib/darthjee/core_ext/object/default_value.rb +2 -1
  23. data/lib/darthjee/core_ext/object.rb +2 -0
  24. data/lib/darthjee/core_ext/symbol.rb +2 -0
  25. data/lib/darthjee/core_ext/time.rb +2 -0
  26. data/lib/darthjee/core_ext/version.rb +3 -1
  27. data/lib/darthjee/core_ext.rb +2 -0
  28. data/lib/darthjee.rb +2 -1
  29. data/spec/lib/array_spec.rb +27 -19
  30. data/spec/lib/date_spec.rb +2 -0
  31. data/spec/lib/enumerable_spec.rb +10 -8
  32. data/spec/lib/hash/chain_fetcher_spec.rb +11 -0
  33. data/spec/lib/hash/deep_hash_constructor_spec.rb +3 -1
  34. data/spec/lib/hash/key_changer_spec.rb +19 -5
  35. data/spec/lib/hash/keys_sorter_spec.rb +10 -0
  36. data/spec/lib/hash/squasher_spec.rb +9 -0
  37. data/spec/lib/hash/to_hash_mapper_spec.rb +10 -0
  38. data/spec/lib/hash_spec.rb +16 -46
  39. data/spec/lib/math_spec.rb +2 -0
  40. data/spec/lib/numeric_spec.rb +2 -0
  41. data/spec/lib/object/default_value_spe.rb +2 -0
  42. data/spec/lib/object_spec.rb +2 -0
  43. data/spec/lib/symbol_spec.rb +2 -0
  44. data/spec/lib/time_spec.rb +2 -0
  45. data/spec/spec_helper.rb +2 -1
  46. data/spec/support/models/default_value.rb +2 -0
  47. data/spec/support/models/hash/value_changer/dummy.rb +19 -13
  48. data/spec/support/models/hash/value_changer/dummy_iteractor.rb +13 -8
  49. data/spec/support/shared_examples/{array_random.rb → array/array_random.rb} +5 -2
  50. data/spec/support/shared_examples/clean.rb +14 -2
  51. data/spec/support/shared_examples/date.rb +2 -0
  52. data/spec/support/shared_examples/expected.rb +2 -1
  53. data/spec/support/shared_examples/{chain_fetch.rb → hash/chain_fetch.rb} +9 -7
  54. data/spec/support/shared_examples/{chain_hash_keys_changer.rb → hash/chain_hash_keys_changer.rb} +14 -11
  55. data/spec/support/shared_examples/{hash_keys_changer.rb → hash/hash_keys_changer.rb} +7 -5
  56. data/spec/support/shared_examples/hash/hash_squasher.rb +23 -0
  57. data/spec/support/shared_examples/{hash_transpose.rb → hash/hash_transpose.rb} +8 -6
  58. data/spec/support/shared_examples/hash/keys_appender.rb +69 -0
  59. data/spec/support/shared_examples/{keys_camelizer.rb → hash/keys_camelizer.rb} +6 -4
  60. data/spec/support/shared_examples/hash/keys_sorter.rb +67 -0
  61. data/spec/support/shared_examples/{keys_underscorer.rb → hash/keys_underscorer.rb} +4 -3
  62. data/spec/support/shared_examples/{map_to_hash.rb → hash/map_to_hash.rb} +16 -16
  63. data/spec/support/shared_examples/{remap.rb → hash/remap.rb} +16 -13
  64. data/spec/support/shared_examples/hash/value_changer.rb +192 -0
  65. metadata +76 -30
  66. data/circle.yml +0 -10
  67. data/spec/support/shared_examples/keys_appender.rb +0 -43
  68. data/spec/support/shared_examples/value_changer.rb +0 -147
@@ -1,62 +1,66 @@
1
- class Hash::ValueChanger
2
- attr_accessor :options, :block
1
+ # frozen_string_literal: true
3
2
 
4
- def initialize(options, &block)
5
- @options = {
6
- recursive: true,
7
- skip_inner: true
8
- }.merge(options)
3
+ class Hash
4
+ class ValueChanger
5
+ attr_accessor :options, :block
9
6
 
10
- @block = block
11
- end
7
+ def initialize(options, &block)
8
+ @options = {
9
+ recursive: true,
10
+ skip_inner: true
11
+ }.merge(options)
12
12
 
13
- def change(object)
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
- private
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
- def change_hash(original_hash)
24
- original_hash.tap do |hash|
25
- original_hash.each do |key, value|
26
- value = new_value(value)
27
- hash[key] = value
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
- def change_array(array)
33
- method = %w(map! map).find { |m| array.respond_to? m }
35
+ def change_array(array)
36
+ method = %w[map! map].find { |m| array.respond_to? m }
34
37
 
35
- array.public_send(method) do |value|
36
- if value.respond_to?(:change_values)
37
- value.change_values(options, &block)
38
- elsif is_iterable?(value)
39
- change_array(value)
40
- else
41
- new_value(value)
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
- def change_value?(value)
47
- !is_iterable?(value) || !options[:skip_inner]
48
- end
49
+ def change_value?(value)
50
+ !iterable?(value) || !options[:skip_inner]
51
+ end
49
52
 
50
- def is_iterable?(value)
51
- value.respond_to?(:each)
52
- end
53
+ def iterable?(value)
54
+ value.respond_to?(:each)
55
+ end
53
56
 
54
- def new_value(value)
55
- value = block.call(value) if change_value?(value)
56
- apply_recursion?(value) ? change(value) : value
57
- end
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
- def apply_recursion?(value)
60
- is_iterable?(value) && options[:recursive]
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, 'darthjee/core_ext/hash/value_changer'
4
+ autoload :ValueChanger, 'darthjee/core_ext/hash/value_changer'
3
5
  autoload :DeepHashConstructor, 'darthjee/core_ext/hash/deep_hash_constructor'
4
- autoload :KeyChanger, 'darthjee/core_ext/hash/key_changer'
5
-
6
- def chain_fetch(*keys)
7
- value = self
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
- value
12
+ def chain_fetch(*keys, &block)
13
+ ChainFetcher.new(self, *keys, &block).fetch
20
14
  end
21
15
 
22
16
  def squash
23
- {}.tap do |hash|
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
- {}.tap do |hash|
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!(remap)
50
- new_hash = {}
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({ uppercase_first_letter: false })
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_") == { :foo_a => 1, "foo_b"=> 2 }
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") == { :a_bar => 1, "b_bar"=> 2 }
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 } == { a:"1", b:"{ c=>1 }
171
- # ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s } == { a:"1", b:{ c=>"1" } }
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| self.delete(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 } == { :A => 1, "B"=> 2 }
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
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Numeric
2
- def percent_of(n)
3
- return Float::INFINITY if n == 0
4
- (to_f / n.to_f) * 100.0
4
+ def percent_of(total)
5
+ return Float::INFINITY if total&.zero?
6
+ (to_f / total.to_f) * 100.0
5
7
  end
6
8
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Object
2
4
  class << self
3
5
  private
@@ -13,4 +15,3 @@ class Object
13
15
  end
14
16
  end
15
17
  end
16
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'darthjee/core_ext/object/default_value'
2
4
 
3
5
  class Object
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Symbol
2
4
  def camelize(type = :upper)
3
5
  to_s.camelize(type).to_sym
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Time
2
4
  delegate :days_between, to: :to_date
3
5
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Darthjee
2
4
  module CoreExt
3
- VERSION = '1.5.6'
5
+ VERSION = '1.6.0'
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/all'
2
4
  require 'darthjee/core_ext/enumerable'
3
5
  require 'darthjee/core_ext/hash'
data/lib/darthjee.rb CHANGED
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Darthjee
2
4
  require 'darthjee/core_ext'
3
5
  end
4
-
@@ -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([ 1, 3 ])
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 change { array }
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) { [ 2 ] }
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) { [ :a, :long_name, :sym ] }
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([ '1', '9', '3' ])
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([ 'final: 1', 'final: 9', 'final: 3' ])
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(a b c) }
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(a b c d e f) }
116
- let(:expected) { { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => nil, 'e' => nil, 'f' => nil } }
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 change { keys } }
123
- it { expect { array.as_hash(keys) }.not_to change { array } }
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) { [ 8,4,2 ] }
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 change { array }
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) { [ 8,4,2 ] }
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Date do
@@ -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 change { hash }
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 change { array }
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 change { hash }
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 change { array }
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(GEAR_LOCK GPS),
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
- expect(subject.underscore_keys).to eq(key_underscore: { another_key: 1 })
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
- expect(subject.underscore_keys).to eq(key_underscore: [{ another_key: 1 }])
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 change { hash }
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
- expect(subject.underscore_keys(recursive: false)).to eq(key_underscore: { anotherKey: 1 })
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
- expect(subject.underscore_keys(recursive: false)).to eq(key_underscore: [{ anotherKey: 1 }])
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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Hash::KeysSorter do
6
+ it_behaves_like 'a class with a keys sort method' do
7
+ subject { described_class.new(hash, **options) }
8
+ let(:result) { subject.sort }
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Hash::Squasher do
6
+ it_behaves_like 'a class that has a method to squash a hash' do
7
+ let(:squashed) { described_class.squash(hash) }
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Hash::ToHashMapper do
6
+ it_behaves_like 'a hash with map_to_hash method' do
7
+ subject { described_class.new(hash) }
8
+ let(:mapped) { subject.map(&mapping_block) }
9
+ end
10
+ end