bidu-core_ext 1.0.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 +7 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +49 -0
- data/LICENSE +22 -0
- data/README.md +143 -0
- data/Rakefile +7 -0
- data/core_ext.gemspec +25 -0
- data/lib/array.rb +15 -0
- data/lib/array/hash_builder.rb +20 -0
- data/lib/bidu.rb +4 -0
- data/lib/bidu/core_ext.rb +12 -0
- data/lib/bidu/core_ext/version.rb +5 -0
- data/lib/enumerable.rb +30 -0
- data/lib/hash.rb +161 -0
- data/lib/hash/deep_hash_constructor.rb +83 -0
- data/lib/hash/key_changer.rb +68 -0
- data/lib/hash/value_changer.rb +52 -0
- data/lib/numeric.rb +6 -0
- data/lib/symbol.rb +5 -0
- data/spec/lib/array_spec.rb +93 -0
- data/spec/lib/enumerable_spec.rb +31 -0
- data/spec/lib/hash/deep_hash_constructor_spec.rb +167 -0
- data/spec/lib/hash_spec.rb +287 -0
- data/spec/lib/numeric_spec.rb +61 -0
- data/spec/lib/symbol_spec.rb +25 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/support/shared_examples/chain_fetch.rb +88 -0
- data/spec/support/shared_examples/clean.rb +143 -0
- data/spec/support/shared_examples/hash_keys_changer.rb +92 -0
- data/spec/support/shared_examples/keys_appender.rb +43 -0
- data/spec/support/shared_examples/keys_camelizer.rb +285 -0
- data/spec/support/shared_examples/remap.rb +89 -0
- data/spec/support/shared_examples/value_changer.rb +63 -0
- metadata +175 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
shared_examples 'a class with remap method' do
|
2
|
+
let(:subject) { { a: 1, b: 2 } }
|
3
|
+
|
4
|
+
describe :remap_keys do
|
5
|
+
it_behaves_like 'a method that remaps the keys', :remap_keys
|
6
|
+
|
7
|
+
it 'does not change the original hash' do
|
8
|
+
expect { subject.remap_keys(a: :e) }.not_to change { subject }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe :remap_keys! do
|
13
|
+
it_behaves_like 'a method that remaps the keys', :remap_keys!
|
14
|
+
|
15
|
+
it 'changes the original hash' do
|
16
|
+
expect { subject.remap_keys!(a: :e) }.to change { subject }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
shared_examples 'a method that remaps the keys' do |method|
|
23
|
+
let(:result) { subject.public_send(method, remap) }
|
24
|
+
|
25
|
+
context 'when remap and hash keys match' do
|
26
|
+
let(:remap) { { a: :e, b: :f } }
|
27
|
+
|
28
|
+
it 'remaps the keys' do
|
29
|
+
expect(result).to eq(e: 1, f: 2)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when remap and hash keys do not match' do
|
34
|
+
let(:remap) { { b: :f } }
|
35
|
+
|
36
|
+
it 'remap only the keys that match' do
|
37
|
+
expect(result).to eq(a: 1, f: 2)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when there are keys out of the keys list' do
|
42
|
+
let(:remap) { { c: :g } }
|
43
|
+
|
44
|
+
it 'creates a nil valued key' do
|
45
|
+
expect(result).to eq(a: 1, b: 2, g: nil)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when remap has no keys' do
|
50
|
+
let(:remap) { {} }
|
51
|
+
|
52
|
+
it 'does not remap the keys' do
|
53
|
+
expect(result).to eq(a: 1, b: 2)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when remap has original array keys' do
|
58
|
+
let(:remap) { { b: :a, a: :b } }
|
59
|
+
|
60
|
+
it 'does not remap the keys' do
|
61
|
+
expect(result).to eq(b: 1, a: 2)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when remap has original array keys' do
|
66
|
+
let(:remap) { { a: :b, b: :c } }
|
67
|
+
|
68
|
+
it 'does not remap the keys' do
|
69
|
+
expect(result).to eq(b: 1, c: 2)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when changing the type of the key' do
|
74
|
+
let(:remap) { { a: 'a' } }
|
75
|
+
|
76
|
+
it 'does not remap the keys' do
|
77
|
+
expect(result).to eq('a' => 1, b: 2)
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'and the original key is an string' do
|
81
|
+
let(:subject) { { 'a' => 1, 'b' => 2 } }
|
82
|
+
let(:remap) { { 'a' => :a } }
|
83
|
+
|
84
|
+
it 'does not remap the keys' do
|
85
|
+
expect(result).to eq(a: 1, 'b' => 2)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
shared_examples 'a class with change_kvalues method' do
|
2
|
+
let(:subject) { { a: 1, b: 2, c: { d: 3, e: 4 } } }
|
3
|
+
|
4
|
+
describe :change_values do
|
5
|
+
it_behaves_like 'a method that change the hash values', :change_values
|
6
|
+
|
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 }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe :change_values! do
|
23
|
+
it_behaves_like 'a method that change the hash values', :change_values!
|
24
|
+
|
25
|
+
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 })
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
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
|
38
|
+
|
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
|
42
|
+
|
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
|
46
|
+
|
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
|
50
|
+
|
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 })
|
53
|
+
end
|
54
|
+
|
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 })
|
57
|
+
end
|
58
|
+
|
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 }])
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bidu-core_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bidu Developers
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-mocks
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- dev@bidu.com.br
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
108
|
+
- LICENSE
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- core_ext.gemspec
|
112
|
+
- lib/array.rb
|
113
|
+
- lib/array/hash_builder.rb
|
114
|
+
- lib/bidu.rb
|
115
|
+
- lib/bidu/core_ext.rb
|
116
|
+
- lib/bidu/core_ext/version.rb
|
117
|
+
- lib/enumerable.rb
|
118
|
+
- lib/hash.rb
|
119
|
+
- lib/hash/deep_hash_constructor.rb
|
120
|
+
- lib/hash/key_changer.rb
|
121
|
+
- lib/hash/value_changer.rb
|
122
|
+
- lib/numeric.rb
|
123
|
+
- lib/symbol.rb
|
124
|
+
- spec/lib/array_spec.rb
|
125
|
+
- spec/lib/enumerable_spec.rb
|
126
|
+
- spec/lib/hash/deep_hash_constructor_spec.rb
|
127
|
+
- spec/lib/hash_spec.rb
|
128
|
+
- spec/lib/numeric_spec.rb
|
129
|
+
- spec/lib/symbol_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- spec/support/shared_examples/chain_fetch.rb
|
132
|
+
- spec/support/shared_examples/clean.rb
|
133
|
+
- spec/support/shared_examples/hash_keys_changer.rb
|
134
|
+
- spec/support/shared_examples/keys_appender.rb
|
135
|
+
- spec/support/shared_examples/keys_camelizer.rb
|
136
|
+
- spec/support/shared_examples/remap.rb
|
137
|
+
- spec/support/shared_examples/value_changer.rb
|
138
|
+
homepage:
|
139
|
+
licenses: []
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.2.2
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: Core Extensions
|
161
|
+
test_files:
|
162
|
+
- spec/lib/array_spec.rb
|
163
|
+
- spec/lib/enumerable_spec.rb
|
164
|
+
- spec/lib/hash/deep_hash_constructor_spec.rb
|
165
|
+
- spec/lib/hash_spec.rb
|
166
|
+
- spec/lib/numeric_spec.rb
|
167
|
+
- spec/lib/symbol_spec.rb
|
168
|
+
- spec/spec_helper.rb
|
169
|
+
- spec/support/shared_examples/chain_fetch.rb
|
170
|
+
- spec/support/shared_examples/clean.rb
|
171
|
+
- spec/support/shared_examples/hash_keys_changer.rb
|
172
|
+
- spec/support/shared_examples/keys_appender.rb
|
173
|
+
- spec/support/shared_examples/keys_camelizer.rb
|
174
|
+
- spec/support/shared_examples/remap.rb
|
175
|
+
- spec/support/shared_examples/value_changer.rb
|