pluck_to_hash 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/lib/pluck_to_hash.rb +9 -1
- data/lib/pluck_to_hash/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb91aa6d4bed6c68b89a0f93d8302a583c8457f3
|
4
|
+
data.tar.gz: 31e52f1a8b768a4f59707b3fef65ca839cc111d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67aa53827c97b17ef1a0d76c2215c39961bfc8ad5045e734118de4de116d4e4feed010b53ae8aa88456c4f1d995baa4959f605a486eb458b610e9a74beea6601
|
7
|
+
data.tar.gz: e100396c843f6fb33634b8659476bc2eee6300b394b2c6fe0c9e323e3af2a2e2df98b56a5a8721168d17ab4472cefa7ff9780a5fb60f8b4a5d119fb0532f1ad6
|
data/README.md
CHANGED
@@ -45,6 +45,16 @@ Post.limit(2).pluck_h(:id)
|
|
45
45
|
#
|
46
46
|
```
|
47
47
|
|
48
|
+
Supports `select` alias
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
User.pluck_to_hash(:id, 'created_at::date as my_date', 'created_at::time as my_time')
|
52
|
+
#
|
53
|
+
# [{:id=>23, :my_date=>Fri, 11 Jul 2014, :my_time=>2000-01-01 07:54:36 UTC},
|
54
|
+
# {:id=>2, :my_date=>Tue, 01 Jul 2014, :my_time=>2000-01-01 14:36:15 UTC}]
|
55
|
+
#
|
56
|
+
```
|
57
|
+
|
48
58
|
## Why not `ActiveRecord.select` or `ActiveRecord.as_json`?
|
49
59
|
|
50
60
|
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!
|
@@ -60,8 +70,6 @@ select 0.566667 0.010000 0.576667 ( 0.590911)
|
|
60
70
|
as_json 1.196667 0.010000 1.206667 ( 1.222286)
|
61
71
|
```
|
62
72
|
|
63
|
-
You might be interested in reading the [reason for this](http://gavinmiller.io/2013/active-records-dirty-little-secret/).
|
64
|
-
|
65
73
|
## Contributing
|
66
74
|
|
67
75
|
1. Fork it ( https://github.com/girishso/pluck_to_hash/fork )
|
data/lib/pluck_to_hash.rb
CHANGED
@@ -5,10 +5,18 @@ module PluckToHash
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def pluck_to_hash(*keys)
|
8
|
+
formatted_keys = keys.map do |k|
|
9
|
+
case k
|
10
|
+
when String
|
11
|
+
k.split(' as ')[-1].to_sym
|
12
|
+
when Symbol
|
13
|
+
k
|
14
|
+
end
|
15
|
+
end
|
8
16
|
|
9
17
|
pluck(*keys).map do |row|
|
10
18
|
row = [row] if keys.size == 1
|
11
|
-
Hash[
|
19
|
+
Hash[formatted_keys.zip(row)]
|
12
20
|
end
|
13
21
|
end
|
14
22
|
|
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
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Girish S
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|