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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +4 -4
- data/lib/bidu/core_ext/version.rb +1 -1
- data/lib/hash/value_changer.rb +15 -6
- data/spec/support/models/hash/value_changer/dummy.rb +15 -0
- data/spec/support/models/hash/value_changer/dummy_iteractor.rb +12 -0
- data/spec/support/shared_examples/value_changer.rb +63 -34
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3aece23d84b190a26be718796ea4986a5a90b3ce
|
4
|
+
data.tar.gz: 41c946d5b73bfea41809ecfaec23ce6c827f1c1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb361421619c5377fc49cc859c152650715a6922d0f11aec782c6f18ca4afd5b271ce6f415b6efcba2a2679ad047cf4840fee2ef725d09617c72faf32df80e87
|
7
|
+
data.tar.gz: 8d42a46be5315f9c21bb5530f81374394c1e3bca46ffdc905f3073c7d88761807deb2595195415a0eeeebf15469c4c98db0b67cbabf0d611c0a7113c7d8e8b47
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
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
|
data/lib/hash/value_changer.rb
CHANGED
@@ -11,9 +11,9 @@ class Hash::ValueChanger
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def change(object)
|
14
|
-
if object.
|
14
|
+
if object.respond_to?(:change_values)
|
15
15
|
change_hash(object)
|
16
|
-
elsif object
|
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
|
-
|
35
|
-
|
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
|
-
!
|
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
|
-
|
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
|
@@ -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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
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
|
-
|
56
|
-
|
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
|
-
|
60
|
-
subject
|
61
|
-
|
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
|
+
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-
|
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
|