bidu-core_ext 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +23 -22
- data/README.md +11 -0
- data/core_ext.gemspec +6 -5
- data/lib/bidu/core_ext/version.rb +1 -1
- data/lib/hash.rb +19 -1
- data/spec/lib/hash/deep_hash_constructor_spec.rb +2 -2
- data/spec/lib/hash_spec.rb +4 -3
- data/spec/support/shared_examples/chain_hash_keys_changer.rb +88 -0
- data/spec/support/shared_examples/expected.rb +6 -0
- data/spec/support/shared_examples/hash_keys_changer.rb +0 -7
- data/spec/support/shared_examples/value_changer.rb +1 -1
- metadata +41 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46ca9596bf1bd9395697f6d139e95529443dfeed
|
4
|
+
data.tar.gz: 1f867d81d89c0da0a422bad9ec9ff241cb16c057
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cc16aa3de7626debf7b94218590db05eb5deefacd264a5dd1bf1cdea39f41e66e47df1e13b5b434ec3a1e02e345f07d890bda70d3cb149d494a39e408c24933
|
7
|
+
data.tar.gz: 5c18491c6ae48e0fbed356615fcf1026b68585597167d9d7fa9eda3a4a1419ea32daa864b0aa31389dcbf3fe0e9647318a31c7e46837b25595365613fcc7a50e
|
data/Gemfile.lock
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bidu-core_ext (1.2.
|
5
|
-
activesupport
|
4
|
+
bidu-core_ext (1.2.4)
|
5
|
+
activesupport (~> 5.1.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (5.
|
10
|
+
activesupport (5.1.1)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (~> 0.7)
|
13
13
|
minitest (~> 5.1)
|
14
14
|
tzinfo (~> 1.1)
|
15
|
-
coderay (1.1.
|
16
|
-
concurrent-ruby (1.0.
|
15
|
+
coderay (1.1.1)
|
16
|
+
concurrent-ruby (1.0.5)
|
17
17
|
diff-lcs (1.2.5)
|
18
18
|
docile (1.1.5)
|
19
|
-
i18n (0.
|
19
|
+
i18n (0.8.4)
|
20
|
+
json (2.1.0)
|
20
21
|
method_source (0.8.2)
|
21
|
-
minitest (5.
|
22
|
-
|
23
|
-
pry (0.10.3)
|
22
|
+
minitest (5.10.2)
|
23
|
+
pry (0.10.4)
|
24
24
|
coderay (~> 1.1.0)
|
25
25
|
method_source (~> 0.8.1)
|
26
26
|
slop (~> 3.4)
|
27
27
|
pry-nav (0.2.4)
|
28
28
|
pry (>= 0.9.10, < 0.11.0)
|
29
|
-
rake (
|
29
|
+
rake (11.3.0)
|
30
30
|
rspec (2.99.0)
|
31
31
|
rspec-core (~> 2.99.0)
|
32
32
|
rspec-expectations (~> 2.99.0)
|
@@ -34,15 +34,15 @@ GEM
|
|
34
34
|
rspec-core (2.99.2)
|
35
35
|
rspec-expectations (2.99.2)
|
36
36
|
diff-lcs (>= 1.1.3, < 2.0)
|
37
|
-
rspec-mocks (2.99.
|
38
|
-
simplecov (0.
|
37
|
+
rspec-mocks (2.99.4)
|
38
|
+
simplecov (0.14.1)
|
39
39
|
docile (~> 1.1.0)
|
40
|
-
|
41
|
-
simplecov-html (~> 0.
|
42
|
-
simplecov-html (0.
|
40
|
+
json (>= 1.8, < 3)
|
41
|
+
simplecov-html (~> 0.10.0)
|
42
|
+
simplecov-html (0.10.1)
|
43
43
|
slop (3.6.0)
|
44
|
-
thread_safe (0.3.
|
45
|
-
tzinfo (1.2.
|
44
|
+
thread_safe (0.3.6)
|
45
|
+
tzinfo (1.2.3)
|
46
46
|
thread_safe (~> 0.1)
|
47
47
|
|
48
48
|
PLATFORMS
|
@@ -51,11 +51,12 @@ PLATFORMS
|
|
51
51
|
DEPENDENCIES
|
52
52
|
bidu-core_ext!
|
53
53
|
bundler (~> 1.6)
|
54
|
-
pry
|
55
|
-
|
54
|
+
pry (~> 0.10.4)
|
55
|
+
pry-nav (~> 0.2.4)
|
56
|
+
rake (~> 11.3.0)
|
56
57
|
rspec (~> 2.14)
|
57
|
-
rspec-mocks
|
58
|
-
simplecov
|
58
|
+
rspec-mocks (~> 2.99.4)
|
59
|
+
simplecov (~> 0.14.1)
|
59
60
|
|
60
61
|
BUNDLED WITH
|
61
|
-
1.
|
62
|
+
1.15.1
|
data/README.md
CHANGED
@@ -106,6 +106,17 @@ returns
|
|
106
106
|
{ 'CA_B' => 1 }
|
107
107
|
```
|
108
108
|
|
109
|
+
###chain_change_keys
|
110
|
+
Change the hash keys usin a chained method call
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
{ ca_b: 1 }.chain_change_keys(:to_s, :upcase, :to_sym)
|
114
|
+
```
|
115
|
+
returns
|
116
|
+
```ruby
|
117
|
+
{ CA_B: 1 }
|
118
|
+
```
|
119
|
+
|
109
120
|
### change_values
|
110
121
|
Change the values of the array
|
111
122
|
```ruby
|
data/core_ext.gemspec
CHANGED
@@ -15,12 +15,13 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
16
|
spec.require_paths = ['lib']
|
17
17
|
|
18
|
-
spec.add_runtime_dependency 'activesupport'
|
18
|
+
spec.add_runtime_dependency 'activesupport', '~> 5.1.1'
|
19
19
|
|
20
20
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
-
spec.add_development_dependency 'rake'
|
21
|
+
spec.add_development_dependency 'rake', '~> 11.3.0'
|
22
22
|
spec.add_development_dependency 'rspec', '~> 2.14'
|
23
|
-
spec.add_development_dependency 'rspec-mocks'
|
24
|
-
spec.add_development_dependency 'pry
|
25
|
-
spec.add_development_dependency '
|
23
|
+
spec.add_development_dependency 'rspec-mocks', '~> 2.99.4'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.10.4'
|
25
|
+
spec.add_development_dependency 'pry-nav', '~> 0.2.4'
|
26
|
+
spec.add_development_dependency 'simplecov', '~> 0.14.1'
|
26
27
|
end
|
data/lib/hash.rb
CHANGED
@@ -90,7 +90,7 @@ class Hash
|
|
90
90
|
|
91
91
|
# change all keys returning the new map
|
92
92
|
# options: { recursive: true }
|
93
|
-
# ex: { "a"
|
93
|
+
# ex: { "a" =>1 }.change_keys{ |key| key.upcase } == { "A" => 1 }
|
94
94
|
def change_keys(options = {}, &block)
|
95
95
|
deep_dup.change_keys!(options, &block)
|
96
96
|
end
|
@@ -102,6 +102,24 @@ class Hash
|
|
102
102
|
Hash::KeyChanger.new(self).change_keys(options, &block)
|
103
103
|
end
|
104
104
|
|
105
|
+
# change all publicaly sending method calls
|
106
|
+
# options: { recursive: true }
|
107
|
+
# ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
|
108
|
+
def chain_change_keys(*calls)
|
109
|
+
deep_dup.chain_change_keys!(*calls)
|
110
|
+
end
|
111
|
+
|
112
|
+
# change all publicaly sending method calls
|
113
|
+
# options: { recursive: true }
|
114
|
+
# ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
|
115
|
+
def chain_change_keys!(*calls)
|
116
|
+
options = calls.extract_options!
|
117
|
+
|
118
|
+
calls.inject(self) do |h, m|
|
119
|
+
h.change_keys!(options, &m)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
105
123
|
# prepend a string to all keys
|
106
124
|
# options {
|
107
125
|
# recursive: true,
|
@@ -96,7 +96,7 @@ describe Hash::DeepHashConstructor do
|
|
96
96
|
'quote_request.personal.person[1].name' => 'Some name 2',
|
97
97
|
'quote_request.personal.person[1].age' => 23,
|
98
98
|
'request[0].status.clazz' => String,
|
99
|
-
'request[1].status.clazz' =>
|
99
|
+
'request[1].status.clazz' => Integer,
|
100
100
|
'request[2].status.clazz' => Date,
|
101
101
|
'trials' => 3
|
102
102
|
}
|
@@ -114,7 +114,7 @@ describe Hash::DeepHashConstructor do
|
|
114
114
|
},
|
115
115
|
'request' => [
|
116
116
|
{ 'status' => { 'clazz' => String } },
|
117
|
-
{ 'status' => { 'clazz' =>
|
117
|
+
{ 'status' => { 'clazz' => Integer } },
|
118
118
|
{ 'status' => { 'clazz' => Date } }
|
119
119
|
],
|
120
120
|
'trials' => 3
|
data/spec/lib/hash_spec.rb
CHANGED
@@ -2,10 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Hash do
|
4
4
|
it_behaves_like 'a class with change_key method'
|
5
|
+
it_behaves_like 'a class with chain_change_key method'
|
5
6
|
it_behaves_like 'a class with camlize_keys method'
|
6
7
|
it_behaves_like 'a class with underscore_keys method'
|
7
8
|
it_behaves_like 'a class with append_keys method'
|
8
|
-
it_behaves_like 'a class with
|
9
|
+
it_behaves_like 'a class with change_values method'
|
9
10
|
it_behaves_like 'a class with remap method'
|
10
11
|
it_behaves_like 'an object with chain_fetch method'
|
11
12
|
|
@@ -173,7 +174,7 @@ describe Hash do
|
|
173
174
|
'quote_request.personal.person[1].name' => 'Some name 2',
|
174
175
|
'quote_request.personal.person[1].age' => 23,
|
175
176
|
'request[0].status.clazz' => String,
|
176
|
-
'request[1].status.clazz' =>
|
177
|
+
'request[1].status.clazz' => Integer,
|
177
178
|
'request[2].status.clazz' => Date,
|
178
179
|
'trials' => 3
|
179
180
|
}
|
@@ -191,7 +192,7 @@ describe Hash do
|
|
191
192
|
},
|
192
193
|
'request' => [
|
193
194
|
{ 'status' => { 'clazz' => String } },
|
194
|
-
{ 'status' => { 'clazz' =>
|
195
|
+
{ 'status' => { 'clazz' => Integer } },
|
195
196
|
{ 'status' => { 'clazz' => Date } }
|
196
197
|
],
|
197
198
|
'trials' => 3
|
@@ -0,0 +1,88 @@
|
|
1
|
+
shared_examples 'a class with chain_change_key method' do
|
2
|
+
let(:hash) do
|
3
|
+
{ 'a' => 1, b: 2, c: { d: 3, e: 4 }, f: [{ g: 5 }, { h: 6 }] }
|
4
|
+
end
|
5
|
+
|
6
|
+
describe :chain_change_keys do
|
7
|
+
|
8
|
+
it_behaves_like 'a method that is able to chain change keys', :chain_change_keys
|
9
|
+
it 'does not affects the original hash' do
|
10
|
+
expect do
|
11
|
+
hash.chain_change_keys(:to_s, :upcase)
|
12
|
+
end.not_to change { hash }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe :ichain_change_keys! do
|
17
|
+
it_behaves_like 'a method that is able to chain change keys', :chain_change_keys!
|
18
|
+
|
19
|
+
it 'affects the original hash' do
|
20
|
+
expect do
|
21
|
+
hash.chain_change_keys!(:to_s, :upcase)
|
22
|
+
end.to change { hash }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
shared_examples 'a method that is able to chain change keys' do |method|
|
28
|
+
let(:result) { hash.public_send(method, *transformations, options) }
|
29
|
+
let(:options) { {} }
|
30
|
+
let(:transformations) { [ :to_s ] }
|
31
|
+
|
32
|
+
context 'with simple level hash' do
|
33
|
+
let(:hash) { { 'a' => 1, b: 2 } }
|
34
|
+
|
35
|
+
context 'with symbol transformation' do
|
36
|
+
let(:transformations) { [ :to_sym ] }
|
37
|
+
let(:expected) { { a: 1, b: 2 } }
|
38
|
+
it_behaves_like 'result is as expected'
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with string transformation' do
|
42
|
+
let(:expected) { { 'a' => 1, 'b' => 2 } }
|
43
|
+
it_behaves_like 'result is as expected'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with recursive hash' do
|
48
|
+
let(:hash) { { 'a' => 1, b: { c: 3, 'd' => 4 } } }
|
49
|
+
let(:expected) do
|
50
|
+
{ 'a' => 1, 'b' => { 'c' => 3, 'd' => 4 } }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when no options are given' do
|
54
|
+
it_behaves_like 'result is as expected'
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when options are given' do
|
58
|
+
let(:options) { { recursive: recursive } }
|
59
|
+
|
60
|
+
context 'with recursion' do
|
61
|
+
let(:recursive) { true }
|
62
|
+
it_behaves_like 'result is as expected'
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'without recursion' do
|
66
|
+
let(:recursive) { false }
|
67
|
+
let(:expected) { { 'a' => 1, 'b' => { c: 3, 'd' => 4 } } }
|
68
|
+
it_behaves_like 'result is as expected'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with many many levels' do
|
74
|
+
let(:hash) { { a: 1, b: { c: 2, d: { e: 3, f: 4 } } } }
|
75
|
+
let(:expected) do
|
76
|
+
{ 'a' => 1, 'b' => { 'c' => 2, 'd' => { 'e' => 3, 'f' => 4 } } }
|
77
|
+
end
|
78
|
+
it_behaves_like 'result is as expected'
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'calling with chained transformations' do
|
82
|
+
let(:transformations) { [ :to_s, :upcase, :to_sym ] }
|
83
|
+
|
84
|
+
let(:hexpected) do
|
85
|
+
{ A: 1, B: 2, C: { D: 3, E: 4 }, F: [{ G: 5 }, { H: 6 }] }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -4,7 +4,6 @@ shared_examples 'a class with change_key method' do
|
|
4
4
|
end
|
5
5
|
|
6
6
|
describe :change_keys do
|
7
|
-
|
8
7
|
it_behaves_like 'a method that is able to change keys', :change_keys
|
9
8
|
it 'does not affects the original hash' do
|
10
9
|
expect do
|
@@ -24,12 +23,6 @@ shared_examples 'a class with change_key method' do
|
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
|
-
shared_examples 'result is as expected' do
|
28
|
-
it 'is as expected' do
|
29
|
-
expect(result).to eq(expected)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
26
|
shared_examples 'a method that is able to change keys' do |method|
|
34
27
|
let(:foo_sym_transformation) do
|
35
28
|
hash.public_send(method) { |k| "foo_#{k}".to_sym }
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bidu Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.1.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.1.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 11.3.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 11.3.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,44 +70,58 @@ dependencies:
|
|
70
70
|
name: rspec-mocks
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 2.99.4
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 2.99.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.10.4
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.10.4
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: pry-nav
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- - "
|
101
|
+
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
103
|
+
version: 0.2.4
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- - "
|
108
|
+
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
110
|
+
version: 0.2.4
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: simplecov
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- - "
|
115
|
+
- - "~>"
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
117
|
+
version: 0.14.1
|
104
118
|
type: :development
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
|
-
- - "
|
122
|
+
- - "~>"
|
109
123
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
124
|
+
version: 0.14.1
|
111
125
|
description:
|
112
126
|
email:
|
113
127
|
- dev@bidu.com.br
|
@@ -145,7 +159,9 @@ files:
|
|
145
159
|
- spec/spec_helper.rb
|
146
160
|
- spec/support/shared_examples/array_random.rb
|
147
161
|
- spec/support/shared_examples/chain_fetch.rb
|
162
|
+
- spec/support/shared_examples/chain_hash_keys_changer.rb
|
148
163
|
- spec/support/shared_examples/clean.rb
|
164
|
+
- spec/support/shared_examples/expected.rb
|
149
165
|
- spec/support/shared_examples/hash_keys_changer.rb
|
150
166
|
- spec/support/shared_examples/keys_appender.rb
|
151
167
|
- spec/support/shared_examples/keys_camelizer.rb
|
@@ -171,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
187
|
version: '0'
|
172
188
|
requirements: []
|
173
189
|
rubyforge_project:
|
174
|
-
rubygems_version: 2.
|
190
|
+
rubygems_version: 2.6.8
|
175
191
|
signing_key:
|
176
192
|
specification_version: 4
|
177
193
|
summary: Core Extensions
|
@@ -186,7 +202,9 @@ test_files:
|
|
186
202
|
- spec/spec_helper.rb
|
187
203
|
- spec/support/shared_examples/array_random.rb
|
188
204
|
- spec/support/shared_examples/chain_fetch.rb
|
205
|
+
- spec/support/shared_examples/chain_hash_keys_changer.rb
|
189
206
|
- spec/support/shared_examples/clean.rb
|
207
|
+
- spec/support/shared_examples/expected.rb
|
190
208
|
- spec/support/shared_examples/hash_keys_changer.rb
|
191
209
|
- spec/support/shared_examples/keys_appender.rb
|
192
210
|
- spec/support/shared_examples/keys_camelizer.rb
|