pluck_to_hash 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8a7cad98ab4502a9b3836f0a5d769d334fa99b0
4
- data.tar.gz: 5eb006f187a81d71311c3a019154fb9b21fbf2ad
3
+ metadata.gz: df4cd9672a0e574e5521ce4715ff26824ce4cd10
4
+ data.tar.gz: 4f3f7812b3e1fbb7765c66d50788035643aca3ca
5
5
  SHA512:
6
- metadata.gz: 760b2cb5d06226b6e8def38a999452471b53b9a1fda35e7aec89c6c8d20cbfb3d3410ad43442e4f6da2e6b9d32363f428f40dee807d95384025c5c9e12d70c84
7
- data.tar.gz: 778887f19c45dd8b929b2979ba0566730faf38fa0655d9dcef07cca08f7fde3b22a1ce36024e9a3076157fe0ca95f4731c459896e2d6c786d0f6d6db562abf3b
6
+ metadata.gz: 1b4d6fdafb153d7d83c5897de79a55994bd0e7bbd65ab94ce45699c28706374a9957917bbfdd6ebad8310e2f7440c434ef803cc6469ed6c044ca535de4dbb78e
7
+ data.tar.gz: c6605c8f62008cbd7364d57f7c4d34c77d23049b0176f0962cee967e782159f9f08ec6fc6754e6329dd35efaa9877ebda3f73da7a0e671cf89e0150446cf730d
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.9.3"
4
3
  - "2.2.0"
5
4
  # uncomment this line if your project needs to run something other than `rake`:
6
5
  script: bundle exec rspec spec
@@ -0,0 +1,5 @@
1
+ # 1.0 - 2016-06-10
2
+
3
+ * Hash keys are now strings instead of symbols to match behavior with `HashWithIndifferentAccess` [[#15](https://github.com/girishso/pluck_to_hash/pull/15)] @MrEmelianenko
4
+ * Now accepts a block, which will return an array of whatever the block returns [[#15](https://github.com/girishso/pluck_to_hash/pull/15#issue-157198216)] @MrEmelianenko
5
+ * Hash and Struct return types can be overridden (defaults are `HashWithIndifferentAccess`/`Struct`) [[#16](https://github.com/girishso/pluck_to_hash/pull/16)] @offthecuff
data/Gemfile CHANGED
@@ -6,4 +6,6 @@ gemspec
6
6
  group :test do
7
7
  gem 'rspec'
8
8
  gem 'sqlite3'
9
+ gem 'activesupport', '4.2.6'
10
+ gem 'pg'
9
11
  end
data/README.md CHANGED
@@ -106,9 +106,9 @@ Post.limit(2).pluck_to_struct(:id, :title) do |post_struct|
106
106
  end
107
107
  ```
108
108
 
109
- Allows specifying the type of struct. Defaults to statndard Struct.
109
+ Allows specifying the type of struct. Defaults to standard Struct.
110
110
  ```ruby
111
- Post.limit(2).pluck_to_struct(:id, :title,struct_type: OtherStructType) do |post_struct|
111
+ Post.limit(2).pluck_to_struct(:id, :title, struct_type: OtherStructType) do |post_struct|
112
112
  puts post_struct.title
113
113
  end
114
114
  ```
@@ -5,8 +5,9 @@ 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
9
8
  block_given = block_given?
9
+ hash_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:hash_type,HashWithIndifferentAccess) : HashWithIndifferentAccess
10
+
10
11
  keys, formatted_keys = format_keys(keys)
11
12
  keys_one = keys.size == 1
12
13
 
@@ -29,23 +30,29 @@ module PluckToHash
29
30
  end
30
31
  end
31
32
 
32
- def format_keys(keys)
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
33
+ private
34
+ def get_correct_hash_type(hash, hash_type)
35
+ hash_type == HashWithIndifferentAccess ? hash.with_indifferent_access : hash
36
+ end
37
+
38
+ def format_keys(keys)
39
+ if keys.blank?
40
+ [column_names, column_names]
41
+ else
42
+ ks = keys.map{|k| k.instance_of?(String) ? k.split(",") : k}.flatten
43
+ [
44
+ ks,
45
+ ks.map do |k|
46
+ case k
47
+ when String
48
+ k.split(/\bas\b/i)[-1].strip.to_sym
49
+ when Symbol
50
+ k
51
+ end
44
52
  end
45
- end
46
- ]
53
+ ]
54
+ end
47
55
  end
48
- end
49
56
 
50
57
  alias_method :pluck_h, :pluck_to_hash
51
58
  alias_method :pluck_s, :pluck_to_struct
@@ -1,3 +1,3 @@
1
1
  module PluckToHash
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "sqlite3", "~> 1.3"
24
+ spec.add_development_dependency "pg", "~> 0.19.0"
24
25
  spec.add_development_dependency "rspec", "~> 3.2"
25
26
  spec.add_development_dependency "values", "~> 1.8"
26
27
 
@@ -0,0 +1,25 @@
1
+ require 'active_record'
2
+
3
+ def run_migrations
4
+ # Turns off messaging during spec running of table creation
5
+ ActiveRecord::Migration.verbose = false
6
+ ActiveRecord::Schema.define do
7
+ create_table :test_models do |t|
8
+ t.string :test_attribute
9
+ t.string :serialized_attribute
10
+ end
11
+ create_table :test_model_children do |t|
12
+ t.integer :test_model_id
13
+ t.string :children_name
14
+ end
15
+ end
16
+ end
17
+
18
+ class TestModel < ActiveRecord::Base
19
+ serialize :serialized_attribute, Array
20
+ has_many :test_model_children
21
+ end
22
+
23
+ class TestModelChild < ActiveRecord::Base
24
+ belongs_to :test_model
25
+ end
@@ -2,73 +2,11 @@ require_relative './spec_helper'
2
2
 
3
3
  describe 'PluckToHash' do
4
4
  before { TestModel.delete_all }
5
+ include_context 'making sure alias is fine'
5
6
 
6
7
  describe '.pluck_to_hash' do
7
- before do
8
- 3.times.each do
9
- TestModel.create!
10
- end
11
- end
12
-
13
- it 'plucks the ids of the objects to a hash correctly' do
14
- TestModel.all.pluck_to_hash(:id).each do |hash|
15
- expect(hash.class).to eq(HashWithIndifferentAccess)
16
- expect(hash).to have_key(:id)
17
- end
18
- end
19
-
20
- it 'pluck field with lowercase alias' do
21
- TestModel.all.pluck_to_hash('id as Something').each do |hash|
22
- expect(hash).to have_key(:Something)
23
- end
24
- end
25
-
26
- it 'pluck field with uppercase alias' do
27
- TestModel.all.pluck_to_hash('id AS otherfield').each do |hash|
28
- expect(hash).to have_key(:otherfield)
29
- end
30
- end
31
-
32
- it 'pluck field with mixedcase alias' do
33
- TestModel.all.pluck_to_hash('id As anotherfield').each do |hash|
34
- expect(hash).to have_key(:anotherfield)
35
- end
36
- end
37
-
38
- context 'the model does not have the attribute specified' do
39
- it 'raises an error' do
40
- expect do
41
- TestModel.all.pluck_to_hash(:foo)
42
- end.to raise_error(ActiveRecord::StatementInvalid)
43
- end
44
- end
45
-
46
- context 'no models exist for the given criteria' do
47
- it 'returns an empty relation' do
48
- result = TestModel.where(id: -1).pluck_to_hash(:id)
49
- expect(result).to be_empty
50
- end
51
- end
52
-
53
- context 'specifying multiple attributes' do
54
- it 'returns a hash with both attributes' do
55
- TestModel.all.pluck_to_hash(:id, :test_attribute).each do |hash|
56
- expect(hash.class).to eq(HashWithIndifferentAccess)
57
- expect(hash).to have_key(:id)
58
- expect(hash).to have_key(:test_attribute)
59
- end
60
- end
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
8
+ include_context 'essentials'
9
+ include_context 'when using a different hash type'
72
10
  end
73
11
 
74
12
  context 'when serialize attributes used' do
@@ -78,7 +16,7 @@ describe 'PluckToHash' do
78
16
  TestModel.create!(serialized_attribute: ['Zygohistomorpic', 'Prepromorphism'])
79
17
  TestModel.create!(serialized_attribute: ['Comonad'])
80
18
  end
81
-
19
+
82
20
  it 'plucks the hash correctly' do
83
21
  result = TestModel.pluck_to_hash(:serialized_attribute)
84
22
  expect(result).to eq [
@@ -98,46 +36,4 @@ describe 'PluckToHash' do
98
36
  end
99
37
  end
100
38
  end
101
-
102
- context 'making sure alias is fine' do
103
- describe '.pluck_h' do
104
- before do
105
- 3.times.each do
106
- TestModel.create!
107
- end
108
- end
109
-
110
- it 'plucks the ids of the objects to a hash correctly' do
111
- TestModel.all.pluck_h(:id).each do |hash|
112
- expect(hash.class).to eq(HashWithIndifferentAccess)
113
- expect(hash).to have_key(:id)
114
- end
115
- end
116
-
117
- context 'the model does not have the attribute specified' do
118
- it 'raises an error' do
119
- expect do
120
- TestModel.all.pluck_h(:foo)
121
- end.to raise_error(ActiveRecord::StatementInvalid)
122
- end
123
- end
124
-
125
- context 'no models exist for the given criteria' do
126
- it 'returns an empty relation' do
127
- result = TestModel.where(id: -1).pluck_h(:id)
128
- expect(result).to be_empty
129
- end
130
- end
131
-
132
- context 'specifying multiple attributes' do
133
- it 'returns a hash with both attributes' do
134
- TestModel.all.pluck_h(:id, :test_attribute).each do |hash|
135
- expect(hash.class).to eq(HashWithIndifferentAccess)
136
- expect(hash).to have_key(:id)
137
- expect(hash).to have_key(:test_attribute)
138
- end
139
- end
140
- end
141
- end
142
- end
143
39
  end
@@ -0,0 +1,32 @@
1
+ require 'active_record'
2
+ require_relative '../lib/pluck_to_hash'
3
+ require_relative './migrations'
4
+ require_relative './support/shared_examples'
5
+
6
+ describe 'PluckToHash with Postgres' do
7
+
8
+ before(:all) do
9
+ ActiveRecord::Base.remove_connection
10
+ @db_name = "pluck_to_hash_test"
11
+ %x( createdb -E UTF8 -T template0 #{@db_name} )
12
+ ActiveRecord::Base.establish_connection(
13
+ "adapter" => "postgresql",
14
+ "database" => @db_name
15
+ )
16
+ run_migrations()
17
+ end
18
+
19
+ after(:all) do
20
+ ActiveRecord::Base.remove_connection
21
+ %x( dropdb #{@db_name})
22
+ end
23
+
24
+ before { TestModel.delete_all }
25
+ include_context 'making sure alias is fine'
26
+
27
+ describe '.pluck_to_hash' do
28
+ include_context 'essentials'
29
+ include_context 'when using a different hash type'
30
+ end
31
+
32
+ end
@@ -1,20 +1,11 @@
1
1
  require 'active_record'
2
2
  require_relative '../lib/pluck_to_hash'
3
+ require_relative './migrations'
4
+ require_relative './support/shared_examples'
3
5
 
4
6
  ActiveRecord::Base.establish_connection(
5
7
  "adapter" => "sqlite3",
6
8
  "database" => ":memory:"
7
9
  )
8
10
 
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
- t.string :serialized_attribute
15
- end
16
- end
17
-
18
- class TestModel < ActiveRecord::Base
19
- serialize :serialized_attribute, Array
20
- end
11
+ run_migrations()
@@ -0,0 +1,126 @@
1
+
2
+ shared_context 'essentials' do
3
+ before do
4
+ 3.times.each do
5
+ test_model = TestModel.create!
6
+ 2.times do
7
+ TestModelChild.create!(test_model_id: test_model.id)
8
+ end
9
+ end
10
+ end
11
+
12
+ it 'plucks the ids of the objects to a hash correctly' do
13
+ TestModel.all.pluck_to_hash(:id).each do |hash|
14
+ expect(hash.class).to eq(HashWithIndifferentAccess)
15
+ expect(hash).to have_key(:id)
16
+ end
17
+ end
18
+
19
+ it 'pluck field with lowercase alias' do
20
+ TestModel.all.pluck_to_hash('id as something').each do |hash|
21
+ expect(hash).to have_key(:something)
22
+ end
23
+ end
24
+
25
+ it 'pluck field with uppercase alias' do
26
+ TestModel.all.pluck_to_hash('id AS otherfield').each do |hash|
27
+ expect(hash).to have_key(:otherfield)
28
+ end
29
+ end
30
+
31
+ it 'pluck field with mixedcase alias' do
32
+ TestModel.all.pluck_to_hash('id As anotherfield').each do |hash|
33
+ expect(hash).to have_key(:anotherfield)
34
+ end
35
+ end
36
+
37
+ it 'works with block parameter' do
38
+ TestModel.pluck_to_hash(:id) do |hash|
39
+ expect(hash).to have_key(:id)
40
+ end
41
+ end
42
+
43
+ it 'works with join' do
44
+ TestModel.joins(:test_model_children).pluck_to_hash('test_models.id, test_model_children.id').each do |hash|
45
+ expect(hash).to have_key('test_models.id')
46
+ expect(hash).to have_key('test_model_children.id')
47
+ end
48
+ end
49
+
50
+ context 'the model does not have the attribute specified' do
51
+ it 'raises an error' do
52
+ expect do
53
+ TestModel.all.pluck_to_hash(:foo)
54
+ end.to raise_error(ActiveRecord::StatementInvalid)
55
+ end
56
+ end
57
+
58
+ context 'no models exist for the given criteria' do
59
+ it 'returns an empty relation' do
60
+ result = TestModel.where(id: -1).pluck_to_hash(:id)
61
+ expect(result).to be_empty
62
+ end
63
+ end
64
+
65
+ context 'specifying multiple attributes' do
66
+ it 'returns a hash with both attributes' do
67
+ TestModel.all.pluck_to_hash(:id, :test_attribute).each do |hash|
68
+ expect(hash.class).to eq(HashWithIndifferentAccess)
69
+ expect(hash).to have_key('id')
70
+ expect(hash).to have_key(:test_attribute)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ shared_context 'making sure alias is fine' do
77
+ describe '.pluck_h' do
78
+ before do
79
+ 3.times.each do
80
+ TestModel.create!
81
+ end
82
+ end
83
+
84
+ it 'plucks the ids of the objects to a hash correctly' do
85
+ TestModel.all.pluck_h(:id).each do |hash|
86
+ expect(hash.class).to eq(HashWithIndifferentAccess)
87
+ expect(hash).to have_key(:id)
88
+ end
89
+ end
90
+
91
+ context 'the model does not have the attribute specified' do
92
+ it 'raises an error' do
93
+ expect do
94
+ TestModel.all.pluck_h(:foo)
95
+ end.to raise_error(ActiveRecord::StatementInvalid)
96
+ end
97
+ end
98
+
99
+ context 'no models exist for the given criteria' do
100
+ it 'returns an empty relation' do
101
+ result = TestModel.where(id: -1).pluck_h(:id)
102
+ expect(result).to be_empty
103
+ end
104
+ end
105
+
106
+ context 'specifying multiple attributes' do
107
+ it 'returns a hash with both attributes' do
108
+ TestModel.all.pluck_h(:id, :test_attribute).each do |hash|
109
+ expect(hash.class).to eq(HashWithIndifferentAccess)
110
+ expect(hash).to have_key(:id)
111
+ expect(hash).to have_key(:test_attribute)
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ shared_context 'when using a different hash type' do
119
+ it 'returns a hash of the correct type with all attributes' do
120
+ TestModel.all.pluck_to_hash(:id, :test_attribute, hash_type: Hash).each do |hash|
121
+ expect(hash.class).to eq(Hash)
122
+ expect(hash.with_indifferent_access).to have_key(:id)
123
+ expect(hash.with_indifferent_access).to have_key(:test_attribute)
124
+ end
125
+ end
126
+ 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: 1.0.0
4
+ version: 1.0.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: 2016-06-10 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.19.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.19.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -118,6 +132,7 @@ extra_rdoc_files: []
118
132
  files:
119
133
  - ".gitignore"
120
134
  - ".travis.yml"
135
+ - CHANGELOG.md
121
136
  - Gemfile
122
137
  - LICENSE.txt
123
138
  - README.md
@@ -125,9 +140,12 @@ files:
125
140
  - lib/pluck_to_hash.rb
126
141
  - lib/pluck_to_hash/version.rb
127
142
  - pluck_to_hash.gemspec
143
+ - spec/migrations.rb
128
144
  - spec/pluck_to_hash_spec.rb
129
145
  - spec/pluck_to_struct_spec.rb
146
+ - spec/postgresql_spec.rb
130
147
  - spec/spec_helper.rb
148
+ - spec/support/shared_examples.rb
131
149
  homepage: https://github.com/girishso/pluck_to_hash
132
150
  licenses:
133
151
  - MIT
@@ -148,11 +166,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
166
  version: '0'
149
167
  requirements: []
150
168
  rubyforge_project:
151
- rubygems_version: 2.4.5
169
+ rubygems_version: 2.6.12
152
170
  signing_key:
153
171
  specification_version: 4
154
172
  summary: Extend ActiveRecord pluck to return hash
155
173
  test_files:
174
+ - spec/migrations.rb
156
175
  - spec/pluck_to_hash_spec.rb
157
176
  - spec/pluck_to_struct_spec.rb
177
+ - spec/postgresql_spec.rb
158
178
  - spec/spec_helper.rb
179
+ - spec/support/shared_examples.rb