bidu-core_ext 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46ca9596bf1bd9395697f6d139e95529443dfeed
4
- data.tar.gz: 1f867d81d89c0da0a422bad9ec9ff241cb16c057
3
+ metadata.gz: 3aece23d84b190a26be718796ea4986a5a90b3ce
4
+ data.tar.gz: 41c946d5b73bfea41809ecfaec23ce6c827f1c1c
5
5
  SHA512:
6
- metadata.gz: 9cc16aa3de7626debf7b94218590db05eb5deefacd264a5dd1bf1cdea39f41e66e47df1e13b5b434ec3a1e02e345f07d890bda70d3cb149d494a39e408c24933
7
- data.tar.gz: 5c18491c6ae48e0fbed356615fcf1026b68585597167d9d7fa9eda3a4a1419ea32daa864b0aa31389dcbf3fe0e9647318a31c7e46837b25595365613fcc7a50e
6
+ metadata.gz: eb361421619c5377fc49cc859c152650715a6922d0f11aec782c6f18ca4afd5b271ce6f415b6efcba2a2679ad047cf4840fee2ef725d09617c72faf32df80e87
7
+ data.tar.gz: 8d42a46be5315f9c21bb5530f81374394c1e3bca46ffdc905f3073c7d88761807deb2595195415a0eeeebf15469c4c98db0b67cbabf0d611c0a7113c7d8e8b47
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  coverage
2
+ pkg
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bidu-core_ext (1.2.4)
4
+ bidu-core_ext (1.2.5)
5
5
  activesupport (~> 5.1.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -4,7 +4,7 @@ Core_Ext
4
4
  This project adds some new methods to the core ruby classes
5
5
 
6
6
  ## Array
7
- ###map_to_hash
7
+ ### map_to_hash
8
8
  map returning a hash with the original array for keys
9
9
 
10
10
  ```ruby
@@ -40,7 +40,7 @@ returns
40
40
  ```
41
41
 
42
42
  ## Hash
43
- ###map_to_hash
43
+ ### map_to_hash
44
44
  map returning a hash with the original keys
45
45
 
46
46
  ```ruby
@@ -62,7 +62,7 @@ h.chain_fetch(:a, :x, :y, :z) { |key, missed_keys| "returned #{key}" }
62
62
  'returned x'
63
63
  ```
64
64
 
65
- ###squash
65
+ ### squash
66
66
  Squash a deep hash into a simple level hash
67
67
 
68
68
  ```ruby
@@ -106,7 +106,7 @@ returns
106
106
  { 'CA_B' => 1 }
107
107
  ```
108
108
 
109
- ###chain_change_keys
109
+ ### chain_change_keys
110
110
  Change the hash keys usin a chained method call
111
111
 
112
112
  ```ruby
@@ -1,5 +1,5 @@
1
1
  module Bidu
2
2
  module CoreExt
3
- VERSION = '1.2.4'
3
+ VERSION = '1.2.5'
4
4
  end
5
5
  end
@@ -11,9 +11,9 @@ class Hash::ValueChanger
11
11
  end
12
12
 
13
13
  def change(object)
14
- if object.is_a? Hash
14
+ if object.respond_to?(:change_values)
15
15
  change_hash(object)
16
- elsif object.is_a? Array
16
+ elsif is_iterable?(object)
17
17
  change_array(object)
18
18
  end
19
19
  end
@@ -30,15 +30,24 @@ class Hash::ValueChanger
30
30
  end
31
31
 
32
32
  def change_array(array)
33
+ array = array.to_a
34
+
33
35
  array.each.with_index do |value, index|
34
- value = value.change_values(options, &block) if value.is_a? Hash
35
- value = change_array(value) if value.is_a? Array
36
+ if value.respond_to?(:change_values)
37
+ value = value.change_values(options, &block)
38
+ elsif is_iterable?(value)
39
+ value = change_array(value)
40
+ end
36
41
  array[index] = value
37
42
  end
38
43
  end
39
44
 
40
45
  def change_value?(value)
41
- !(value.is_a?(Hash) || value.is_a?(Array)) || !options[:skip_inner]
46
+ !is_iterable?(value) || !options[:skip_inner]
47
+ end
48
+
49
+ def is_iterable?(value)
50
+ value.respond_to?(:each)
42
51
  end
43
52
 
44
53
  def new_value(value)
@@ -47,6 +56,6 @@ class Hash::ValueChanger
47
56
  end
48
57
 
49
58
  def apply_recursion?(value)
50
- (value.is_a?(Hash) || value.is_a?(Array)) && options[:recursive]
59
+ is_iterable?(value) && options[:recursive]
51
60
  end
52
61
  end
@@ -0,0 +1,15 @@
1
+ class Hash::ValueChanger::Dummy
2
+ attr_reader :value
3
+
4
+ delegate :+, to: :value
5
+
6
+ def initialize(value)
7
+ @value = value
8
+ end
9
+
10
+ def eql?(other)
11
+ return true if equals?(other)
12
+ return false unless other.is_a?(self.class)
13
+ a.value == value
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ class Hash::ValueChanger::DummyIteractor
2
+ def initialize(*array)
3
+ @array = array
4
+ end
5
+
6
+ delegate :each, :to_a, to: :array
7
+
8
+ private
9
+
10
+ attr_reader :array
11
+ end
12
+
@@ -5,17 +5,9 @@ shared_examples 'a class with change_values method' do
5
5
  it_behaves_like 'a method that change the hash values', :change_values
6
6
 
7
7
  it 'does not change original hash' do
8
- subject.change_values { |value| value + 1 }
9
- expect(subject).to eq(a: 1, b: 2, c: { d: 3, e: 4 })
10
- end
11
-
12
- it 'should call change_values!' do
13
- original = { 'a' => 1, c: { d: 3, e: 4 } }
14
- copy = { 'a' => 1, c: { d: 3, e: 4 } }
15
-
16
- expect(original).to receive(:deep_dup).and_return(copy)
17
- expect(copy).to receive(:change_values!)
18
- original.change_values { |value| value + 1 }
8
+ expect do
9
+ subject.change_values { |value| value + 1 }
10
+ end.not_to change { subject }
19
11
  end
20
12
  end
21
13
 
@@ -23,41 +15,78 @@ shared_examples 'a class with change_values method' do
23
15
  it_behaves_like 'a method that change the hash values', :change_values!
24
16
 
25
17
  it 'changes original hash' do
26
- subject.change_values! { |value| value + 1 }
27
-
28
- expect(subject).to_not eq(a: 1, b: 2, c: { d: 3, e: 4 })
29
- expect(subject).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
18
+ expect do
19
+ subject.change_values! { |value| value + 1 }
20
+ end.to change { subject }
30
21
  end
31
22
  end
32
23
  end
33
24
 
34
25
  shared_examples 'a method that change the hash values' do |method|
35
- it 'updates values of hash' do
36
- expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
37
- end
26
+ context 'when using deeply nested hashes' do
27
+ it 'updates values of hash' do
28
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
29
+ end
38
30
 
39
- it 'works recursively when parameter is passed' do
40
- expect(subject.change_values(recursive: true) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
41
- end
31
+ it 'works recursively when parameter is passed' do
32
+ expect(subject.public_send(method, recursive: true) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
33
+ end
42
34
 
43
- it 'does not work recursively when parameter is passed as false' do
44
- expect(subject.change_values(recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
45
- end
35
+ it 'does not work recursively when parameter is passed as false' do
36
+ expect(subject.public_send(method, recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
37
+ end
46
38
 
47
- it 'does not ignore hash when option is passed' do
48
- expect(subject.change_values(skip_inner: false) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 12)
49
- end
39
+ it 'does not ignore hash when option is passed' do
40
+ expect(subject.public_send(method, skip_inner: false) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 12)
41
+ end
50
42
 
51
- it 'ignore hash and work recursively when option is passed' do
52
- expect(subject.change_values(skip_inner: false) { |value| value.is_a?(Hash) ? value : value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
43
+ it 'ignore hash and work recursively when option is passed' do
44
+ expect(subject.public_send(method, skip_inner: true) { |value| value.is_a?(Hash) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: { d: 4, e: 5 })
45
+ end
46
+
47
+ it 'ignore hash and does not work recursively when option is passed' do
48
+ expect(subject.public_send(method, skip_inner: false, recursive: false) { |value| value.is_a?(Hash) ? value : value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
49
+ end
53
50
  end
54
51
 
55
- it 'ignore hash and does not work recursively when option is passed' do
56
- expect(subject.change_values(skip_inner: false, recursive: false) { |value| value.is_a?(Hash) ? value : value + 1 }).to eq(a: 2, b: 3, c: { d: 3, e: 4 })
52
+ context 'when using deeply nested arrays' do
53
+ let(:subject) { { a: 1, b: 2, c: [{ d: 3 }, { e: { f: 4 } } ] } }
54
+
55
+ it 'goes recursivly true arrays' do
56
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
57
+ end
58
+
59
+ it 'does not work recursively when parameter is passed as false' do
60
+ expect(subject.public_send(method, recursive: false) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }])
61
+ end
62
+
63
+ it 'does not ignore array when option is passed' do
64
+ expect(subject.public_send(method, skip_inner: false) { |value| value.is_a?(Array) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: 12)
65
+ end
66
+
67
+ it 'ignores array when option is passed' do
68
+ expect(subject.public_send(method, skip_inner: true) { |value| value.is_a?(Array) ? 10 + value.size : value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
69
+ end
70
+
71
+ it 'ignore hash and does not work recursively when option is passed' do
72
+ expect(subject.public_send(method, skip_inner: false, recursive: false) { |value| value.is_a?(Array) ? value : value + 1 }).to eq(a: 2, b: 3, c: [{ d: 3 }, { e: { f: 4 } }])
73
+ end
57
74
  end
58
75
 
59
- it 'applies to arrays as well' do
60
- subject = { a: 1, b: 2, c: [{ d: 3 }, { e: 4 }] }
61
- expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: 5 }])
76
+ context 'when using a nested extra class' do
77
+ let(:subject) { { a: 1, b: 2, c: Hash::ValueChanger::Dummy.new(3) } }
78
+
79
+ it 'goes perform the mapping with the extra class' do
80
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: 4)
81
+ end
82
+
83
+ context 'when class is an interactor' do
84
+ let(:subject) { { a: 1, b: 2, c: Hash::ValueChanger::DummyIteractor.new({ d: 3 }, { e: { f: 4 } }) } }
85
+
86
+ it 'goes through the iteractor' do
87
+ expect(subject.public_send(method) { |value| value + 1 }).to eq(a: 2, b: 3, c: [{ d: 4 }, { e: { f: 5 } }])
88
+ end
89
+ end
90
+
62
91
  end
63
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bidu-core_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bidu Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-04 00:00:00.000000000 Z
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -157,6 +157,8 @@ files:
157
157
  - spec/lib/numeric_spec.rb
158
158
  - spec/lib/symbol_spec.rb
159
159
  - spec/spec_helper.rb
160
+ - spec/support/models/hash/value_changer/dummy.rb
161
+ - spec/support/models/hash/value_changer/dummy_iteractor.rb
160
162
  - spec/support/shared_examples/array_random.rb
161
163
  - spec/support/shared_examples/chain_fetch.rb
162
164
  - spec/support/shared_examples/chain_hash_keys_changer.rb
@@ -200,6 +202,8 @@ test_files:
200
202
  - spec/lib/numeric_spec.rb
201
203
  - spec/lib/symbol_spec.rb
202
204
  - spec/spec_helper.rb
205
+ - spec/support/models/hash/value_changer/dummy.rb
206
+ - spec/support/models/hash/value_changer/dummy_iteractor.rb
203
207
  - spec/support/shared_examples/array_random.rb
204
208
  - spec/support/shared_examples/chain_fetch.rb
205
209
  - spec/support/shared_examples/chain_hash_keys_changer.rb