relation_to_struct 1.2.0 → 1.2.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: 20d99ebc500ab5e07fd4b78fcb158790fa26b475
4
- data.tar.gz: 5af91e2d780a6e0ec03383c3c29599674707f0a5
3
+ metadata.gz: 3f6639fc279267133528f887315dc029f1089880
4
+ data.tar.gz: 6865cc290bf029d43c9b9dc72c75a262164bdbc8
5
5
  SHA512:
6
- metadata.gz: 732739b20458d9223cb161bdc4c3c9928b28a2b1c70536aca9371ddf642264a83b6f78ea1dd8eb78862d22069672a78b4cc12930d9cfbb97f448aa506ecdc759
7
- data.tar.gz: 258ea4297e9b7f2538d54a727b9b4eeee01df5f8dfae0041c5ee8ebf2ecc0ba1753866aa0d127e9699aedfc2af95005a74d76b35e97995899e3450b9d444b4c4
6
+ metadata.gz: eb0f5c09deb989f4bfede6bfa8f1e1cee914188b72923e69a98a468746564a8a030587130691a38eb58b97434f0258ac452065120c295fd2ba0138e68c004ce3
7
+ data.tar.gz: c16127b078adb408b6076c2b5bbf60b0f93a29520398e46e916b46ff43ed55eb29cb095ce20bccf62bd2b4c713da9c030a0349c128f7a64b72c5390140d1e7df
data/Appraisals CHANGED
@@ -1,4 +1,4 @@
1
- %w(4.1 4.2).each do |version|
1
+ %w(4.1 4.2 5.0).each do |version|
2
2
  appraise "rails-#{version.gsub(/\./, "-")}" do
3
3
  gem "rails", "~> #{version}.0"
4
4
  end
data/README.md CHANGED
@@ -57,6 +57,16 @@ eos
57
57
  ActiveRecord::Base.value_from_sql(sql) # => single value
58
58
  ```
59
59
 
60
+ ```
61
+ sql = <<-eos
62
+ SELECT users.id, users.name
63
+ FROM users
64
+ LIMIT 1
65
+ eos
66
+
67
+ ActiveRecord::Base.tuple_from_sql(sql) # => [id, name]
68
+ ```
69
+
60
70
  ## Contributing
61
71
 
62
72
  1. Fork it ( https://github.com/jcoleman/relation_to_struct/fork )
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.0.0"
6
+
7
+ gemspec :path => "../"
@@ -2,8 +2,13 @@ module RelationToStruct::ActiveRecordBaseExtension
2
2
  extend ::ActiveSupport::Concern
3
3
 
4
4
  module ClassMethods
5
+ def _sanitize_sql_for_relation_to_struct(sql)
6
+ sanitized_sql = ActiveRecord::VERSION::MAJOR >= 5 ? sanitize_sql(sql) : sanitize_sql(sql, nil)
7
+ end
8
+
5
9
  def structs_from_sql(struct_class, sql, binds=[])
6
- result = connection.select_all(sanitize_sql(sql, nil), "Structs SQL Load", binds)
10
+ sanitized_sql = _sanitize_sql_for_relation_to_struct(sql)
11
+ result = connection.select_all(sanitized_sql, "Structs SQL Load", binds)
7
12
 
8
13
  if result.columns.size != result.columns.uniq.size
9
14
  raise ArgumentError, 'Expected column names to be unique'
@@ -25,12 +30,14 @@ module RelationToStruct::ActiveRecordBaseExtension
25
30
  end
26
31
 
27
32
  def pluck_from_sql(sql, binds=[])
28
- result = connection.select_all(sanitize_sql(sql, nil), "Pluck SQL Load", binds)
33
+ sanitized_sql = _sanitize_sql_for_relation_to_struct(sql)
34
+ result = connection.select_all(sanitized_sql, "Pluck SQL Load", binds)
29
35
  result.cast_values()
30
36
  end
31
37
 
32
38
  def value_from_sql(sql, binds=[])
33
- result = connection.select_all(sanitize_sql(sql, nil), "Value SQL Load", binds)
39
+ sanitized_sql = _sanitize_sql_for_relation_to_struct(sql)
40
+ result = connection.select_all(sanitized_sql, "Value SQL Load", binds)
34
41
  raise ArgumentError, 'Expected exactly one column to be selected' unless result.columns.size == 1
35
42
 
36
43
  values = result.cast_values()
@@ -45,7 +52,8 @@ module RelationToStruct::ActiveRecordBaseExtension
45
52
  end
46
53
 
47
54
  def tuple_from_sql(sql, binds=[])
48
- result = connection.select_all(sanitize_sql(sql, nil), "Value SQL Load", binds)
55
+ sanitized_sql = _sanitize_sql_for_relation_to_struct(sql)
56
+ result = connection.select_all(sanitized_sql, "Value SQL Load", binds)
49
57
  values = result.cast_values()
50
58
 
51
59
  case values.size
@@ -15,14 +15,14 @@ module RelationToStruct::ActiveRecordRelationExtension
15
15
  raise ArgumentError, 'Expected column names to be unique'
16
16
  end
17
17
 
18
- result.cast_values(klass.column_types)
18
+ values_after_casting = ActiveRecord::VERSION::MAJOR >= 5 ? result.cast_values() : result.cast_values(klass.column_types)
19
19
 
20
20
  if result.columns.size == 1
21
- result.cast_values(klass.column_types).map do |tuple|
21
+ values_after_casting.map do |tuple|
22
22
  struct_class.new(tuple)
23
23
  end
24
24
  else
25
- result.cast_values(klass.column_types).map do |tuple|
25
+ values_after_casting.map do |tuple|
26
26
  struct_class.new(*tuple)
27
27
  end
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module RelationToStruct
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -26,6 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "pry-byebug"
27
27
  spec.add_development_dependency "pg"
28
28
 
29
- spec.add_dependency "activerecord", "~> 4.1"
30
- spec.add_dependency "activesupport", "~> 4.1"
29
+ spec.add_dependency "activerecord", ">= 4.1", "< 5.1"
30
+ spec.add_dependency "activesupport", ">= 4.1", "< 5.1"
31
31
  end
@@ -13,17 +13,17 @@ ActiveRecord::Base.configurations = {
13
13
  }
14
14
 
15
15
  env = ENV['DATABASE'] ||= 'sqlite'
16
+ config = ActiveRecord::Base.configurations[env]
17
+
16
18
  case env
17
19
  when 'postgresql'
18
20
  ActiveRecord::Tasks::DatabaseTasks.instance_variable_set('@env', env)
19
- config = ActiveRecord::Base.configurations[env]
20
-
21
21
  ActiveRecord::Tasks::DatabaseTasks.drop_current
22
22
  ActiveRecord::Tasks::DatabaseTasks.create_current
23
23
  ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, File.expand_path('../schema.rb', __FILE__))
24
- ActiveRecord::Base.establish_connection(env)
24
+ ActiveRecord::Base.establish_connection(config)
25
25
  when 'sqlite'
26
- ActiveRecord::Base.establish_connection(env)
26
+ ActiveRecord::Base.establish_connection(config)
27
27
  require_relative 'schema'
28
28
  else
29
29
  raise ArgumentError, 'Unrecognized ENV["DATABASE"] argument.'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relation_to_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Coleman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-14 00:00:00.000000000 Z
11
+ date: 2017-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -112,30 +112,42 @@ dependencies:
112
112
  name: activerecord
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - "~>"
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '4.1'
118
+ - - "<"
119
+ - !ruby/object:Gem::Version
120
+ version: '5.1'
118
121
  type: :runtime
119
122
  prerelease: false
120
123
  version_requirements: !ruby/object:Gem::Requirement
121
124
  requirements:
122
- - - "~>"
125
+ - - ">="
123
126
  - !ruby/object:Gem::Version
124
127
  version: '4.1'
128
+ - - "<"
129
+ - !ruby/object:Gem::Version
130
+ version: '5.1'
125
131
  - !ruby/object:Gem::Dependency
126
132
  name: activesupport
127
133
  requirement: !ruby/object:Gem::Requirement
128
134
  requirements:
129
- - - "~>"
135
+ - - ">="
130
136
  - !ruby/object:Gem::Version
131
137
  version: '4.1'
138
+ - - "<"
139
+ - !ruby/object:Gem::Version
140
+ version: '5.1'
132
141
  type: :runtime
133
142
  prerelease: false
134
143
  version_requirements: !ruby/object:Gem::Requirement
135
144
  requirements:
136
- - - "~>"
145
+ - - ">="
137
146
  - !ruby/object:Gem::Version
138
147
  version: '4.1'
148
+ - - "<"
149
+ - !ruby/object:Gem::Version
150
+ version: '5.1'
139
151
  description: ''
140
152
  email:
141
153
  - jtc331@gmail.com
@@ -152,6 +164,7 @@ files:
152
164
  - Rakefile
153
165
  - gemfiles/rails_4_1.gemfile
154
166
  - gemfiles/rails_4_2.gemfile
167
+ - gemfiles/rails_5_0.gemfile
155
168
  - lib/relation_to_struct.rb
156
169
  - lib/relation_to_struct/active_record_base_extension.rb
157
170
  - lib/relation_to_struct/active_record_relation_extension.rb
@@ -186,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
199
  version: '0'
187
200
  requirements: []
188
201
  rubyforge_project:
189
- rubygems_version: 2.5.1
202
+ rubygems_version: 2.4.5.1
190
203
  signing_key:
191
204
  specification_version: 4
192
205
  summary: Return struct results from ActiveRecord relation queries
@@ -199,4 +212,3 @@ test_files:
199
212
  - spec/active_record_relation_spec.rb
200
213
  - spec/relation_to_struct_spec.rb
201
214
  - spec/spec_helper.rb
202
- has_rdoc: