pluck_to_hash 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bfdbe8405632df3de1d9a02798734f4d240385b1
4
- data.tar.gz: 62f0dec2dcbde43840140536dc347d6e8453b4f0
3
+ metadata.gz: d65f5c05dd598640b480fff78b2bbcbd50715877
4
+ data.tar.gz: 18d5c56bf46b8d7b93c7ad86c5af48b196ba2fae
5
5
  SHA512:
6
- metadata.gz: ff28d40d114f7e0b17588bca904f22b812a4464fbf62b14827945eb8b9282fa7d05903f80dee43b8e11212f8b29f134aff24598c63aabbb6f90acf39a37b24ec
7
- data.tar.gz: f41526670657c194b1d5937535d5d8c9aa6fecff0754f9b9eb80fb83482c20df6bf77bc49af25b0fddfa3514219cbb6e4fa81e960dfb32d98cb17b83420117bb
6
+ metadata.gz: a20b889c5bfcf85c7ff8be47bc0620c647481ae27a95b5eb81933a87a6d412f884a10c8378b7248c3645436b2961f0f74b79276363dbc6e7475c8bdab74e2514
7
+ data.tar.gz: 360ac11e42810918c61308223250ca01194f46be5ed721cf7762252f963e94828b69a8599ac17ca78680002e402391026c24401624e4a5081fa2c5a67960ed08
data/README.md CHANGED
@@ -45,6 +45,23 @@ Post.limit(2).pluck_h(:id)
45
45
  #
46
46
  ```
47
47
 
48
+ ## Why not `ActiveRecord.select` or `ActiveRecord.as_json`?
49
+
50
+ 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!
51
+
52
+ ```ruby
53
+ # Node.pluck_h(:id, :title)
54
+ # Node.select(:id, :title).to_a
55
+ # Node.select(:id, :title).as_json
56
+
57
+ user system total real
58
+ pluck_to_hash 0.145000 0.005000 0.150000 ( 0.164836)
59
+ select 0.566667 0.010000 0.576667 ( 0.590911)
60
+ as_json 1.196667 0.010000 1.206667 ( 1.222286)
61
+ ```
62
+
63
+ You might be interested in reading the [reason for this](http://gavinmiller.io/2013/active-records-dirty-little-secret/).
64
+
48
65
  ## Contributing
49
66
 
50
67
  1. Fork it ( https://github.com/girishso/pluck_to_hash/fork )
@@ -5,7 +5,11 @@ module PluckToHash
5
5
 
6
6
  module ClassMethods
7
7
  def pluck_to_hash(*keys)
8
- pluck(*keys).map{|row| Hash[keys.zip(Array(row))]}
8
+
9
+ pluck(*keys).map do |row|
10
+ row = [row] if keys.size == 1
11
+ Hash[keys.zip(row)]
12
+ end
9
13
  end
10
14
 
11
15
  alias_method :pluck_h, :pluck_to_hash
@@ -1,3 +1,3 @@
1
1
  module PluckToHash
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require_relative './spec_helper'
2
2
 
3
3
  describe 'PluckToHash' do
4
+ before { TestModel.delete_all }
5
+
4
6
  describe '.pluck_to_hash' do
5
7
  before do
6
8
  3.times.each do
@@ -43,6 +45,34 @@ describe 'PluckToHash' do
43
45
  end
44
46
  end
45
47
 
48
+ context 'when serialize attributes used' do
49
+ describe '.pluck_to_hash' do
50
+ before do
51
+ TestModel.create!(serialized_attribute: [])
52
+ TestModel.create!(serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'])
53
+ TestModel.create!(serialized_attribute: ['Comonad'])
54
+ end
55
+
56
+ it 'plucks the hash correctly' do
57
+ result = TestModel.pluck_to_hash(:serialized_attribute)
58
+ expect(result).to eq [
59
+ { serialized_attribute: [] },
60
+ { serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'] },
61
+ { serialized_attribute: ['Comonad'] }
62
+ ]
63
+ end
64
+
65
+ it 'plucks a hash with multiple attributes' do
66
+ result = TestModel.pluck_to_hash(:test_attribute, :serialized_attribute)
67
+ 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'] }
71
+ ]
72
+ end
73
+ end
74
+ end
75
+
46
76
  context 'making sure alias is fine' do
47
77
  describe '.pluck_h' do
48
78
  before do
@@ -11,8 +11,10 @@ ActiveRecord::Migration.verbose = false
11
11
  ActiveRecord::Schema.define do
12
12
  create_table :test_models do |t|
13
13
  t.string :test_attribute
14
+ t.string :serialized_attribute
14
15
  end
15
16
  end
16
17
 
17
18
  class TestModel < ActiveRecord::Base
18
- end
19
+ serialize :serialized_attribute, Array
20
+ 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.1
4
+ version: 0.1.2
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-05-09 00:00:00.000000000 Z
11
+ date: 2015-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler