bidu-core_ext 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff128913bd7444df25b5fc6ae4842d3ad7ea6315
4
- data.tar.gz: ecc39a938de9254da1532c18162fecd2a90e4f1c
3
+ metadata.gz: b59372acd2cbf72c2ce3da4da40d77eed277e706
4
+ data.tar.gz: 2f1294f24be1e567e6b508c67d8a70fd09d0f278
5
5
  SHA512:
6
- metadata.gz: b5ff287b0509250b668221ffd7e25b2e0efb3c7b9ad5bf680597c2c9f9debdfbbe54799a195d2ca578447bd6c5dfa555d7e8435f0b889d09fdc869cd7dcf27b9
7
- data.tar.gz: 3f3f108767944b904e3538029e250fb6c76f46d1e9f7bd808dddedc5a851833197599d103216f726ce6fcd7fdd7b8a57136716049ee855dd614602e36aeb27ed
6
+ metadata.gz: 0972c125e7aba80a11163b24ca6e45cd4508e57eca74e0bffa767747ca192258b5130ed0151daec8c825452c9eb5b0036fe54d8a516ee510a15c32dd62e956f9
7
+ data.tar.gz: 7baca49a766b41edb9ddb8c75d457f5d8c98d5ebb267915f03526ac6fd449379020116800567d3df9d87b208d4ae3b01d65e53046b50675ff9876c2c81f03fed
data/Gemfile.lock CHANGED
@@ -1,25 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bidu-core_ext (1.2.2)
4
+ bidu-core_ext (1.2.3)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (4.2.6)
10
+ activesupport (5.0.0.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
12
  i18n (~> 0.7)
12
- json (~> 1.7, >= 1.7.7)
13
13
  minitest (~> 5.1)
14
- thread_safe (~> 0.3, >= 0.3.4)
15
14
  tzinfo (~> 1.1)
16
15
  coderay (1.1.0)
16
+ concurrent-ruby (1.0.2)
17
17
  diff-lcs (1.2.5)
18
18
  docile (1.1.5)
19
19
  i18n (0.7.0)
20
- json (1.8.3)
21
20
  method_source (0.8.2)
22
- minitest (5.9.0)
21
+ minitest (5.9.1)
23
22
  multi_json (1.10.1)
24
23
  pry (0.10.3)
25
24
  coderay (~> 1.1.0)
@@ -59,4 +58,4 @@ DEPENDENCIES
59
58
  simplecov
60
59
 
61
60
  BUNDLED WITH
62
- 1.11.2
61
+ 1.12.5
data/README.md CHANGED
@@ -4,6 +4,15 @@ Core_Ext
4
4
  This project adds some new methods to the core ruby classes
5
5
 
6
6
  ## Array
7
+ ###map_to_hash
8
+ map returning a hash with the original array for keys
9
+
10
+ ```ruby
11
+ array = %w(a ab)
12
+ arrays.map_to_hash { |val| val.length }
13
+ { 'a' => 1, 'b' => 2 }
14
+ ```
15
+
7
16
  ### chain_map
8
17
  applies map in a chain
9
18
 
@@ -31,6 +40,15 @@ returns
31
40
  ```
32
41
 
33
42
  ## Hash
43
+ ###map_to_hash
44
+ map returning a hash with the original keys
45
+
46
+ ```ruby
47
+ hash = { a: 1, b: 2 }
48
+ hash.map_to_hash { |k, v| "#{k}_#{v}" }
49
+ { a: "a_1", b: "b_2" }
50
+ ```
51
+
34
52
  ### chain_fetch
35
53
  Applies fetch in a chain
36
54
 
@@ -140,4 +158,5 @@ CLeans empty values from a hash
140
158
  returns
141
159
  ```ruby
142
160
  {}
143
- ```
161
+ ```
162
+
@@ -1,5 +1,5 @@
1
1
  module Bidu
2
2
  module CoreExt
3
- VERSION = '1.2.2'
3
+ VERSION = '1.2.3'
4
4
  end
5
5
  end
data/lib/enumerable.rb CHANGED
@@ -28,6 +28,14 @@ module Enumerable
28
28
  mapped.select { |e| e }
29
29
  end
30
30
 
31
+ def map_to_hash
32
+ {}.tap do |hash|
33
+ each do |element|
34
+ hash[element] = yield(element)
35
+ end
36
+ end
37
+ end
38
+
31
39
  private
32
40
 
33
41
  def empty_value?(v)
data/lib/hash.rb CHANGED
@@ -34,6 +34,14 @@ class Hash
34
34
  end
35
35
  end
36
36
 
37
+ def map_to_hash
38
+ {}.tap do |hash|
39
+ each do |k, v|
40
+ hash[k] = yield(k, v)
41
+ end
42
+ end
43
+ end
44
+
37
45
  def remap_keys(remap)
38
46
  dup.remap_keys!(remap)
39
47
  end
@@ -145,4 +145,85 @@ describe Array do
145
145
  end
146
146
  end
147
147
  end
148
+
149
+ describe '#map_to_hash' do
150
+ context 'whe subject is an array' do
151
+ let(:subject) { %w(word1 wooord2) }
152
+ let(:mapping_block) { proc{ |word| word.length } }
153
+ let(:mapped) { subject.map_to_hash(&mapping_block) }
154
+ let(:expected) { { 'word1' => 5, 'wooord2' => 7 } }
155
+
156
+ it { expect(mapped).to be_a(Hash) }
157
+
158
+ it 'has the original array as keys' do
159
+ expect(mapped.keys).to eq(subject)
160
+ end
161
+
162
+ it 'has the mapped values as values' do
163
+ expect(mapped.values).to eq(subject.map(&mapping_block))
164
+ end
165
+
166
+ it 'correctly map keys to value' do
167
+ expect(mapped).to eq(expected)
168
+ end
169
+
170
+ context 'whe subject is an array' do
171
+ let(:subject) { [%w(w1), %w(w2 w3)] }
172
+ let(:mapped) { subject.map_to_hash(&mapping_block) }
173
+ let(:expected) { { %w(w1) => 1, %w(w2 w3) => 2 } }
174
+
175
+
176
+ it 'has the original array as keys' do
177
+ expect(mapped.keys).to eq(subject)
178
+ end
179
+
180
+ it 'has the mapped values as values' do
181
+ expect(mapped.values).to eq(subject.map(&mapping_block))
182
+ end
183
+
184
+ it 'correctly map keys to value' do
185
+ expect(mapped).to eq(expected)
186
+ end
187
+ end
188
+ end
189
+
190
+ context 'whe subject is a hash' do
191
+ let(:subject) { { a: 1, b: 2 } }
192
+ let(:mapping_block) { proc{ |k, v| "#{k}_#{v}" } }
193
+ let(:mapped) { subject.map_to_hash(&mapping_block) }
194
+ let(:expected) { { a: 'a_1', b: 'b_2' } }
195
+
196
+ it { expect(mapped).to be_a(Hash) }
197
+
198
+ it 'has the original keys as keys' do
199
+ expect(mapped.keys).to eq(subject.keys)
200
+ end
201
+
202
+ it 'has the mapped values as values' do
203
+ expect(mapped.values).to eq(subject.map(&mapping_block))
204
+ end
205
+
206
+ it 'correctly map keys to value' do
207
+ expect(mapped).to eq(expected)
208
+ end
209
+
210
+ context 'when hash uses arrays for keys' do
211
+ let(:subject) { { [:a, :b] => 1, [:c, :d] => 2 } }
212
+ let(:mapping_block) { proc{ |k, v| "#{k.join('_')}_#{v}" } }
213
+ let(:expected) { { [:a, :b]=> 'a_b_1', [:c, :d] => 'c_d_2' } }
214
+
215
+ it 'has the original keys as keys' do
216
+ expect(mapped.keys).to eq(subject.keys)
217
+ end
218
+
219
+ it 'has the mapped values as values' do
220
+ expect(mapped.values).to eq(subject.map(&mapping_block))
221
+ end
222
+
223
+ it 'correctly map keys to value' do
224
+ expect(mapped).to eq(expected)
225
+ end
226
+ end
227
+ end
228
+ end
148
229
  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.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bidu Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-16 00:00:00.000000000 Z
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport