pluck_to_hash 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pluck_to_hash.rb +7 -6
- data/lib/pluck_to_hash/version.rb +1 -1
- data/pluck_to_hash.gemspec +1 -0
- data/spec/pluck_to_hash_spec.rb +29 -10
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a7fe060dfcbb892add553d800bd8575bd621487
|
4
|
+
data.tar.gz: cb03eada7845be91083740aa56668ad708ee95f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebddbc03cf70b455ea0f3194cbd028fb562f5f3bb058822c9ddb3981e90b7378985b781ba7d6ccf369db5e371724b3715f389f0321e6c95ebb0f9ba537eaa1c8
|
7
|
+
data.tar.gz: a932bd372a7733578182362ffc00c67d9add36000820518aeca94b8a244d4bd89e8566464023e907f96292dda83591e0c541ac488cf09efcb42f029c511c7629
|
data/lib/pluck_to_hash.rb
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
require_relative "./pluck_to_hash/version"
|
2
2
|
|
3
3
|
module PluckToHash
|
4
|
-
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
end
|
4
|
+
extend ActiveSupport::Concern
|
7
5
|
|
8
6
|
module ClassMethods
|
9
7
|
def pluck_to_hash(*keys)
|
8
|
+
block_given = block_given?
|
9
|
+
keys = column_names if keys.blank?
|
10
10
|
formatted_keys = keys.map do |k|
|
11
11
|
case k
|
12
12
|
when String
|
13
|
-
k.split(
|
13
|
+
k.split(/ as /i)[-1].to_sym
|
14
14
|
when Symbol
|
15
15
|
k
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
19
18
|
pluck(*keys).map do |row|
|
20
19
|
row = [row] if keys.size == 1
|
21
|
-
|
20
|
+
value = HashWithIndifferentAccess[formatted_keys.zip(row)]
|
21
|
+
yield(value) if block_given
|
22
|
+
value
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
data/pluck_to_hash.gemspec
CHANGED
data/spec/pluck_to_hash_spec.rb
CHANGED
@@ -12,11 +12,30 @@ describe 'PluckToHash' do
|
|
12
12
|
|
13
13
|
it 'plucks the ids of the objects to a hash correctly' do
|
14
14
|
TestModel.all.pluck_to_hash(:id).each do |hash|
|
15
|
-
expect(hash.class).to eq(
|
15
|
+
expect(hash.class).to eq(HashWithIndifferentAccess)
|
16
16
|
expect(hash).to have_key(:id)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
it 'pluck field with lowercase alias' do
|
21
|
+
TestModel.all.pluck_to_hash('id as Something').each do |hash|
|
22
|
+
expect(hash).to have_key(:Something)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'pluck field with uppercase alias' do
|
27
|
+
TestModel.all.pluck_to_hash('id AS otherfield').each do |hash|
|
28
|
+
expect(hash).to have_key(:otherfield)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'pluck field with mixedcase alias' do
|
33
|
+
TestModel.all.pluck_to_hash('id As anotherfield').each do |hash|
|
34
|
+
expect(hash).to have_key(:anotherfield)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
20
39
|
context 'the model does not have the attribute specified' do
|
21
40
|
it 'raises an error' do
|
22
41
|
expect do
|
@@ -37,7 +56,7 @@ describe 'PluckToHash' do
|
|
37
56
|
context 'specifying multiple attributes' do
|
38
57
|
it 'returns a hash with both attributes' do
|
39
58
|
TestModel.all.pluck_to_hash(:id, :test_attribute).each do |hash|
|
40
|
-
expect(hash.class).to eq(
|
59
|
+
expect(hash.class).to eq(HashWithIndifferentAccess)
|
41
60
|
expect(hash).to have_key(:id)
|
42
61
|
expect(hash).to have_key(:test_attribute)
|
43
62
|
end
|
@@ -56,18 +75,18 @@ describe 'PluckToHash' do
|
|
56
75
|
it 'plucks the hash correctly' do
|
57
76
|
result = TestModel.pluck_to_hash(:serialized_attribute)
|
58
77
|
expect(result).to eq [
|
59
|
-
{ serialized_attribute: [] },
|
60
|
-
{ serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'] },
|
61
|
-
{ serialized_attribute: ['Comonad'] }
|
78
|
+
{ serialized_attribute: [] }.with_indifferent_access,
|
79
|
+
{ serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'] }.with_indifferent_access,
|
80
|
+
{ serialized_attribute: ['Comonad'] }.with_indifferent_access
|
62
81
|
]
|
63
82
|
end
|
64
83
|
|
65
84
|
it 'plucks a hash with multiple attributes' do
|
66
85
|
result = TestModel.pluck_to_hash(:test_attribute, :serialized_attribute)
|
67
86
|
expect(result).to eq [
|
68
|
-
{ test_attribute: nil, serialized_attribute: [] },
|
69
|
-
{ test_attribute: nil, serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'] },
|
70
|
-
{ test_attribute: nil, serialized_attribute: ['Comonad'] }
|
87
|
+
{ test_attribute: nil, serialized_attribute: [] }.with_indifferent_access,
|
88
|
+
{ test_attribute: nil, serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'] }.with_indifferent_access,
|
89
|
+
{ test_attribute: nil, serialized_attribute: ['Comonad'] }.with_indifferent_access
|
71
90
|
]
|
72
91
|
end
|
73
92
|
end
|
@@ -83,7 +102,7 @@ describe 'PluckToHash' do
|
|
83
102
|
|
84
103
|
it 'plucks the ids of the objects to a hash correctly' do
|
85
104
|
TestModel.all.pluck_h(:id).each do |hash|
|
86
|
-
expect(hash.class).to eq(
|
105
|
+
expect(hash.class).to eq(HashWithIndifferentAccess)
|
87
106
|
expect(hash).to have_key(:id)
|
88
107
|
end
|
89
108
|
end
|
@@ -108,7 +127,7 @@ describe 'PluckToHash' do
|
|
108
127
|
context 'specifying multiple attributes' do
|
109
128
|
it 'returns a hash with both attributes' do
|
110
129
|
TestModel.all.pluck_h(:id, :test_attribute).each do |hash|
|
111
|
-
expect(hash.class).to eq(
|
130
|
+
expect(hash.class).to eq(HashWithIndifferentAccess)
|
112
131
|
expect(hash).to have_key(:id)
|
113
132
|
expect(hash).to have_key(:test_attribute)
|
114
133
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluck_to_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Girish S
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 4.0.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 4.0.2
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 4.0.2
|
83
97
|
description: Extend ActiveRecord pluck to return hash instead of an array. Useful
|
84
98
|
when plucking multiple columns.
|
85
99
|
email:
|