querier 0.0.9 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -14
  3. data/lib/querier.rb +49 -36
  4. metadata +7 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd47eff36c3f752f277ce56bcd18635ef34f9a79d7765fdd91b56ae20f06444d
4
- data.tar.gz: 6f95ffd91d3e52181eeb33ddc2f0fd3a444035365542f774c7b9ad8c923962e7
3
+ metadata.gz: de1729ce8cc885a52a8a5892e79d2a4e4008b48d95d5a821c2f985bc5925abf9
4
+ data.tar.gz: 0db5814a6b46ecd4d73f766763d1f82347c2fecf3075c0f1c3218d6870b4cfa9
5
5
  SHA512:
6
- metadata.gz: 5a861883fac193952c33733c58ed2701d7424ad22196618d199fdd7a469075d0689196baa2cea9d0c3e5ac212ba99a252aff9d2670cdc4a382da4d844839615f
7
- data.tar.gz: 343f454ff2489dea937cc6906c262d214383efb2b63d774a1e0f240b113691d04805f3f0e9656e50d1b539e35933a550bfe3af35e303bcb7d2cbfce8cf706df0
6
+ metadata.gz: b3384fe46ceb0cbfbd19e15caa51d4e8bb14e77e2565d4db001f773d6fb2d8ac1a88f53ede1527f150ae3d916a50d41e7ac7aed789ca59f72b58a9f0f1871c8f
7
+ data.tar.gz: 343e400b47d07e0e8e55be4b025fbca6c3e672e01ece56ab3bd6c13061eb9e1597d27dfe70c36ebeb17904efcc2676a4ee7f90ffc921f1f63b9799755f5af88d
data/README.md CHANGED
@@ -1,22 +1,11 @@
1
1
  # querier
2
2
 
3
3
  # class UserQuerier < Querier
4
- QUERY_TEMPLATE = <<-END_OF_QUERY_TEMPLATE
5
-
6
- SELECT
7
- *
8
- FROM
9
- users
10
- WHERE
11
- name = {?user_name}
12
- AND active = {?active}
13
-
14
- END_OF_QUERY_TEMPLATE
15
-
4
+ @active_record_class = ApplicationRecord
16
5
  def initialize user_name:, active:
17
- @query_template = QUERY_TEMPLATE
6
+ @query_template = "SELECT * FROM users WHERE name = ${user_name} AND active = ${active}"
18
7
  super
19
8
  end
20
9
  end
21
10
 
22
- # UserQuerier.new(user_name: 'foo', active: true).execute
11
+ # UserQuerier.new(user_name: 'foo', active: true).execute
data/lib/querier.rb CHANGED
@@ -2,77 +2,90 @@ class Querier
2
2
  PARAM_NAME_INDEX = 0
3
3
  PARAM_VALUE_INDEX = 1
4
4
 
5
+ @active_record_class = ActiveRecord::Base
6
+ # based on rubocop's tips at: https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/ClassVars
7
+ # solution here: https://www.ruby-lang.org/en/documentation/faq/8/
8
+ class << self
9
+ attr_accessor :active_record_class
10
+ end
11
+
5
12
  attr_reader :query_execution_count, :query_template, :query_params
6
-
7
- def initialize **template_query_params
13
+
14
+ def initialize(**template_query_params)
15
+ @active_record_class = self.class.active_record_class || self.class.superclass.active_record_class
8
16
  @query_execution_count = 0
9
17
  @query_params = template_query_params.dup
10
18
  end
11
19
 
12
20
  def execute
13
21
  @query_execution_count += 1
14
- @execution_cached_result = ActiveRecord::Base.connection.select_all(fill_query_params(query_template: @query_template, query_params: @query_params))
15
- @execution_cached_result.map! {|record| record.symbolize_keys!}
16
- end
22
+ @execution_cached_result = @active_record_class.connection.select_all(fill_query_params(query_template: @query_template,
23
+ query_params: @query_params)).map(&:symbolize_keys!)
24
+ end
17
25
 
18
- def cached_result format: :hash
26
+ def cached_result(format: :hash)
19
27
  raise 'query not executed yet' if @query_execution_count.eql?(0)
20
-
21
- case format.to_s
22
- when 'hash'
23
- @execution_cached_result
24
- when 'open_struct'
25
- hash_to_open_struct(dataset: @execution_cached_result)
26
- else
27
- raise 'invalid value type'
28
+
29
+ case format
30
+ when :hash
31
+ @execution_cached_result
32
+ when :open_struct
33
+ hash_to_open_struct(dataset: @execution_cached_result)
34
+ else
35
+ raise 'invalid value type'
28
36
  end
29
37
  end
30
-
38
+
31
39
  def structured_results
32
- hash_to_open_struct dataset: self.execute
33
- end
40
+ hash_to_open_struct(dataset: execute)
41
+ end
34
42
 
35
43
  def to_sql
36
44
  fill_query_params(query_template: @query_template, query_params: @query_params)
37
45
  end
38
46
 
39
47
  def to_file
40
- file_name = "querier #{Time.now.strftime "[%d-%m-%Y]-[%Hh %Mm %Ss]"}.sql"
41
- File.open("tmp/#{file_name}", 'w') {|f| f << self.to_sql}
48
+ file_name = "querier #{Time.now.strftime '[%d-%m-%Y]-[%Hh %Mm %Ss]'}.sql"
49
+ File.open("tmp/#{file_name}", 'w') { |f| f << to_sql }
42
50
  end
43
51
 
44
- def field_group_and_count field_name:, sort_element_index: nil, reverse_sort: true
45
- count_result = self.cached_results(format: :open_struct).group_by(&field_name).map {|k, v| [k, v.count]}
46
-
52
+ def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true)
53
+ count_result = cached_result(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] }
54
+
47
55
  unless sort_element_index.nil?
48
- count_result = count_result.sort_by {|el| el[sort_element_index]}
56
+ count_result = count_result.sort_by { |el| el[sort_element_index] }
49
57
  count_result.reverse! if reverse_sort.eql? true
50
58
  end
51
-
59
+
52
60
  count_result
53
- end
61
+ end
54
62
 
55
63
  private
56
64
 
57
- def hash_to_open_struct dataset:
58
- dataset.map {|record| OpenStruct.new(record.symbolize_keys!)}
59
- end
60
-
61
- def get_param_value raw_query_param, quotefy_param=true
65
+ def hash_to_open_struct(dataset:)
66
+ dataset.map { |record| OpenStruct.new(record.symbolize_keys!) }
67
+ end
68
+
69
+ def get_param_value(raw_query_param:, quotefy_param: true)
62
70
  # where's String#quote when we need it?
63
- raw_query_param.class.eql?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
71
+ raw_query_param.instance_of?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
64
72
  end
65
73
 
66
- def fill_query_params query_template:, query_params:
74
+ def fill_query_params(query_template:, query_params:)
67
75
  query = query_template.dup
68
-
76
+
69
77
  query_params.each do |query_param|
70
78
  query_param_name = query_param[PARAM_NAME_INDEX].to_s
71
79
 
72
- query.gsub! /{\?#{query_param_name}}/, get_param_value(query_param[PARAM_VALUE_INDEX], true)
73
- query.gsub! /{\?#{query_param_name}\/no_quote}/, get_param_value(query_param[PARAM_VALUE_INDEX], false)
80
+ query.gsub!(/\${#{query_param_name}}/,
81
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
82
+ quotefy_param: true))
83
+
84
+ query.gsub!(/\${#{query_param_name}\/no_quote}/,
85
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
86
+ quotefy_param: false))
74
87
  end
75
88
 
76
89
  query
77
90
  end
78
- end
91
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: querier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-28 00:00:00.000000000 Z
11
+ date: 2021-08-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Active Record queries with variable number of params
14
14
  email: gedean.dias@gmail.com
@@ -22,7 +22,7 @@ homepage: https://github.com/gedean/querier
22
22
  licenses:
23
23
  - MIT
24
24
  metadata: {}
25
- post_install_message:
25
+ post_install_message:
26
26
  rdoc_options: []
27
27
  require_paths:
28
28
  - lib
@@ -30,16 +30,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.0.0
33
+ version: 2.7.0
34
34
  required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
36
  - - ">="
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
- rubyforge_project:
41
- rubygems_version: 2.7.7
42
- signing_key:
40
+ rubygems_version: 3.2.25
41
+ signing_key:
43
42
  specification_version: 4
44
43
  summary: Active Record Querier
45
44
  test_files: []