wise_gopher 0.2.0 → 0.3.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
  SHA256:
3
- metadata.gz: dc88a6090305daa53b83df1cd298531d6209dbb74cce4aa2c1492a2c3987c168
4
- data.tar.gz: 874dc19cf652522bdb9ebf89e45543c5ce912dd8a636d6dd01bc7ab7e78e10ec
3
+ metadata.gz: 1b5ee8812c5f43c35badd034435bc43135bde85326b16aaa943047fe8ba6c6cb
4
+ data.tar.gz: 5ff89577e0ee88db9eeb45b89dd7a40357f0a2cd0e8cc6eb1c8e68fc2f323f14
5
5
  SHA512:
6
- metadata.gz: 4b0f65b90b90414aa03e572e33e0d3ac360f886c61afd19dc3b026b9ff0e76c9fd7040b77cf8c1b1bf89dcddd665d36e1125ff5ec27ff21c73d232b42dfeab09
7
- data.tar.gz: ed4ceb261617680856ad9896b68b1ac0711ae911edf264d3d14fde7a5b7405b2d6843cd03333be036927fe762e45b7054e023d6f53e353cd19e117f05e451db7
6
+ metadata.gz: 45dc2fe309a15e395539e80c4e0ccb153382633508eed87d2d8ea8617542da9c9e98c7e31bb564be482a90487f4586ce9615fdac6987255ac91e4c74aeac4911
7
+ data.tar.gz: 7563bd1ef7407c2ab244776ace9b8ee5a21c14f42cf294a2cda4f6ab00aef2018760f6a86df5c37cd7862f118d024be22648da5bb140afcba592da002cbb786e
data/.gitignore CHANGED
@@ -9,3 +9,6 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ # test file
14
+ .testing.rb
data/Gemfile.lock CHANGED
@@ -1,35 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wise_gopher (0.2.0)
5
- activerecord (>= 5, < 7)
4
+ wise_gopher (0.3.2)
5
+ activerecord (~> 7, >= 5)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (6.1.4)
11
- activesupport (= 6.1.4)
12
- activerecord (6.1.4)
13
- activemodel (= 6.1.4)
14
- activesupport (= 6.1.4)
15
- activesupport (6.1.4)
10
+ activemodel (7.0.2.3)
11
+ activesupport (= 7.0.2.3)
12
+ activerecord (7.0.2.3)
13
+ activemodel (= 7.0.2.3)
14
+ activesupport (= 7.0.2.3)
15
+ activesupport (7.0.2.3)
16
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
17
  i18n (>= 1.6, < 2)
18
18
  minitest (>= 5.1)
19
19
  tzinfo (~> 2.0)
20
- zeitwerk (~> 2.3)
21
20
  ast (2.4.2)
22
21
  byebug (11.1.3)
23
22
  coderay (1.1.3)
24
- concurrent-ruby (1.1.9)
23
+ concurrent-ruby (1.1.10)
25
24
  database_cleaner (1.99.0)
26
- diff-lcs (1.4.4)
27
- i18n (1.8.10)
25
+ diff-lcs (1.5.0)
26
+ i18n (1.10.0)
28
27
  concurrent-ruby (~> 1.0)
29
28
  method_source (1.0.0)
30
- minitest (5.14.4)
31
- parallel (1.20.1)
32
- parser (3.0.1.1)
29
+ minitest (5.15.0)
30
+ parallel (1.22.1)
31
+ parser (3.1.2.0)
33
32
  ast (~> 2.4.1)
34
33
  pry (0.14.1)
35
34
  coderay (~> 1.1)
@@ -37,51 +36,52 @@ GEM
37
36
  pry-byebug (3.8.0)
38
37
  byebug (~> 11.0)
39
38
  pry (~> 0.10)
40
- pry-doc (1.1.0)
39
+ pry-doc (1.3.0)
41
40
  pry (~> 0.11)
42
41
  yard (~> 0.9.11)
43
- rainbow (3.0.0)
44
- rake (13.0.3)
45
- regexp_parser (2.1.1)
42
+ rainbow (3.1.1)
43
+ rake (13.0.6)
44
+ regexp_parser (2.3.0)
46
45
  rexml (3.2.5)
47
- rspec (3.10.0)
48
- rspec-core (~> 3.10.0)
49
- rspec-expectations (~> 3.10.0)
50
- rspec-mocks (~> 3.10.0)
51
- rspec-core (3.10.1)
52
- rspec-support (~> 3.10.0)
53
- rspec-expectations (3.10.1)
46
+ rspec (3.11.0)
47
+ rspec-core (~> 3.11.0)
48
+ rspec-expectations (~> 3.11.0)
49
+ rspec-mocks (~> 3.11.0)
50
+ rspec-core (3.11.0)
51
+ rspec-support (~> 3.11.0)
52
+ rspec-expectations (3.11.0)
54
53
  diff-lcs (>= 1.2.0, < 2.0)
55
- rspec-support (~> 3.10.0)
56
- rspec-mocks (3.10.2)
54
+ rspec-support (~> 3.11.0)
55
+ rspec-mocks (3.11.1)
57
56
  diff-lcs (>= 1.2.0, < 2.0)
58
- rspec-support (~> 3.10.0)
59
- rspec-support (3.10.2)
60
- rubocop (1.17.0)
57
+ rspec-support (~> 3.11.0)
58
+ rspec-support (3.11.0)
59
+ rubocop (1.27.0)
61
60
  parallel (~> 1.10)
62
- parser (>= 3.0.0.0)
61
+ parser (>= 3.1.0.0)
63
62
  rainbow (>= 2.2.2, < 4.0)
64
63
  regexp_parser (>= 1.8, < 3.0)
65
64
  rexml
66
- rubocop-ast (>= 1.7.0, < 2.0)
65
+ rubocop-ast (>= 1.16.0, < 2.0)
67
66
  ruby-progressbar (~> 1.7)
68
67
  unicode-display_width (>= 1.4.0, < 3.0)
69
- rubocop-ast (1.7.0)
70
- parser (>= 3.0.1.1)
71
- rubocop-rake (0.5.1)
72
- rubocop
73
- rubocop-rspec (2.4.0)
68
+ rubocop-ast (1.17.0)
69
+ parser (>= 3.1.1.0)
70
+ rubocop-rake (0.6.0)
74
71
  rubocop (~> 1.0)
75
- rubocop-ast (>= 1.1.0)
72
+ rubocop-rspec (2.9.0)
73
+ rubocop (~> 1.19)
76
74
  ruby-progressbar (1.11.0)
77
75
  sqlite3 (1.4.2)
78
76
  tzinfo (2.0.4)
79
77
  concurrent-ruby (~> 1.0)
80
- unicode-display_width (2.0.0)
81
- yard (0.9.26)
82
- zeitwerk (2.4.2)
78
+ unicode-display_width (2.1.0)
79
+ webrick (1.7.0)
80
+ yard (0.9.27)
81
+ webrick (~> 1.7.0)
83
82
 
84
83
  PLATFORMS
84
+ x86_64-darwin-21
85
85
  x86_64-linux
86
86
 
87
87
  DEPENDENCIES
@@ -98,4 +98,4 @@ DEPENDENCIES
98
98
  wise_gopher!
99
99
 
100
100
  BUNDLED WITH
101
- 2.2.20
101
+ 2.3.9
data/README.md CHANGED
@@ -9,7 +9,7 @@ This gem tries to solve some problems found when you need to execute custom and/
9
9
  3. The column types are not always correctly retrieved by ActiveRecord, or sometimes you need a little more ruby treatment on the value before using it.
10
10
 
11
11
  [This article](https://blog.saeloun.com/2019/10/28/bind-parameters-in-activerecord-sql-queries.html) describe the benefits of using bind parameters with ActiveRecord.
12
- [This one](https://use-the-index-luke.com/sql/where-clause/bind-parameters) goes further one the subject.
12
+ [This one](https://use-the-index-luke.com/sql/where-clause/bind-parameters) goes further on the subject.
13
13
 
14
14
  The basic idea of this gem is to provide you a way to declare what your query needs as input, what columns it returns and their type. In returns it will allow you to retrieve the rows from result as an array of plain Ruby objects. It will also dynamically creates a class for the row objects that you can customize or can provide it yourself.
15
15
 
@@ -59,9 +59,9 @@ class PopularArticle < WiseGopher::Base
59
59
 
60
60
  row do
61
61
  column :title, :string, transform: :capitalize
62
- column :average_rating, :float, -> { round(2) }
62
+ column :average_rating, :float, transform: -> { round(2) }
63
63
  column :published_at, :datetime
64
- column :author_username, as: :author
64
+ column :author_username, :string, as: :author
65
65
 
66
66
  def to_s
67
67
  "Article '#{title}' by #{author} is rated #{"%.2f" % average_rating}/5."
@@ -151,7 +151,7 @@ end
151
151
 
152
152
  ------
153
153
  ## Raw params
154
- If you need to dinamically interpolate raw SQL in your query, you can use `raw_param`. The value passed with `execute_with` will be interpolated in the base query before inserting the other params.
154
+ If you need to dynamically interpolate raw SQL in your query, you can use `raw_param`. The value passed with `execute_with` will be interpolated in the base query before inserting the other params.
155
155
  ```ruby
156
156
  class AnnualReport < WiseGopher::Base
157
157
  query <<-SQL
@@ -5,13 +5,19 @@ require "forwardable"
5
5
  module WiseGopher
6
6
  # Main inteface of the gem. Class to be inherited by the query class
7
7
  class Base
8
- def self.inherited(base)
9
- base.class_eval do
10
- @raw_params = {}
11
- @params = {}
8
+ def self.inherited(child_class)
9
+ parent_class = self
10
+ child_class.extend ClassMethods
11
+ child_class.set_defaults
12
+
13
+ # if child_class is already a WiseGopher::Base
14
+ child_class.ancestors.include?(Methods) && child_class.class_eval do
15
+ @raw_params = parent_class.raw_params.deep_dup
16
+ @params = parent_class.params.deep_dup
17
+ @row_class = parent_class.row_class
12
18
  end
13
- base.include Methods
14
- base.extend ClassMethods
19
+
20
+ child_class.include Methods
15
21
  end
16
22
 
17
23
  # class methods for WiseGopher::Base
@@ -41,7 +47,7 @@ module WiseGopher
41
47
  def row(base = nil, &block)
42
48
  @row_class ||= base || define_generic_row_class
43
49
 
44
- @row_class.include WiseGopher::Row
50
+ @row_class.include WiseGopher::Row unless @row_class.ancestors.include?(WiseGopher::Row)
45
51
 
46
52
  @row_class.class_eval(&block) if block_given?
47
53
  end
@@ -64,6 +70,11 @@ module WiseGopher
64
70
  raise WiseGopher::ArgumentError, required_params.slice(*missing_params) if missing_params.any?
65
71
  end
66
72
 
73
+ def set_defaults
74
+ @raw_params = {}
75
+ @params = {}
76
+ end
77
+
67
78
  private
68
79
 
69
80
  def define_generic_row_class
@@ -90,7 +101,6 @@ module WiseGopher
90
101
  def initialize(inputs = {})
91
102
  @inputs = inputs
92
103
  @binds = []
93
- @bind_symbol = WiseGopher.postgresql? ? +"$1" : "?"
94
104
  @query_prepared = false
95
105
 
96
106
  self.class.ensure_all_params_are_given(inputs)
@@ -169,16 +179,20 @@ module WiseGopher
169
179
 
170
180
  def use_bind_symbol
171
181
  if WiseGopher.postgresql?
172
- symbol = @bind_symbol.dup
182
+ symbol = bind_symbol.dup
173
183
 
174
- @bind_symbol.next!
184
+ bind_symbol.next!
175
185
 
176
186
  symbol
177
187
  else
178
- @bind_symbol
188
+ bind_symbol
179
189
  end
180
190
  end
181
191
 
192
+ def bind_symbol
193
+ @bind_symbol ||= WiseGopher.postgresql? ? +"$1" : "?"
194
+ end
195
+
182
196
  def ensure_row_class_is_declared
183
197
  raise RowClassIsMissing unless row_class
184
198
  end
@@ -3,21 +3,25 @@
3
3
  module WiseGopher
4
4
  # Cast query columns and transform value
5
5
  class Column
6
- attr_reader :name, :type, :alias
6
+ attr_reader :name, :alias
7
7
 
8
8
  def initialize(name, type_symbol, transform: nil, as: nil)
9
- @alias = as&.to_s.freeze || name.to_s.freeze
10
- @name = name.to_s.freeze
11
- @type = ActiveRecord::Type.lookup type_symbol
9
+ @alias = as&.to_s.freeze || name.to_s.freeze
10
+ @name = name.to_s.freeze
11
+ @type_symbol = type_symbol
12
12
  @transform = transform&.to_proc
13
13
  end
14
14
 
15
15
  def cast(value)
16
- casted_value = @type.deserialize(value)
16
+ casted_value = type.deserialize(value)
17
17
 
18
18
  @transform ? transform_value(casted_value) : casted_value
19
19
  end
20
20
 
21
+ def type
22
+ @type ||= ActiveRecord::Type.lookup type_symbol
23
+ end
24
+
21
25
  def define_getter(row_class)
22
26
  column = self
23
27
 
@@ -32,6 +36,8 @@ module WiseGopher
32
36
 
33
37
  private
34
38
 
39
+ attr_reader :type_symbol
40
+
35
41
  def transform_value(value)
36
42
  case @transform.arity
37
43
  when 0 then value.instance_exec(&@transform)
@@ -3,11 +3,11 @@
3
3
  module WiseGopher
4
4
  # Register query's params and build query's bind variables
5
5
  class Param
6
- attr_reader :name, :type
6
+ attr_reader :name
7
7
 
8
8
  def initialize(name, type_symbol, transform = nil)
9
- @name = name.to_s.freeze
10
- @type = ActiveRecord::Type.lookup type_symbol
9
+ @name = name.to_s.freeze
10
+ @type_symbol = type_symbol
11
11
  @transform = transform&.to_proc
12
12
  end
13
13
 
@@ -17,8 +17,14 @@ module WiseGopher
17
17
  ActiveRecord::Relation::QueryAttribute.new(name, prepared_value, type)
18
18
  end
19
19
 
20
+ def type
21
+ @type ||= ActiveRecord::Type.lookup type_symbol
22
+ end
23
+
20
24
  private
21
25
 
26
+ attr_reader :type_symbol
27
+
22
28
  def transform_value(value)
23
29
  case @transform.arity
24
30
  when 0 then value.instance_exec(&@transform)
@@ -3,8 +3,8 @@
3
3
  module WiseGopher
4
4
  module VERSION
5
5
  MAJOR = 0
6
- MINOR = 2
7
- PATCH = 0
6
+ MINOR = 3
7
+ PATCH = 2
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
10
10
  end
data/wise_gopher.gemspec CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
- spec.add_dependency "activerecord", ">= 5", "< 7"
35
+ spec.add_dependency "activerecord", ">= 5", "~> 7"
36
36
 
37
37
  spec.add_development_dependency "database_cleaner", "~> 1.5"
38
38
  spec.add_development_dependency "pry"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wise_gopher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - PageHey
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-04 00:00:00.000000000 Z
11
+ date: 2022-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,7 +17,7 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '5'
20
- - - "<"
20
+ - - "~>"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '7'
23
23
  type: :runtime
@@ -27,7 +27,7 @@ dependencies:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '5'
30
- - - "<"
30
+ - - "~>"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '7'
33
33
  - !ruby/object:Gem::Dependency
@@ -210,7 +210,7 @@ metadata:
210
210
  homepage_uri: https://github.com/Pagehey/wise_gopher
211
211
  source_code_uri: https://github.com/Pagehey/wise_gopher
212
212
  changelog_uri: https://github.com/Pagehey/wise_gopher/blob/main/CHANGELOG.md
213
- post_install_message:
213
+ post_install_message:
214
214
  rdoc_options: []
215
215
  require_paths:
216
216
  - lib
@@ -225,8 +225,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
225
  - !ruby/object:Gem::Version
226
226
  version: '0'
227
227
  requirements: []
228
- rubygems_version: 3.1.6
229
- signing_key:
228
+ rubygems_version: 3.2.32
229
+ signing_key:
230
230
  specification_version: 4
231
231
  summary: Encapsulate raw SQL queries and return result as plain Ruby objects, using
232
232
  ActiveRecord.