pluck_to_hash 0.1.0 → 0.1.1
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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/README.md +1 -1
- data/lib/pluck_to_hash.rb +2 -2
- data/lib/pluck_to_hash/version.rb +1 -1
- data/pluck_to_hash.gemspec +2 -0
- data/spec/pluck_to_hash_spec.rb +89 -0
- data/spec/spec_helper.rb +18 -0
- metadata +36 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfdbe8405632df3de1d9a02798734f4d240385b1
|
4
|
+
data.tar.gz: 62f0dec2dcbde43840140536dc347d6e8453b4f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff28d40d114f7e0b17588bca904f22b812a4464fbf62b14827945eb8b9282fa7d05903f80dee43b8e11212f8b29f134aff24598c63aabbb6f90acf39a37b24ec
|
7
|
+
data.tar.gz: f41526670657c194b1d5937535d5d8c9aa6fecff0754f9b9eb80fb83482c20df6bf77bc49af25b0fddfa3514219cbb6e4fa81e960dfb32d98cb17b83420117bb
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
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
|
-
[](http://badge.fury.io/rb/pluck_to_hash)
|
5
|
+
[](http://badge.fury.io/rb/pluck_to_hash) [](https://travis-ci.org/girishso/pluck_to_hash)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
data/lib/pluck_to_hash.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
|
1
|
+
require_relative "./pluck_to_hash/version"
|
2
2
|
|
3
3
|
module PluckToHash
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def pluck_to_hash(*keys)
|
8
|
-
pluck(*keys).map{|row| Hash[
|
8
|
+
pluck(*keys).map{|row| Hash[keys.zip(Array(row))]}
|
9
9
|
end
|
10
10
|
|
11
11
|
alias_method :pluck_h, :pluck_to_hash
|
data/pluck_to_hash.gemspec
CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "sqlite3", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
23
25
|
|
24
26
|
spec.add_dependency "activerecord", ">= 4.0.2"
|
25
27
|
spec.add_dependency "activesupport", ">= 4.0.2"
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require_relative './spec_helper'
|
2
|
+
|
3
|
+
describe 'PluckToHash' do
|
4
|
+
describe '.pluck_to_hash' do
|
5
|
+
before do
|
6
|
+
3.times.each do
|
7
|
+
TestModel.create!
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'plucks the ids of the objects to a hash correctly' do
|
12
|
+
TestModel.all.pluck_to_hash(:id).each do |hash|
|
13
|
+
expect(hash.class).to eq(Hash)
|
14
|
+
expect(hash).to have_key(:id)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'the model does not have the attribute specified' do
|
19
|
+
it 'raises an error' do
|
20
|
+
expect do
|
21
|
+
TestModel.all.pluck_to_hash(:foo)
|
22
|
+
end.to raise_error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'no models exist for the given criteria' do
|
27
|
+
it 'returns an empty relation' do
|
28
|
+
expect do
|
29
|
+
result = TestModel.where(id: -1).pluck_to_hash(:id)
|
30
|
+
expect(result).to be_empty
|
31
|
+
end.to_not raise_error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'specifying multiple attributes' do
|
36
|
+
it 'returns a hash with both attributes' do
|
37
|
+
TestModel.all.pluck_to_hash(:id, :test_attribute).each do |hash|
|
38
|
+
expect(hash.class).to eq(Hash)
|
39
|
+
expect(hash).to have_key(:id)
|
40
|
+
expect(hash).to have_key(:test_attribute)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'making sure alias is fine' do
|
47
|
+
describe '.pluck_h' do
|
48
|
+
before do
|
49
|
+
3.times.each do
|
50
|
+
TestModel.create!
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'plucks the ids of the objects to a hash correctly' do
|
55
|
+
TestModel.all.pluck_h(:id).each do |hash|
|
56
|
+
expect(hash.class).to eq(Hash)
|
57
|
+
expect(hash).to have_key(:id)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'the model does not have the attribute specified' do
|
62
|
+
it 'raises an error' do
|
63
|
+
expect do
|
64
|
+
TestModel.all.pluck_h(:foo)
|
65
|
+
end.to raise_error
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'no models exist for the given criteria' do
|
70
|
+
it 'returns an empty relation' do
|
71
|
+
expect do
|
72
|
+
result = TestModel.where(id: -1).pluck_h(:id)
|
73
|
+
expect(result).to be_empty
|
74
|
+
end.to_not raise_error
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'specifying multiple attributes' do
|
79
|
+
it 'returns a hash with both attributes' do
|
80
|
+
TestModel.all.pluck_h(:id, :test_attribute).each do |hash|
|
81
|
+
expect(hash.class).to eq(Hash)
|
82
|
+
expect(hash).to have_key(:id)
|
83
|
+
expect(hash).to have_key(:test_attribute)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require_relative '../lib/pluck_to_hash'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection(
|
5
|
+
"adapter" => "sqlite3",
|
6
|
+
"database" => ":memory:"
|
7
|
+
)
|
8
|
+
|
9
|
+
# Turns off messaging during spec running of table creation
|
10
|
+
ActiveRecord::Migration.verbose = false
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
create_table :test_models do |t|
|
13
|
+
t.string :test_attribute
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class TestModel < ActiveRecord::Base
|
18
|
+
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
|
+
version: 0.1.1
|
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-
|
11
|
+
date: 2015-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: activerecord
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,6 +103,7 @@ extensions: []
|
|
75
103
|
extra_rdoc_files: []
|
76
104
|
files:
|
77
105
|
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
78
107
|
- Gemfile
|
79
108
|
- LICENSE.txt
|
80
109
|
- README.md
|
@@ -82,6 +111,8 @@ files:
|
|
82
111
|
- lib/pluck_to_hash.rb
|
83
112
|
- lib/pluck_to_hash/version.rb
|
84
113
|
- pluck_to_hash.gemspec
|
114
|
+
- spec/pluck_to_hash_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
85
116
|
homepage: https://github.com/girishso/pluck_to_hash
|
86
117
|
licenses:
|
87
118
|
- MIT
|
@@ -106,4 +137,6 @@ rubygems_version: 2.4.5
|
|
106
137
|
signing_key:
|
107
138
|
specification_version: 4
|
108
139
|
summary: Extend ActiveRecord pluck to return hash
|
109
|
-
test_files:
|
140
|
+
test_files:
|
141
|
+
- spec/pluck_to_hash_spec.rb
|
142
|
+
- spec/spec_helper.rb
|