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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cc4111bafde1bf250b1fdfcf6e026e57cc8a16a
4
- data.tar.gz: 545491238c25aacb3ea58a585f913a9c9f383c2e
3
+ metadata.gz: 8a7fe060dfcbb892add553d800bd8575bd621487
4
+ data.tar.gz: cb03eada7845be91083740aa56668ad708ee95f7
5
5
  SHA512:
6
- metadata.gz: 96fa60aa01a6fa9178c7a6645256f08c5d00c950fbec2ad2fb417eaadfcc01bd7aa466d01f2e36e722155586d786506ddebdef449cbf391b9209f81f296175a6
7
- data.tar.gz: 9f75a7b6546880b6d4bd2e70237c59449fc7988ac8dfae3dd0d56d591b27a2d70c18897bd40ae206a9f1cb55a806c784cc7a39a165f0fc98ea7ed374c4d9d51a
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
- def self.included(base)
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(' as ')[-1].to_sym
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
- Hash[formatted_keys.zip(row)]
20
+ value = HashWithIndifferentAccess[formatted_keys.zip(row)]
21
+ yield(value) if block_given
22
+ value
22
23
  end
23
24
  end
24
25
 
@@ -1,3 +1,3 @@
1
1
  module PluckToHash
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec", "~> 3.2"
25
25
 
26
26
  spec.add_dependency "activerecord", ">= 4.0.2"
27
+ spec.add_dependency "activesupport", ">= 4.0.2"
27
28
  end
@@ -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(Hash)
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(Hash)
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(Hash)
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(Hash)
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.1.4
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-05-16 00:00:00.000000000 Z
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: