darthjee-core_ext 1.2.6 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ARRAY_README.md +35 -0
- data/ENUMERABLE_README.md +12 -0
- data/Gemfile.lock +1 -1
- data/HASH_README.md +252 -0
- data/README.md +9 -167
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/spec/lib/array_spec.rb +2 -81
- data/spec/lib/hash_spec.rb +1 -0
- data/spec/support/shared_examples/map_to_hash.rb +86 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e119db2a6c2d8eb97f42b48b82500d93f81665fb
|
4
|
+
data.tar.gz: 9fa9cbda6ad8a1feef1f57eb917df0b0391d8a52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f904ea861ca6e47029632718356f0802d4f828bb4343ea5fd14db142406a6ec95c4462c28b2010bab2eb410af6b71539a08e87e34ec5cc849062239f8f05529
|
7
|
+
data.tar.gz: 90afdb065d5f08e91ee76927ff2487d0f848ae903af8c036f42ac89e318a1b34b2b24f2343165b2aff9059197a6a066012763252159028642e57518a7039af8f
|
data/ARRAY_README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
## Array
|
2
|
+
### #map_to_hash
|
3
|
+
map returning a hash with the original array for keys
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
array = %w(a ab)
|
7
|
+
arrays.map_to_hash { |val| val.length }
|
8
|
+
{ 'a' => 1, 'b' => 2 }
|
9
|
+
```
|
10
|
+
|
11
|
+
### #chain_map
|
12
|
+
applies map in a chain
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
array = [ :a, :long_name, :sym ]
|
16
|
+
array.chain_map(:to_s, :size, :to_s)
|
17
|
+
[ '1', '9', '3' ]
|
18
|
+
```
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
array = [ :a, :long_name, :sym ]
|
22
|
+
array.chain_map(:to_s, :size) { |v| "final: #{v}" }
|
23
|
+
[ 'final: 1', 'final: 9', 'final: 3' ]
|
24
|
+
```
|
25
|
+
|
26
|
+
### #as_hash
|
27
|
+
Creates a hash from the array using the argumen array as keys
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
[1, 2, 3].as_hash %w(a b c)
|
31
|
+
```
|
32
|
+
returns
|
33
|
+
```ruby
|
34
|
+
{ 'a' => 1, 'b' => 2, 'c' => 3 } }
|
35
|
+
```
|
data/Gemfile.lock
CHANGED
data/HASH_README.md
ADDED
@@ -0,0 +1,252 @@
|
|
1
|
+
## Hash
|
2
|
+
### #map_to_hash
|
3
|
+
map returning a hash with the original keys
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
hash = { a: 1, b: 2 }
|
7
|
+
hash.map_to_hash { |k, v| "#{k}_#{v}" }
|
8
|
+
{ a: "a_1", b: "b_2" }
|
9
|
+
```
|
10
|
+
|
11
|
+
### #chain_fetch
|
12
|
+
Applies fetch in a chain
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
{ a: { b: { c: { d: 10 } } } }.chain_fetch(:a, :b, :c, :d)
|
16
|
+
10
|
17
|
+
```
|
18
|
+
|
19
|
+
A block can be passed so that when a key is not found, the block will define the value to be returned
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
h = { a: { b: { c: { d: 10 } } } }
|
23
|
+
h.chain_fetch(:a, :x, :y, :z) { |key, missed_keys| "returned #{key}" }
|
24
|
+
'returned x'
|
25
|
+
```
|
26
|
+
|
27
|
+
### #squash
|
28
|
+
Squash a deep hash into a simple level hash
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
{ a: { b:1 } }.squash
|
32
|
+
```
|
33
|
+
returns
|
34
|
+
```ruby
|
35
|
+
{ 'a.b' => 1 }
|
36
|
+
```
|
37
|
+
|
38
|
+
### #to_deep_hash
|
39
|
+
Changes a hash spliting keys into inner hashs
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
{ 'a.b' => 1 }.to_deep_hash
|
43
|
+
```
|
44
|
+
returns
|
45
|
+
```ruby
|
46
|
+
{ 'a' => { 'b' => 1 } }
|
47
|
+
```
|
48
|
+
|
49
|
+
### #change_keys
|
50
|
+
Change the array keys using a block accepting parameters:
|
51
|
+
- recursive: when true, does it recursivly through inner arrays (default: true)
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
{ ca_b: 1, k: [{ a_b: 1 }] }.change_keys { |k| k.to_s.upcase }
|
55
|
+
```
|
56
|
+
returns
|
57
|
+
```ruby
|
58
|
+
{"CA_B"=>1, "K"=>[{"A_B"=>1}]}
|
59
|
+
```
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
{ ca_b: 1, k: [{ a_b: 1 }] }.change_keys(recursive: false) { |k| k.to_s.upcase }
|
63
|
+
```
|
64
|
+
returns
|
65
|
+
```ruby
|
66
|
+
{"CA_B"=>1, "K"=>[{:a_b=>1}]}
|
67
|
+
```
|
68
|
+
|
69
|
+
### #chain_change_keys
|
70
|
+
Change the hash keys usin a chained method call
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
{ ca_b: 1 }.chain_change_keys(:to_s, :upcase, :to_sym)
|
74
|
+
```
|
75
|
+
returns
|
76
|
+
```ruby
|
77
|
+
{ CA_B: 1 }
|
78
|
+
```
|
79
|
+
|
80
|
+
### #camelize_keys
|
81
|
+
Change the keys camelizing them and accepting parameters:
|
82
|
+
- uppercase_first_letter: Use the java or javascript format (default: true)
|
83
|
+
- recursive: when true, does it recursivly through inner arrays (default: true)
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
{ ca_b: 1, k: [{ a_b: 1 }] }.camelize_keys
|
87
|
+
```
|
88
|
+
returns
|
89
|
+
```ruby
|
90
|
+
{:CaB=>1, :K=>[{:AB=>1}]}
|
91
|
+
```
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
{ ca_b: 1, k: [{ a_b: 1 }] }.camelize_keys(recursive: false)
|
95
|
+
```
|
96
|
+
returns
|
97
|
+
```ruby
|
98
|
+
{:CaB=>1, :K=>[{:a_b=>1}]}
|
99
|
+
```
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
{ ca_b: 1, k: [{ a_b: 1 }] }.camelize_keys(uppercase_first_letter: false)
|
103
|
+
```
|
104
|
+
returns
|
105
|
+
```ruby
|
106
|
+
{:caB=>1, :k=>[{:aB=>1}]}
|
107
|
+
```
|
108
|
+
|
109
|
+
### #lower_camelize_keys
|
110
|
+
Alias for [#camelize_keys](camelize_keys)(uppercase_first_letter: false)
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
{ ca_b: 1, k: [{ a_b: 1 }] }.lower_camelize_keys
|
114
|
+
```
|
115
|
+
returns
|
116
|
+
```ruby
|
117
|
+
{:caB=>1, :k=>[{:aB=>1}]}
|
118
|
+
```
|
119
|
+
|
120
|
+
### #underscore_keys
|
121
|
+
Change the keys from camelcase to snakecase (underscore)
|
122
|
+
- recursive: when true, does it recursivly through inner arrays (default: true)
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
{ Ca_B: 1, 'kB' => [{ KeysHash: 1 }] }.underscore_keys
|
126
|
+
```
|
127
|
+
returns
|
128
|
+
```ruby
|
129
|
+
{ca_b: 1, "k_b"=>[{keys_hash: 1}]}
|
130
|
+
```
|
131
|
+
|
132
|
+
### #change_values
|
133
|
+
Change the values of the array accepting parametes:
|
134
|
+
- recursive: when true, does it recursivly through inner arrays and hashes (default: true)
|
135
|
+
- skip_inner: when true, do not call the block for iterators such as Hash and Arrays (default: true)
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
{ a: 1, b: [{ c: 2 }] }.change_values { |v| (v+1).to_s }
|
139
|
+
```
|
140
|
+
returns
|
141
|
+
```ruby
|
142
|
+
{ a: '2' b: [{ c: '3' }] }
|
143
|
+
```
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
{ a: 1, b: [{ c: 2 }] }.change_values(recursive: false) { |v| (v+1).to_s }
|
147
|
+
```
|
148
|
+
returns
|
149
|
+
```ruby
|
150
|
+
{ a: '2' b: [{ c: 2 }] }
|
151
|
+
```
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
{ a: 1, b: [{ c: 2 }] }.change_values(skip_inner: false) do |v|
|
155
|
+
v.is_a?(Integer) ? (v+1).to_s : v.class
|
156
|
+
end
|
157
|
+
```
|
158
|
+
returns
|
159
|
+
```ruby
|
160
|
+
{ a: '2' b: Array }
|
161
|
+
```
|
162
|
+
|
163
|
+
### #prepend_to_keys
|
164
|
+
Change each keys prepending an string
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
{ key: 1 }.prepend_to_keys 'scope:'
|
168
|
+
```
|
169
|
+
returns
|
170
|
+
```ruby
|
171
|
+
{ :'scope:key' => 1 }
|
172
|
+
```
|
173
|
+
### #append_to_keys
|
174
|
+
Change each keys appending an string
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
{ key: 1 }.append_to_keys 's'
|
178
|
+
```
|
179
|
+
returns
|
180
|
+
```ruby
|
181
|
+
{ keys: 1 }
|
182
|
+
```
|
183
|
+
|
184
|
+
### #sort_keys
|
185
|
+
Sort the hash usig the keys
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
{ b:1, a:2 }.sort_keys
|
189
|
+
```
|
190
|
+
returns
|
191
|
+
```ruby
|
192
|
+
{ a:2, b:1 }
|
193
|
+
```
|
194
|
+
|
195
|
+
### #map_to_hash
|
196
|
+
map returning a hash with the original keys for keys
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
hash = { a: 1, b: 2 }
|
200
|
+
hash.map_to_hash { |k, v| "#{k}_#{v}" }
|
201
|
+
{ a: 'a_1', b: 'b_2' }
|
202
|
+
```
|
203
|
+
|
204
|
+
### #remap_keys
|
205
|
+
Changes the keys of the hash based on a map of { old: new } value
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
hash = { a: 1, b: 2 }
|
209
|
+
hash.remap_keys(a: :c, d: :e)
|
210
|
+
{ c: 1, b: 2, e: nil }
|
211
|
+
```
|
212
|
+
|
213
|
+
### #exclusive_merge
|
214
|
+
Like #merge but only for existing keys
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
{ a: 1, b: 2 }.exclusive_merge(b: 3, c: 4)
|
218
|
+
{ a: 1, b: 3 }
|
219
|
+
```
|
220
|
+
|
221
|
+
### #clean
|
222
|
+
Cleans empty values from a hash
|
223
|
+
```ruby
|
224
|
+
{ a: 1, b: [], c: nil, d: {}, e: '', f: { b: [], c: nil, d: {}, e: '' } }.clean
|
225
|
+
```
|
226
|
+
returns
|
227
|
+
```ruby
|
228
|
+
{}
|
229
|
+
```
|
230
|
+
|
231
|
+
### #map_and_find
|
232
|
+
Operates like map, but will stop as soon as a non false value is found returning the value found
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
class Ob
|
236
|
+
attr_reader :v
|
237
|
+
def initialize(v = nil)
|
238
|
+
@v = v
|
239
|
+
end
|
240
|
+
end
|
241
|
+
hash = { a: Ob.new, b: Ob.new(false), c: Ob.new(1), d: Ob.new(3) }
|
242
|
+
```
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
hash.map_and_find { |k,o| o.v }
|
246
|
+
1
|
247
|
+
```
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
hash.map_and_find { |k,o| o.v && k }
|
251
|
+
:c
|
252
|
+
```
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
Core_Ext
|
2
2
|
========
|
3
3
|
|
4
|
+
# Usage
|
4
5
|
This project adds some new methods to the core ruby classes
|
5
6
|
|
6
7
|
To use core-ext either intall directly
|
@@ -9,180 +10,21 @@ To use core-ext either intall directly
|
|
9
10
|
gem install darthjee-core_ext
|
10
11
|
```
|
11
12
|
|
13
|
+
or add it to Gemfile
|
14
|
+
|
12
15
|
```
|
13
|
-
gem '
|
16
|
+
gem 'darthjee-core_ext'
|
14
17
|
```
|
15
18
|
|
16
19
|
```console
|
17
|
-
bundle install
|
20
|
+
bundle install darthjee-core_ext
|
18
21
|
```
|
19
22
|
|
23
|
+
#methods added
|
20
24
|
|
21
25
|
## Array
|
22
|
-
|
23
|
-
map returning a hash with the original array for keys
|
24
|
-
|
25
|
-
```ruby
|
26
|
-
array = %w(a ab)
|
27
|
-
arrays.map_to_hash { |val| val.length }
|
28
|
-
{ 'a' => 1, 'b' => 2 }
|
29
|
-
```
|
30
|
-
|
31
|
-
### chain_map
|
32
|
-
applies map in a chain
|
33
|
-
|
34
|
-
```ruby
|
35
|
-
array = [ :a, :long_name, :sym ]
|
36
|
-
array.chain_map(:to_s, :size, :to_s)
|
37
|
-
[ '1', '9', '3' ]
|
38
|
-
```
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
array = [ :a, :long_name, :sym ]
|
42
|
-
array.chain_map(:to_s, :size) { |v| "final: #{v}" }
|
43
|
-
[ 'final: 1', 'final: 9', 'final: 3' ]
|
44
|
-
```
|
45
|
-
|
46
|
-
### as_hash
|
47
|
-
Creates a hash from the array using the argumen array as keys
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
[1, 2, 3].as_hash %w(a b c)
|
51
|
-
```
|
52
|
-
returns
|
53
|
-
```ruby
|
54
|
-
{ 'a' => 1, 'b' => 2, 'c' => 3 } }
|
55
|
-
```
|
56
|
-
|
26
|
+
[ARRAY_README.md](ARRAY_README.md)
|
57
27
|
## Hash
|
58
|
-
|
59
|
-
map returning a hash with the original keys
|
60
|
-
|
61
|
-
```ruby
|
62
|
-
hash = { a: 1, b: 2 }
|
63
|
-
hash.map_to_hash { |k, v| "#{k}_#{v}" }
|
64
|
-
{ a: "a_1", b: "b_2" }
|
65
|
-
```
|
66
|
-
|
67
|
-
### chain_fetch
|
68
|
-
Applies fetch in a chain
|
69
|
-
|
70
|
-
```ruby
|
71
|
-
{ a: { b: { c: { d: 10 } } } }.chain_fetch(:a, :b, :c, :d)
|
72
|
-
10
|
73
|
-
```
|
74
|
-
```ruby
|
75
|
-
h = { a: { b: { c: { d: 10 } } } }
|
76
|
-
h.chain_fetch(:a, :x, :y, :z) { |key, missed_keys| "returned #{key}" }
|
77
|
-
'returned x'
|
78
|
-
```
|
79
|
-
|
80
|
-
### squash
|
81
|
-
Squash a deep hash into a simple level hash
|
82
|
-
|
83
|
-
```ruby
|
84
|
-
{ a: { b:1 } }.squash
|
85
|
-
```
|
86
|
-
returns
|
87
|
-
```ruby
|
88
|
-
{ 'a.b' => 1 }
|
89
|
-
```
|
90
|
-
|
91
|
-
### to_deep_hash
|
92
|
-
Changes a hash spliting keys into inner hashs
|
93
|
-
|
94
|
-
```ruby
|
95
|
-
{ 'a.b' => 1 }.to_deep_hash
|
96
|
-
```
|
97
|
-
returns
|
98
|
-
```ruby
|
99
|
-
{ 'a' => { 'b' => 1 } }
|
100
|
-
```
|
101
|
-
|
102
|
-
### camelize_keys
|
103
|
-
Change the keys camelizing them
|
104
|
-
|
105
|
-
```ruby
|
106
|
-
{ ca_b: 1 }.camelize_keys
|
107
|
-
```
|
108
|
-
returns
|
109
|
-
```ruby
|
110
|
-
{ CaB: 1 }
|
111
|
-
```
|
112
|
-
|
113
|
-
### change_keys
|
114
|
-
Change the array keys using a block
|
115
|
-
|
116
|
-
```ruby
|
117
|
-
{ ca_b: 1 }.change_keys { |k| k.to_s.upcase }
|
118
|
-
```
|
119
|
-
returns
|
120
|
-
```ruby
|
121
|
-
{ 'CA_B' => 1 }
|
122
|
-
```
|
123
|
-
|
124
|
-
### chain_change_keys
|
125
|
-
Change the hash keys usin a chained method call
|
126
|
-
|
127
|
-
```ruby
|
128
|
-
{ ca_b: 1 }.chain_change_keys(:to_s, :upcase, :to_sym)
|
129
|
-
```
|
130
|
-
returns
|
131
|
-
```ruby
|
132
|
-
{ CA_B: 1 }
|
133
|
-
```
|
134
|
-
|
135
|
-
### change_values
|
136
|
-
Change the values of the array
|
137
|
-
```ruby
|
138
|
-
{ a: 1 }.change_keys { |v| (v+1).to_s }
|
139
|
-
```
|
140
|
-
returns
|
141
|
-
```ruby
|
142
|
-
{ a: '2' }
|
143
|
-
```
|
144
|
-
|
145
|
-
### prepend_to_keys
|
146
|
-
Change each keys prepending an string
|
147
|
-
|
148
|
-
```ruby
|
149
|
-
{ key: 1 }.prepend_to_keys 'scope:'
|
150
|
-
```
|
151
|
-
returns
|
152
|
-
```ruby
|
153
|
-
{ :'scope:key' => 1 }
|
154
|
-
```
|
155
|
-
### append_to_keys
|
156
|
-
Change each keys appending an string
|
157
|
-
|
158
|
-
```ruby
|
159
|
-
{ key: 1 }.append_to_keys 's'
|
160
|
-
```
|
161
|
-
returns
|
162
|
-
```ruby
|
163
|
-
{ keys: 1 }
|
164
|
-
```
|
165
|
-
|
166
|
-
### sort_keys
|
167
|
-
Sort the hash usig the keys
|
168
|
-
|
169
|
-
```ruby
|
170
|
-
{ b:1, a:2 }.sort_keys
|
171
|
-
```
|
172
|
-
returns
|
173
|
-
```ruby
|
174
|
-
{ a:2, b:1 }
|
175
|
-
```
|
176
|
-
|
28
|
+
[HASH_README.md](HASH_README.md)
|
177
29
|
## Enumerable
|
178
|
-
|
179
|
-
## clean!
|
180
|
-
CLeans empty values from a hash
|
181
|
-
```ruby
|
182
|
-
{ a: 1, b: [], c: nil, d: {}, e: '', f: { b: [], c: nil, d: {}, e: '' } }.clean!
|
183
|
-
```
|
184
|
-
returns
|
185
|
-
```ruby
|
186
|
-
{}
|
187
|
-
```
|
188
|
-
|
30
|
+
[ENUMERABLE_README.md](ENUMERABLE_README.md)
|
data/spec/lib/array_spec.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
+
it_behaves_like 'an array with map_to_hash method'
|
5
|
+
|
4
6
|
describe '#chain_map' do
|
5
7
|
let(:array) { [ :a, :long_name, :sym ] }
|
6
8
|
let(:mapped) { array.chain_map(:to_s, :size, :to_s) }
|
@@ -145,85 +147,4 @@ describe Array do
|
|
145
147
|
end
|
146
148
|
end
|
147
149
|
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
|
229
150
|
end
|
data/spec/lib/hash_spec.rb
CHANGED
@@ -9,6 +9,7 @@ describe Hash do
|
|
9
9
|
it_behaves_like 'a class with change_values method'
|
10
10
|
it_behaves_like 'a class with remap method'
|
11
11
|
it_behaves_like 'an object with chain_fetch method'
|
12
|
+
it_behaves_like 'a hash with map_to_hash method'
|
12
13
|
|
13
14
|
describe :squash do
|
14
15
|
let(:hash) { { a: { b: 1, c: { d: 2 } } } }
|
@@ -0,0 +1,86 @@
|
|
1
|
+
shared_examples 'an array with map_to_hash method' do
|
2
|
+
describe '#map_to_hash' do
|
3
|
+
let(:subject) { %w(word1 wooord2) }
|
4
|
+
let(:mapping_block) { proc{ |word| word.length } }
|
5
|
+
let(:mapped) { subject.map_to_hash(&mapping_block) }
|
6
|
+
let(:expected) { { 'word1' => 5, 'wooord2' => 7 } }
|
7
|
+
|
8
|
+
it { expect(mapped).to be_a(Hash) }
|
9
|
+
|
10
|
+
it 'has the original array as keys' do
|
11
|
+
expect(mapped.keys).to eq(subject)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'has the mapped values as values' do
|
15
|
+
expect(mapped.values).to eq(subject.map(&mapping_block))
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'correctly map keys to value' do
|
19
|
+
expect(mapped).to eq(expected)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'whe subject is an array' do
|
23
|
+
let(:subject) { [%w(w1), %w(w2 w3)] }
|
24
|
+
let(:mapped) { subject.map_to_hash(&mapping_block) }
|
25
|
+
let(:expected) { { %w(w1) => 1, %w(w2 w3) => 2 } }
|
26
|
+
|
27
|
+
|
28
|
+
it 'has the original array as keys' do
|
29
|
+
expect(mapped.keys).to eq(subject)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'has the mapped values as values' do
|
33
|
+
expect(mapped.values).to eq(subject.map(&mapping_block))
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'correctly map keys to value' do
|
37
|
+
expect(mapped).to eq(expected)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
shared_examples 'a hash with map_to_hash method' do
|
44
|
+
describe '#map_to_hash' do
|
45
|
+
let(:subject) { { a: 1, b: 2 } }
|
46
|
+
let(:mapping_block) { proc{ |k, v| "#{k}_#{v}" } }
|
47
|
+
let(:mapped) { subject.map_to_hash(&mapping_block) }
|
48
|
+
let(:expected) { { a: 'a_1', b: 'b_2' } }
|
49
|
+
|
50
|
+
it { expect(mapped).to be_a(Hash) }
|
51
|
+
|
52
|
+
it do
|
53
|
+
expect { subject.map_to_hash(&mapping_block) }.not_to change { subject }
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'has the original keys as keys' do
|
57
|
+
expect(mapped.keys).to eq(subject.keys)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'has the mapped values as values' do
|
61
|
+
expect(mapped.values).to eq(subject.map(&mapping_block))
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'correctly map keys to value' do
|
65
|
+
expect(mapped).to eq(expected)
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when hash uses arrays for keys' do
|
69
|
+
let(:subject) { { [:a, :b] => 1, [:c, :d] => 2 } }
|
70
|
+
let(:mapping_block) { proc{ |k, v| "#{k.join('_')}_#{v}" } }
|
71
|
+
let(:expected) { { [:a, :b]=> 'a_b_1', [:c, :d] => 'c_d_2' } }
|
72
|
+
|
73
|
+
it 'has the original keys as keys' do
|
74
|
+
expect(mapped.keys).to eq(subject.keys)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'has the mapped values as values' do
|
78
|
+
expect(mapped.values).to eq(subject.map(&mapping_block))
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'correctly map keys to value' do
|
82
|
+
expect(mapped).to eq(expected)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: darthjee-core_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -131,8 +131,11 @@ extra_rdoc_files: []
|
|
131
131
|
files:
|
132
132
|
- ".gitignore"
|
133
133
|
- ".rspec"
|
134
|
+
- ARRAY_README.md
|
135
|
+
- ENUMERABLE_README.md
|
134
136
|
- Gemfile
|
135
137
|
- Gemfile.lock
|
138
|
+
- HASH_README.md
|
136
139
|
- LICENSE
|
137
140
|
- README.md
|
138
141
|
- Rakefile
|
@@ -168,6 +171,7 @@ files:
|
|
168
171
|
- spec/support/shared_examples/keys_appender.rb
|
169
172
|
- spec/support/shared_examples/keys_camelizer.rb
|
170
173
|
- spec/support/shared_examples/keys_underscorer.rb
|
174
|
+
- spec/support/shared_examples/map_to_hash.rb
|
171
175
|
- spec/support/shared_examples/remap.rb
|
172
176
|
- spec/support/shared_examples/value_changer.rb
|
173
177
|
homepage:
|
@@ -213,5 +217,6 @@ test_files:
|
|
213
217
|
- spec/support/shared_examples/keys_appender.rb
|
214
218
|
- spec/support/shared_examples/keys_camelizer.rb
|
215
219
|
- spec/support/shared_examples/keys_underscorer.rb
|
220
|
+
- spec/support/shared_examples/map_to_hash.rb
|
216
221
|
- spec/support/shared_examples/remap.rb
|
217
222
|
- spec/support/shared_examples/value_changer.rb
|