querier 0.0.7 → 0.2.0

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 +2 -14
  3. data/lib/querier.rb +49 -35
  4. metadata +7 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f6095a76853086f43c79e597c28a20917d6b0fbc0899a06e7bf7e8b4e149426
4
- data.tar.gz: cd705a152f12b102461c116c53fcdd3df45d084304e9c68da8b23c038dea5254
3
+ metadata.gz: b665dcde0c37b483e764ddcf1d30221ba9c4b34f478533e6fb911aaf11a81996
4
+ data.tar.gz: 4bd3bb6e6e3cc430b257ac9b408629440b8249ad642c1db89a6f9ece21d671f3
5
5
  SHA512:
6
- metadata.gz: 66791bebaf7e2821d53426ae68ed684bd0d76a5a83c7fc3c4130d417c98632cbab8a366bbaf15f4f3bc632a2119be5107b6815ace2fb11fe1a8b3d06a7a056c2
7
- data.tar.gz: af12dd6bf950ae71ed78945258770330fb005bb3f94589dea44f2e6dce99717aca1c73b3424698a44ac589fef3340115c06d7295561dd0fc54992e88305bad95
6
+ metadata.gz: 5e0fc946d4e39bc164ffeda2bd762c4e3bddc4f769363d9590e4dc08a6a7379b3f3e27f5516b93baaea2a8c8216ac2808946096a0f48a28b847246d8a73d1da7
7
+ data.tar.gz: 3af81af98208e77a0f9360de68a5d15cd45e153613aaa5e1654157bcf4ea4ccf77977c80d1e30b22259db96eabff5d13bc6b66a843c4cbbcb210ad114ac905c6
data/README.md CHANGED
@@ -1,22 +1,10 @@
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
-
16
4
  def initialize user_name:, active:
17
- @query_template = QUERY_TEMPLATE
5
+ @query_template = "SELECT * FROM users WHERE name = ${user_name} AND active = ${active}"
18
6
  super
19
7
  end
20
8
  end
21
9
 
22
- # UserQuerier.new(user_name: 'foo', active: true).execute
10
+ # UserQuerier.new(user_name: 'foo', active: true).execute
data/lib/querier.rb CHANGED
@@ -2,76 +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
- 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
16
25
 
17
- def cached_result format: :hash
26
+ def cached_result(format: :hash)
18
27
  raise 'query not executed yet' if @query_execution_count.eql?(0)
19
-
20
- case format.to_s
21
- when 'hash'
22
- @execution_cached_result
23
- when 'open_struct'
24
- hash_to_open_struct(dataset: @execution_cached_result)
25
- else
26
- 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'
27
36
  end
28
37
  end
29
-
38
+
30
39
  def structured_results
31
- hash_to_open_struct dataset: self.execute
32
- end
40
+ hash_to_open_struct(dataset: execute)
41
+ end
33
42
 
34
43
  def to_sql
35
44
  fill_query_params(query_template: @query_template, query_params: @query_params)
36
45
  end
37
46
 
38
47
  def to_file
39
- file_name = "querier #{Time.now.strftime "[%d-%m-%Y]-[%Hh %Mm %Ss]"}.sql"
40
- 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 }
41
50
  end
42
51
 
43
- def field_group_and_count field_name:, sort_element_index: nil, reverse_sort: true
44
- count_result = self.cached_results(format: :open_struct).group_by(&field_name).map {|k, v| [k, v.count]}
45
-
52
+ def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true)
53
+ count_result = cached_results(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] }
54
+
46
55
  unless sort_element_index.nil?
47
- count_result = count_result.sort_by {|el| el[sort_element_index]}
56
+ count_result = count_result.sort_by { |el| el[sort_element_index] }
48
57
  count_result.reverse! if reverse_sort.eql? true
49
58
  end
50
-
59
+
51
60
  count_result
52
- end
61
+ end
53
62
 
54
63
  private
55
64
 
56
- def hash_to_open_struct dataset:
57
- dataset.map {|record| OpenStruct.new(record.symbolize_keys!)}
58
- end
59
-
60
- def get_param_value raw_query_param
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)
61
70
  # where's String#quote when we need it?
62
- raw_query_param.class.eql?(String) ? "'#{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
63
72
  end
64
73
 
65
- def fill_query_params query_template:, query_params:
74
+ def fill_query_params(query_template:, query_params:)
66
75
  query = query_template.dup
67
-
76
+
68
77
  query_params.each do |query_param|
69
78
  query_param_name = query_param[PARAM_NAME_INDEX].to_s
70
- query_param_value = get_param_value(query_param[PARAM_VALUE_INDEX])
71
79
 
72
- query.gsub! /{\?#{query_param_name}}/, query_param_value
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))
73
87
  end
74
88
 
75
89
  query
76
90
  end
77
- 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.7
4
+ version: 0.2.0
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-01-29 00:00:00.000000000 Z
11
+ date: 2021-04-05 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.4
42
- signing_key:
40
+ rubygems_version: 3.2.15
41
+ signing_key:
43
42
  specification_version: 4
44
43
  summary: Active Record Querier
45
44
  test_files: []