darthjee-core_ext 1.4.0 → 1.4.1

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: d2345bf7cdf843714d515bb2bee9466683401837
4
- data.tar.gz: 5e05050eabe46f189e9a1bd7dea334de6589fdbd
3
+ metadata.gz: dd5dd16fc0fc9a7a42a9ab2947bd5b2826545c8c
4
+ data.tar.gz: 66d35f5d8765636a1d47855a746505d0631246b2
5
5
  SHA512:
6
- metadata.gz: 884c6906d6ecf48a9f2daeafe4802506e1add3cc46f49335dc6adda8d408bcd0b9cfaa3ce1eceeeb16f9d82ce31b435cb82fdff2b37e194af08f77bc09272455
7
- data.tar.gz: 4c61c2a039d1af1309367b6058af1ce141abd8d37e10923f19fa15d5ba5bd161fb7fc6c789a883ef8b46c2f88e24d13f75b31846ff125cfb4945b6b1a612189d
6
+ metadata.gz: b44cc8d55182fc41c3374bf6fa63257ac95a693ecaaed69d9ac7d3438b820cc2fdb0927c42acc51ddd2b8d41ce9940322d1e0c77d38710c8e550541f4fefd99a
7
+ data.tar.gz: 6f2749c189ae42dde35ee2b836485d00612bec1def1f33303fd69a95e59f555d596cf3e1ee39f019931b3db559faa920f29ce884dd94bb77666e5a3e2f041f87
@@ -250,3 +250,16 @@ hash.map_and_find { |k,o| o.v }
250
250
  hash.map_and_find { |k,o| o.v && k }
251
251
  :c
252
252
  ```
253
+
254
+ ### #transpose
255
+ swap keys with values of the hash
256
+
257
+ ```ruby
258
+ { a: 1, b: :a, c: [2, 3] }.transpose
259
+ ```
260
+
261
+ returns
262
+
263
+ ```ruby
264
+ { 1 => :a, a: b, [2, 3] => :c }
265
+ ```
@@ -181,6 +181,23 @@ class Hash
181
181
  Hash::DeepHashConstructor.new(separator).deep_hash(self)
182
182
  end
183
183
 
184
+ def transpose!
185
+ aux = dup
186
+ keys.each { |k| self.delete(k) }
187
+ aux.each do |k, v|
188
+ self[v] = k
189
+ end
190
+ self
191
+ end
192
+
193
+ def transpose
194
+ {}.tap do |new_hash|
195
+ each do |k, v|
196
+ new_hash[v] = k
197
+ end
198
+ end
199
+ end
200
+
184
201
  private
185
202
 
186
203
  # changes the text of the keys
@@ -1,5 +1,5 @@
1
1
  module Darthjee
2
2
  module CoreExt
3
- VERSION = '1.4.0'
3
+ VERSION = '1.4.1'
4
4
  end
5
5
  end
@@ -10,6 +10,7 @@ describe Hash do
10
10
  it_behaves_like 'a class with remap method'
11
11
  it_behaves_like 'an object with chain_fetch method'
12
12
  it_behaves_like 'a hash with map_to_hash method'
13
+ it_behaves_like 'a class with transpose methods'
13
14
 
14
15
  describe :squash do
15
16
  let(:hash) { { a: { b: 1, c: { d: 2 } } } }
@@ -0,0 +1,100 @@
1
+ shared_examples 'a class with transpose method' do |method|
2
+ let(:hash) { { a: 1 } }
3
+
4
+ describe "##{method}" do
5
+ it 'swap keys with values' do
6
+ expect(hash.public_send(method)).to eq(1 => :a)
7
+ end
8
+
9
+ it 'works when repeating call' do
10
+ expect(hash.public_send(method).public_send(method)).to eq(hash)
11
+ end
12
+
13
+ context 'when hash has sub hasehs' do
14
+ let(:sub_hash) { { b: 1 } }
15
+ let(:hash) { { a: sub_hash } }
16
+
17
+ it 'do not work it recursively' do
18
+ expect(hash.public_send(method)).to eq(sub_hash => :a)
19
+ end
20
+
21
+ it 'works when repeating call' do
22
+ expect(hash.public_send(method).public_send(method)).to eq(hash)
23
+ end
24
+
25
+ context 'whe key is already a hash' do
26
+ let(:key) { { c: 2 } }
27
+ let(:hash) { { key => sub_hash } }
28
+
29
+ it 'swap keys with values' do
30
+ expect(hash.public_send(method)).to eq(sub_hash => key)
31
+ end
32
+
33
+ it 'works when repeating call' do
34
+ expect(hash.public_send(method).public_send(method)).to eq(hash)
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'when value is an array' do
40
+ let(:hash) { { a: [1, 2] } }
41
+
42
+ it 'swap keys with values' do
43
+ expect(hash.public_send(method)).to eq([1, 2] => :a)
44
+ end
45
+
46
+ it 'works when repeating call' do
47
+ expect(hash.public_send(method).public_send(method)).to eq(hash)
48
+ end
49
+
50
+ context 'when key is already an array' do
51
+ let(:hash) { { [1, 2] => [3, 4] } }
52
+
53
+ it 'swap keys with values' do
54
+ expect(hash.public_send(method)).to eq([3, 4] => [1, 2])
55
+ end
56
+
57
+ it 'works when repeating call' do
58
+ expect(hash.public_send(method).public_send(method)).to eq(hash)
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'when there is a clash of keys' do
64
+ let(:hash) { { a: :b, c: :b} }
65
+
66
+ it 'uses the last key for value' do
67
+ expect(hash.public_send(method)).to eq(b: :c)
68
+ end
69
+ end
70
+
71
+ context 'when there is a key which alreay has been defined' do
72
+ let(:hash) { { a: :b, b: :c} }
73
+
74
+ it 'does not override values' do
75
+ expect(hash.public_send(method)).to eq(b: :a, c: :b)
76
+ end
77
+
78
+ it 'works when repeating call' do
79
+ expect(hash.public_send(method).public_send(method)).to eq(hash)
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ shared_examples 'a class with transpose methods' do
86
+ it_behaves_like 'a class with transpose method', :transpose do
87
+ it do
88
+ expect do
89
+ hash.transpose
90
+ end.not_to change { hash }
91
+ end
92
+ end
93
+ it_behaves_like 'a class with transpose method', :transpose! do
94
+ it do
95
+ expect do
96
+ hash.transpose!
97
+ end.to change { hash }
98
+ end
99
+ end
100
+ 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.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-09 00:00:00.000000000 Z
11
+ date: 2017-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -175,6 +175,7 @@ files:
175
175
  - spec/support/shared_examples/date.rb
176
176
  - spec/support/shared_examples/expected.rb
177
177
  - spec/support/shared_examples/hash_keys_changer.rb
178
+ - spec/support/shared_examples/hash_transpose.rb
178
179
  - spec/support/shared_examples/keys_appender.rb
179
180
  - spec/support/shared_examples/keys_camelizer.rb
180
181
  - spec/support/shared_examples/keys_underscorer.rb
@@ -224,6 +225,7 @@ test_files:
224
225
  - spec/support/shared_examples/date.rb
225
226
  - spec/support/shared_examples/expected.rb
226
227
  - spec/support/shared_examples/hash_keys_changer.rb
228
+ - spec/support/shared_examples/hash_transpose.rb
227
229
  - spec/support/shared_examples/keys_appender.rb
228
230
  - spec/support/shared_examples/keys_camelizer.rb
229
231
  - spec/support/shared_examples/keys_underscorer.rb