bidu-core_ext 1.0.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/bidu/core_ext/version.rb +1 -1
- data/lib/hash.rb +8 -0
- data/lib/hash/key_changer.rb +8 -0
- data/lib/symbol.rb +4 -0
- data/spec/lib/hash/key_changer_spec.rb +55 -0
- data/spec/lib/hash_spec.rb +1 -0
- data/spec/lib/symbol_spec.rb +11 -1
- data/spec/support/shared_examples/keys_underscorer.rb +82 -0
- data/spec/support/shared_examples/remap.rb +0 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb348c3d47b45a7c8cbfbe2501db5bce9c4761e6
|
4
|
+
data.tar.gz: 5c97c0b953856f15bb523d6725f278a1d20d36cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e288ff1798c21ff2664f4c957722a3805139ec3879c360e1b6963728bd70a05c9bd3e7a91957d1495c18beb2da9d5ae46dd88eb7b2d2a6325b8f4322086419e
|
7
|
+
data.tar.gz: a2235fde7dc2fab7e094956e050bd397b6bc796d63fb289868f5a38fa0a84021e94890b49d983e9b0b4bb0e275944e550b2ee6d9c1ff1c90fa7d757fc66a4453
|
data/Gemfile.lock
CHANGED
data/lib/hash.rb
CHANGED
@@ -64,6 +64,14 @@ class Hash
|
|
64
64
|
Hash::KeyChanger.new(self).camelize_keys(options)
|
65
65
|
end
|
66
66
|
|
67
|
+
def underscore_keys(options = {})
|
68
|
+
dup.underscore_keys!(options)
|
69
|
+
end
|
70
|
+
|
71
|
+
def underscore_keys!(options = {})
|
72
|
+
Hash::KeyChanger.new(self).underscore_keys(options)
|
73
|
+
end
|
74
|
+
|
67
75
|
def exclusive_merge(hash)
|
68
76
|
dup.exclusive_merge!(hash)
|
69
77
|
end
|
data/lib/hash/key_changer.rb
CHANGED
data/lib/symbol.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash::KeyChanger do
|
4
|
+
let(:subject) { described_class.new(hash) }
|
5
|
+
|
6
|
+
describe '#underscore_keys' do
|
7
|
+
let(:hash) { { keyUnderscore: 1 } }
|
8
|
+
|
9
|
+
it 'underscore all the keys' do
|
10
|
+
expect(subject.underscore_keys).to eq(key_underscore: 1)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'when hash is a many level hash' do
|
14
|
+
let(:hash) { { keyUnderscore: { anotherKey: 1 } } }
|
15
|
+
|
16
|
+
it 'underscore all the keys' do
|
17
|
+
expect(subject.underscore_keys).to eq(key_underscore: { another_key: 1 })
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when hash has an array' do
|
22
|
+
let(:hash) { { keyUnderscore: [{ anotherKey: 1 }] } }
|
23
|
+
|
24
|
+
it 'underscore all the keys' do
|
25
|
+
expect(subject.underscore_keys).to eq(key_underscore: [{ another_key: 1 }])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'changes the hash' do
|
30
|
+
it 'underscore all the keys' do
|
31
|
+
expect do
|
32
|
+
subject.underscore_keys
|
33
|
+
end.to change { hash }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when giving non recursive options' do
|
38
|
+
context 'when hash is a many level hash' do
|
39
|
+
let(:hash) { { keyUnderscore: { anotherKey: 1 } } }
|
40
|
+
|
41
|
+
it 'underscore all the keys' do
|
42
|
+
expect(subject.underscore_keys(recursive: false)).to eq(key_underscore: { anotherKey: 1 })
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when hash has an array' do
|
47
|
+
let(:hash) { { keyUnderscore: [{ anotherKey: 1 }] } }
|
48
|
+
|
49
|
+
it 'underscore all the keys' do
|
50
|
+
expect(subject.underscore_keys(recursive: false)).to eq(key_underscore: [{ anotherKey: 1 }])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/lib/hash_spec.rb
CHANGED
@@ -3,6 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Hash do
|
4
4
|
it_behaves_like 'a class with change_key method'
|
5
5
|
it_behaves_like 'a class with camlize_keys method'
|
6
|
+
it_behaves_like 'a class with underscore_keys method'
|
6
7
|
it_behaves_like 'a class with append_keys method'
|
7
8
|
it_behaves_like 'a class with change_kvalues method'
|
8
9
|
it_behaves_like 'a class with remap method'
|
data/spec/lib/symbol_spec.rb
CHANGED
@@ -11,7 +11,7 @@ describe Symbol do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context 'when called with lower option' do
|
14
|
-
it '
|
14
|
+
it 'camelize the symbol without captalization' do
|
15
15
|
expect(:underscore_sym.camelize(:lower)).to eq(:underscoreSym)
|
16
16
|
end
|
17
17
|
end
|
@@ -22,4 +22,14 @@ describe Symbol do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
describe '#underscore' do
|
27
|
+
it { expect(:symBol.underscore).to be_kind_of(Symbol) }
|
28
|
+
|
29
|
+
context 'when called with upper option' do
|
30
|
+
it 'underscore the symbol' do
|
31
|
+
expect(:symBol.underscore).to eq(:sym_bol)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
25
35
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
shared_examples 'a class with underscore_keys method basic' do |method|
|
2
|
+
describe :underscore_keys do
|
3
|
+
let(:expected) { { input_key: 'value' } }
|
4
|
+
|
5
|
+
context 'with camel case keys' do
|
6
|
+
let(:hash) { { inputKey: 'value' } }
|
7
|
+
|
8
|
+
it 'converts the keys to snake case' do
|
9
|
+
expect(hash.send(method)).to eq(expected)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with string keys' do
|
14
|
+
let(:expected) { { 'input_key' => 'value' } }
|
15
|
+
let(:hash) { { 'InputKey' => 'value' } }
|
16
|
+
|
17
|
+
it 'converts the keys to snake case' do
|
18
|
+
expect(hash.send(method)).to eq(expected)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with deep keys change' do
|
23
|
+
let(:expected) { { input_key: { inner_key: 'value' } } }
|
24
|
+
let(:hash) { { InputKey: { InnerKey: 'value' } } }
|
25
|
+
|
26
|
+
it 'converts the keys to snake case' do
|
27
|
+
expect(hash.send(method)).to eq(expected)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with array keys change' do
|
32
|
+
let(:expected) { { input_key: [{ inner_key: 'value' }] } }
|
33
|
+
let(:hash) { { InputKey: [{ InnerKey: 'value' }] } }
|
34
|
+
|
35
|
+
it 'converts the keys to snake case' do
|
36
|
+
expect(hash.send(method)).to eq(expected)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'without recursive options' do
|
41
|
+
context 'with deep keys change' do
|
42
|
+
let(:expected) { { input_key: { InnerKey: 'value' } } }
|
43
|
+
let(:hash) { { InputKey: { InnerKey: 'value' } } }
|
44
|
+
|
45
|
+
it 'converts the keys to lower camel case' do
|
46
|
+
expect(hash.send(method, recursive: false)).to eq(expected)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'with array keys change' do
|
51
|
+
let(:expected) { { input_key: [{ InnerKey: 'value' }] } }
|
52
|
+
let(:hash) { { InputKey: [{ InnerKey: 'value' }] } }
|
53
|
+
|
54
|
+
it 'converts the keys to camle case' do
|
55
|
+
expect(hash.send(method, recursive: false)).to eq(expected)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
shared_examples 'a class with underscore_keys! method' do
|
63
|
+
let(:hash) { { inputKey: 'value' } }
|
64
|
+
|
65
|
+
it_behaves_like 'a class with underscore_keys method basic', :underscore_keys!
|
66
|
+
|
67
|
+
it 'changes original hash' do
|
68
|
+
expect { hash.underscore_keys! }.to change { hash }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
shared_examples 'a class with underscore_keys method' do
|
73
|
+
let(:hash) { { inputKey: 'value' } }
|
74
|
+
|
75
|
+
it_behaves_like 'a class with underscore_keys method basic', :underscore_keys
|
76
|
+
it_behaves_like 'a class with underscore_keys! method'
|
77
|
+
|
78
|
+
it 'does not change original hash' do
|
79
|
+
expect { hash.underscore_keys }.not_to change { hash }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
File without changes
|
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.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bidu Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- spec/lib/array_spec.rb
|
125
125
|
- spec/lib/enumerable_spec.rb
|
126
126
|
- spec/lib/hash/deep_hash_constructor_spec.rb
|
127
|
+
- spec/lib/hash/key_changer_spec.rb
|
127
128
|
- spec/lib/hash_spec.rb
|
128
129
|
- spec/lib/numeric_spec.rb
|
129
130
|
- spec/lib/symbol_spec.rb
|
@@ -133,6 +134,7 @@ files:
|
|
133
134
|
- spec/support/shared_examples/hash_keys_changer.rb
|
134
135
|
- spec/support/shared_examples/keys_appender.rb
|
135
136
|
- spec/support/shared_examples/keys_camelizer.rb
|
137
|
+
- spec/support/shared_examples/keys_underscorer.rb
|
136
138
|
- spec/support/shared_examples/remap.rb
|
137
139
|
- spec/support/shared_examples/value_changer.rb
|
138
140
|
homepage:
|
@@ -154,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
156
|
version: '0'
|
155
157
|
requirements: []
|
156
158
|
rubyforge_project:
|
157
|
-
rubygems_version: 2.
|
159
|
+
rubygems_version: 2.4.6
|
158
160
|
signing_key:
|
159
161
|
specification_version: 4
|
160
162
|
summary: Core Extensions
|
@@ -162,6 +164,7 @@ test_files:
|
|
162
164
|
- spec/lib/array_spec.rb
|
163
165
|
- spec/lib/enumerable_spec.rb
|
164
166
|
- spec/lib/hash/deep_hash_constructor_spec.rb
|
167
|
+
- spec/lib/hash/key_changer_spec.rb
|
165
168
|
- spec/lib/hash_spec.rb
|
166
169
|
- spec/lib/numeric_spec.rb
|
167
170
|
- spec/lib/symbol_spec.rb
|
@@ -171,5 +174,6 @@ test_files:
|
|
171
174
|
- spec/support/shared_examples/hash_keys_changer.rb
|
172
175
|
- spec/support/shared_examples/keys_appender.rb
|
173
176
|
- spec/support/shared_examples/keys_camelizer.rb
|
177
|
+
- spec/support/shared_examples/keys_underscorer.rb
|
174
178
|
- spec/support/shared_examples/remap.rb
|
175
179
|
- spec/support/shared_examples/value_changer.rb
|