pluck_to_hash 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -1
- data/lib/pluck_to_hash.rb +26 -7
- data/lib/pluck_to_hash/version.rb +1 -1
- data/spec/pluck_to_struct_spec.rb +80 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a3229df2da1185b057f0e9044706edf65e95c40
|
4
|
+
data.tar.gz: 6747bf279dc92fb4b16fb4e42a84186ee1b5e63b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b8c27695035f0ff794a34d5fd153728eb872aa693fef0cb078bc0326c00523c9f9d912a473a41925c14dfe6f2e0a662f7c8c1de4992e0029a786be44e6bb936
|
7
|
+
data.tar.gz: b3f5d9d7e24ffea05bf52c579cc381cf88b51f8280d41f805efb567dceb4b94866bb99f09ab795a31450d2a17e06b6d720b0949d012409817518a917d93e4aac
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Extends ActiveRecord by adding `pluck_to_hash` method that returns array of hashes instead of array of arrays. Useful when plucking multiple columns for rendering json or you need to access individual fields in your view for example.
|
4
4
|
|
5
|
+
Supports `pluck_to_struct` since version 0.3.0. `pluck_to_struct` returns an array of `struct`s.
|
6
|
+
|
5
7
|
[![Gem Version](https://badge.fury.io/rb/pluck_to_hash.png)](http://badge.fury.io/rb/pluck_to_hash) [![Build Status](https://travis-ci.org/girishso/pluck_to_hash.svg?branch=master)](https://travis-ci.org/girishso/pluck_to_hash)
|
6
8
|
|
7
9
|
## Installation
|
@@ -55,6 +57,52 @@ User.pluck_to_hash(:id, 'created_at::date as my_date', 'created_at::time as my_t
|
|
55
57
|
#
|
56
58
|
```
|
57
59
|
|
60
|
+
Accepts `block` parameter
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
User.pluck_to_hash(:id, :title) do |user_hash|
|
64
|
+
...
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### Using `pluck_to_struct`
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
posts = Post.limit(2).pluck_to_struct(:id, :title)
|
72
|
+
#
|
73
|
+
# [#<struct id=1, title="foo">, #<struct id=2, title="bar">]
|
74
|
+
#
|
75
|
+
|
76
|
+
posts.first.id
|
77
|
+
# 1
|
78
|
+
|
79
|
+
posts.first.title
|
80
|
+
# "foo"
|
81
|
+
|
82
|
+
```
|
83
|
+
|
84
|
+
or use the shorter alias `pluck_s`
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
posts = Post.limit(2).pluck_s(:id, :title)
|
88
|
+
#
|
89
|
+
# [#<struct id=1, title="foo">, #<struct id=2, title="bar">]
|
90
|
+
#
|
91
|
+
```
|
92
|
+
|
93
|
+
Supports `block` parameter as well
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
Post.limit(2).pluck_to_struct(:id, :title) do |post_struct|
|
97
|
+
puts post_struct.title
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
## Using with Sinatra or other non-rails frameworks without ActiveSupport
|
103
|
+
|
104
|
+
Use version `0.1.4` that removes ActiveSupport dependency. `HashWithIndifferentAccess` is not used in that case.
|
105
|
+
|
58
106
|
## Why not `ActiveRecord.select` or `ActiveRecord.as_json`?
|
59
107
|
|
60
108
|
Here are results of "benchmark" tests performed on MacBook Air. Each method did 10 runs, rejected the 2 highest and 2 lowest times and average the remaining 6. Ran these tests on about 40,000 records. We notice that `pluck_to_hash` is almost 4x faster than `select` and about 8x faster than `as_json`. As with all the "benchmarks", you should take these results with a pinch of salt!
|
@@ -81,4 +129,4 @@ as_json 1.196667 0.010000 1.206667 ( 1.222286)
|
|
81
129
|
## Licence
|
82
130
|
MIT License
|
83
131
|
|
84
|
-
Brought to you by: [Cube Root Software](http://www.cuberoot.in) ©
|
132
|
+
Brought to you by: [Cube Root Software](http://www.cuberoot.in) © 2016
|
data/lib/pluck_to_hash.rb
CHANGED
@@ -7,7 +7,31 @@ module PluckToHash
|
|
7
7
|
def pluck_to_hash(*keys)
|
8
8
|
block_given = block_given?
|
9
9
|
keys = column_names if keys.blank?
|
10
|
-
formatted_keys = keys
|
10
|
+
formatted_keys = format_keys(keys)
|
11
|
+
pluck(*keys).map do |row|
|
12
|
+
row = [row] if keys.size == 1
|
13
|
+
value = HashWithIndifferentAccess[formatted_keys.zip(row)]
|
14
|
+
yield(value) if block_given
|
15
|
+
value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def pluck_to_struct(*keys)
|
20
|
+
block_given = block_given?
|
21
|
+
keys = column_names if keys.blank?
|
22
|
+
formatted_keys = format_keys(keys)
|
23
|
+
|
24
|
+
struct = Struct.new(*formatted_keys)
|
25
|
+
pluck(*keys).map do |row|
|
26
|
+
row = [row] if keys.size == 1
|
27
|
+
value = struct.new(*row)
|
28
|
+
yield(value) if block_given
|
29
|
+
value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def format_keys(keys)
|
34
|
+
keys.map do |k|
|
11
35
|
case k
|
12
36
|
when String
|
13
37
|
k.split(/ as /i)[-1].to_sym
|
@@ -15,15 +39,10 @@ module PluckToHash
|
|
15
39
|
k
|
16
40
|
end
|
17
41
|
end
|
18
|
-
pluck(*keys).map do |row|
|
19
|
-
row = [row] if keys.size == 1
|
20
|
-
value = HashWithIndifferentAccess[formatted_keys.zip(row)]
|
21
|
-
yield(value) if block_given
|
22
|
-
value
|
23
|
-
end
|
24
42
|
end
|
25
43
|
|
26
44
|
alias_method :pluck_h, :pluck_to_hash
|
45
|
+
alias_method :pluck_s, :pluck_to_struct
|
27
46
|
end
|
28
47
|
end
|
29
48
|
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative './spec_helper'
|
2
|
+
|
3
|
+
describe 'PluckToStruct' do
|
4
|
+
before { TestModel.delete_all }
|
5
|
+
|
6
|
+
context '.pluck_to_struct' do
|
7
|
+
before do
|
8
|
+
TestModel.create!(id: 1, test_attribute: "test1")
|
9
|
+
TestModel.create!(id: 2, test_attribute: "test2")
|
10
|
+
TestModel.create!(id: 3, test_attribute: "test3")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'plucks the ids, test_attributes of the objects to a struct correctly' do
|
14
|
+
TestModel.all.pluck_to_struct(:test_attribute, :id).each_with_index do |model, ix|
|
15
|
+
id = ix+1
|
16
|
+
expect(model).to be_a(Struct)
|
17
|
+
expect(model.test_attribute).to eq("test#{id}")
|
18
|
+
expect(model.id).to eq(id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'alias pluck_s' do
|
23
|
+
it 'works correctly' do
|
24
|
+
struct = TestModel.where(id: 1).pluck_s(:test_attribute).first
|
25
|
+
expect(struct.test_attribute).to eq("test1")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'the model does not have the attribute specified' do
|
30
|
+
it 'raises an error' do
|
31
|
+
expect do
|
32
|
+
TestModel.all.pluck_h(:foo)
|
33
|
+
end.to raise_error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'no models exist for the given criteria' do
|
38
|
+
it 'returns an empty relation' do
|
39
|
+
expect do
|
40
|
+
result = TestModel.where(id: -1).pluck_h(:id)
|
41
|
+
expect(result).to be_empty
|
42
|
+
end.to_not raise_error
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
context 'when serialize attributes used' do
|
50
|
+
describe '.pluck_to_struct' do
|
51
|
+
before do
|
52
|
+
TestModel.create!(id: 1, serialized_attribute: [])
|
53
|
+
TestModel.create!(id: 2, serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'])
|
54
|
+
TestModel.create!(id: 3, serialized_attribute: ['Comonad'])
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'plucks to struct correctly' do
|
58
|
+
result = TestModel.pluck_to_struct(:serialized_attribute)
|
59
|
+
|
60
|
+
expect(result[0].serialized_attribute).to eq([])
|
61
|
+
expect(result[1].serialized_attribute).to eq(['Zygohistomorpic', 'Prepromorphism'])
|
62
|
+
expect(result[2].serialized_attribute).to eq(['Comonad'])
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'plucks to struct with multiple attributes' do
|
66
|
+
result = TestModel.pluck_to_struct(:id, :serialized_attribute)
|
67
|
+
|
68
|
+
expect(result[0].serialized_attribute).to eq([])
|
69
|
+
expect(result[0].id).to eq(1)
|
70
|
+
|
71
|
+
expect(result[1].serialized_attribute).to eq(['Zygohistomorpic', 'Prepromorphism'])
|
72
|
+
expect(result[1].id).to eq(2)
|
73
|
+
|
74
|
+
expect(result[2].serialized_attribute).to eq(['Comonad'])
|
75
|
+
expect(result[2].id).to eq(3)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
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.3.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-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/pluck_to_hash/version.rb
|
113
113
|
- pluck_to_hash.gemspec
|
114
114
|
- spec/pluck_to_hash_spec.rb
|
115
|
+
- spec/pluck_to_struct_spec.rb
|
115
116
|
- spec/spec_helper.rb
|
116
117
|
homepage: https://github.com/girishso/pluck_to_hash
|
117
118
|
licenses:
|
@@ -139,4 +140,5 @@ specification_version: 4
|
|
139
140
|
summary: Extend ActiveRecord pluck to return hash
|
140
141
|
test_files:
|
141
142
|
- spec/pluck_to_hash_spec.rb
|
143
|
+
- spec/pluck_to_struct_spec.rb
|
142
144
|
- spec/spec_helper.rb
|