bidu-core_ext 1.0.0 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a917ec3ef22ae183e9cbb569e438a80456800ad
4
- data.tar.gz: dc600d8c5f69a6776e77e8aacddbef83d446cd52
3
+ metadata.gz: fb348c3d47b45a7c8cbfbe2501db5bce9c4761e6
4
+ data.tar.gz: 5c97c0b953856f15bb523d6725f278a1d20d36cf
5
5
  SHA512:
6
- metadata.gz: 97f018e78cf3b0eeff67540fc48c4c2e0b19a99db46edde05fe4e22708e4856b8e0cf1314f7b2a97f538d55e38ed527c0b12dad122f46208775f91e8a71a6560
7
- data.tar.gz: f3825dd7fd3d241a4daa9312d8748bfa580d8e1b37160b8be2bc183050239bd90f423790b1e1880b5fa09bf78d9380ee3317708a0bf5fb3bd98df066f1dbef35
6
+ metadata.gz: 9e288ff1798c21ff2664f4c957722a3805139ec3879c360e1b6963728bd70a05c9bd3e7a91957d1495c18beb2da9d5ae46dd88eb7b2d2a6325b8f4322086419e
7
+ data.tar.gz: a2235fde7dc2fab7e094956e050bd397b6bc796d63fb289868f5a38fa0a84021e94890b49d983e9b0b4bb0e275944e550b2ee6d9c1ff1c90fa7d757fc66a4453
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bidu-core_ext (1.0.0)
4
+ bidu-core_ext (1.1.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  module Bidu
2
2
  module CoreExt
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.1'
4
4
  end
5
5
  end
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
@@ -29,6 +29,14 @@ class Hash::KeyChanger
29
29
  end
30
30
  end
31
31
 
32
+ def underscore_keys(settings = {})
33
+ merge_options({}, settings)
34
+
35
+ change_keys do |k|
36
+ k.underscore
37
+ end
38
+ end
39
+
32
40
  def change_text(options = {}, &block)
33
41
  merge_options({
34
42
  type: :keep
data/lib/symbol.rb CHANGED
@@ -2,4 +2,8 @@ class Symbol
2
2
  def camelize(type = :upper)
3
3
  to_s.camelize(type).to_sym
4
4
  end
5
+
6
+ def underscore
7
+ to_s.underscore.to_sym
8
+ end
5
9
  end
@@ -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
@@ -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'
@@ -11,7 +11,7 @@ describe Symbol do
11
11
  end
12
12
 
13
13
  context 'when called with lower option' do
14
- it 'snake case the symbol' do
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.0.0
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-05-08 00:00:00.000000000 Z
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.2.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