pluck_to_hash 0.3.0 → 1.0.0
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/README.md +14 -0
- data/lib/pluck_to_hash.rb +26 -20
- data/lib/pluck_to_hash/version.rb +1 -1
- data/pluck_to_hash.gemspec +1 -0
- data/spec/pluck_to_hash_spec.rb +16 -11
- data/spec/pluck_to_struct_spec.rb +15 -8
- data/spec/spec_helper.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8a7cad98ab4502a9b3836f0a5d769d334fa99b0
|
4
|
+
data.tar.gz: 5eb006f187a81d71311c3a019154fb9b21fbf2ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 760b2cb5d06226b6e8def38a999452471b53b9a1fda35e7aec89c6c8d20cbfb3d3410ad43442e4f6da2e6b9d32363f428f40dee807d95384025c5c9e12d70c84
|
7
|
+
data.tar.gz: 778887f19c45dd8b929b2979ba0566730faf38fa0655d9dcef07cca08f7fde3b22a1ce36024e9a3076157fe0ca95f4731c459896e2d6c786d0f6d6db562abf3b
|
data/README.md
CHANGED
@@ -65,6 +65,14 @@ User.pluck_to_hash(:id, :title) do |user_hash|
|
|
65
65
|
end
|
66
66
|
```
|
67
67
|
|
68
|
+
Allows specifying the type of hash. Defaults to HashWithIndifferentAccess
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
User.pluck_to_hash(:id, :title, hash_type: CustomHash) do |custom_hash|
|
72
|
+
...
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
68
76
|
### Using `pluck_to_struct`
|
69
77
|
|
70
78
|
```ruby
|
@@ -98,6 +106,12 @@ Post.limit(2).pluck_to_struct(:id, :title) do |post_struct|
|
|
98
106
|
end
|
99
107
|
```
|
100
108
|
|
109
|
+
Allows specifying the type of struct. Defaults to statndard Struct.
|
110
|
+
```ruby
|
111
|
+
Post.limit(2).pluck_to_struct(:id, :title,struct_type: OtherStructType) do |post_struct|
|
112
|
+
puts post_struct.title
|
113
|
+
end
|
114
|
+
```
|
101
115
|
|
102
116
|
## Using with Sinatra or other non-rails frameworks without ActiveSupport
|
103
117
|
|
data/lib/pluck_to_hash.rb
CHANGED
@@ -5,39 +5,45 @@ module PluckToHash
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def pluck_to_hash(*keys)
|
8
|
+
hash_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:hash_type,HashWithIndifferentAccess) : HashWithIndifferentAccess
|
8
9
|
block_given = block_given?
|
9
|
-
keys =
|
10
|
-
|
10
|
+
keys, formatted_keys = format_keys(keys)
|
11
|
+
keys_one = keys.size == 1
|
12
|
+
|
11
13
|
pluck(*keys).map do |row|
|
12
|
-
|
13
|
-
|
14
|
-
yield(value) if block_given
|
15
|
-
value
|
14
|
+
value = hash_type[formatted_keys.zip(keys_one ? [row] : row)]
|
15
|
+
block_given ? yield(value) : value
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
def pluck_to_struct(*keys)
|
20
|
+
struct_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:struct_type,Struct) : Struct
|
20
21
|
block_given = block_given?
|
21
|
-
keys =
|
22
|
-
|
22
|
+
keys, formatted_keys = format_keys(keys)
|
23
|
+
keys_one = keys.size == 1
|
23
24
|
|
24
|
-
struct =
|
25
|
+
struct = struct_type.new(*formatted_keys)
|
25
26
|
pluck(*keys).map do |row|
|
26
|
-
|
27
|
-
|
28
|
-
yield(value) if block_given
|
29
|
-
value
|
27
|
+
value = keys_one ? struct.new(*[row]) : struct.new(*row)
|
28
|
+
block_given ? yield(value) : value
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
32
|
def format_keys(keys)
|
34
|
-
keys.
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
k
|
40
|
-
|
33
|
+
if keys.blank?
|
34
|
+
[column_names, column_names]
|
35
|
+
else
|
36
|
+
[
|
37
|
+
keys,
|
38
|
+
keys.map do |k|
|
39
|
+
case k
|
40
|
+
when String
|
41
|
+
k.split(/\bas\b/i)[-1].strip.to_sym
|
42
|
+
when Symbol
|
43
|
+
k
|
44
|
+
end
|
45
|
+
end
|
46
|
+
]
|
41
47
|
end
|
42
48
|
end
|
43
49
|
|
data/pluck_to_hash.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency "sqlite3", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.2"
|
25
|
+
spec.add_development_dependency "values", "~> 1.8"
|
25
26
|
|
26
27
|
spec.add_dependency "activerecord", ">= 4.0.2"
|
27
28
|
spec.add_dependency "activesupport", ">= 4.0.2"
|
data/spec/pluck_to_hash_spec.rb
CHANGED
@@ -35,21 +35,18 @@ describe 'PluckToHash' do
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
38
|
context 'the model does not have the attribute specified' do
|
40
39
|
it 'raises an error' do
|
41
40
|
expect do
|
42
41
|
TestModel.all.pluck_to_hash(:foo)
|
43
|
-
end.to raise_error
|
42
|
+
end.to raise_error(ActiveRecord::StatementInvalid)
|
44
43
|
end
|
45
44
|
end
|
46
45
|
|
47
46
|
context 'no models exist for the given criteria' do
|
48
47
|
it 'returns an empty relation' do
|
49
|
-
|
50
|
-
|
51
|
-
expect(result).to be_empty
|
52
|
-
end.to_not raise_error
|
48
|
+
result = TestModel.where(id: -1).pluck_to_hash(:id)
|
49
|
+
expect(result).to be_empty
|
53
50
|
end
|
54
51
|
end
|
55
52
|
|
@@ -62,6 +59,16 @@ describe 'PluckToHash' do
|
|
62
59
|
end
|
63
60
|
end
|
64
61
|
end
|
62
|
+
|
63
|
+
context 'when using a different hash type' do
|
64
|
+
it 'returns a hash of the correct type with all attributes' do
|
65
|
+
TestModel.all.pluck_to_hash(:id, :test_attribute,hash_type: Hash).each do |hash|
|
66
|
+
expect(hash.class).to eq(Hash)
|
67
|
+
expect(hash).to have_key(:id)
|
68
|
+
expect(hash).to have_key(:test_attribute)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
65
72
|
end
|
66
73
|
|
67
74
|
context 'when serialize attributes used' do
|
@@ -111,16 +118,14 @@ describe 'PluckToHash' do
|
|
111
118
|
it 'raises an error' do
|
112
119
|
expect do
|
113
120
|
TestModel.all.pluck_h(:foo)
|
114
|
-
end.to raise_error
|
121
|
+
end.to raise_error(ActiveRecord::StatementInvalid)
|
115
122
|
end
|
116
123
|
end
|
117
124
|
|
118
125
|
context 'no models exist for the given criteria' do
|
119
126
|
it 'returns an empty relation' do
|
120
|
-
|
121
|
-
|
122
|
-
expect(result).to be_empty
|
123
|
-
end.to_not raise_error
|
127
|
+
result = TestModel.where(id: -1).pluck_h(:id)
|
128
|
+
expect(result).to be_empty
|
124
129
|
end
|
125
130
|
end
|
126
131
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require_relative './spec_helper'
|
2
|
+
require 'values'
|
2
3
|
|
3
4
|
describe 'PluckToStruct' do
|
4
5
|
before { TestModel.delete_all }
|
@@ -12,7 +13,7 @@ describe 'PluckToStruct' do
|
|
12
13
|
|
13
14
|
it 'plucks the ids, test_attributes of the objects to a struct correctly' do
|
14
15
|
TestModel.all.pluck_to_struct(:test_attribute, :id).each_with_index do |model, ix|
|
15
|
-
id = ix+1
|
16
|
+
id = ix + 1
|
16
17
|
expect(model).to be_a(Struct)
|
17
18
|
expect(model.test_attribute).to eq("test#{id}")
|
18
19
|
expect(model.id).to eq(id)
|
@@ -29,17 +30,23 @@ describe 'PluckToStruct' do
|
|
29
30
|
context 'the model does not have the attribute specified' do
|
30
31
|
it 'raises an error' do
|
31
32
|
expect do
|
32
|
-
TestModel.all.
|
33
|
-
end.to raise_error
|
33
|
+
TestModel.all.pluck_s(:foo)
|
34
|
+
end.to raise_error(ActiveRecord::StatementInvalid)
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
38
|
context 'no models exist for the given criteria' do
|
38
39
|
it 'returns an empty relation' do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
result = TestModel.where(id: -1).pluck_s(:id)
|
41
|
+
expect(result).to be_empty
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when a different struct type is specified' do
|
46
|
+
it 'returns an object with all attributes' do
|
47
|
+
TestModel.all.pluck_to_struct(:test_attribute, :id, struct_type: Value).each do |model|
|
48
|
+
expect(model).to respond_to(:id, :test_attribute)
|
49
|
+
end
|
43
50
|
end
|
44
51
|
end
|
45
52
|
|
@@ -77,4 +84,4 @@ describe 'PluckToStruct' do
|
|
77
84
|
end
|
78
85
|
end
|
79
86
|
|
80
|
-
end
|
87
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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: 1.0.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-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: values
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.8'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: activerecord
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|