querier 0.0.5 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/querier.rb +43 -21
  3. metadata +7 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31e48481f70f97cfdd3afb78f304b6aafab887c129aab817808a44271c9d2c56
4
- data.tar.gz: e46be7a0c70ae3e3048bfaeee3c8ae4ef47d94d2fc340d530b533b938e05eaa5
3
+ metadata.gz: f15f96f79aea614fe9eec3fa44a8ed7c6f51009c4bdb6ee18316aef6577b41f9
4
+ data.tar.gz: e4d5d7abe1357c899a4f6e45fa407a3fedc831779aa5af1b999dd51917986cb0
5
5
  SHA512:
6
- metadata.gz: df307669dbf4a7de150e6d3fbe197ba87dca147c21370bec05cbb9d07956f9ad6522735816d06fb8fbabe3b7f05a78bd5c2cdbe25679d2ff14989f2e6b30e1cd
7
- data.tar.gz: bbbd8830d8b90a6d2dc2a6303f043b6074dd21ed7546a96b0a209d29bf8cc3048755c58f6ed96444277af68a1cf0464b9544560b9bf746beba7649a4721b7b67
6
+ metadata.gz: 0baf3ced087fa4a9a5c1525fa0b8ae4076290a8d8af8d9be5257d83c610becf469fba2624e73ea3689d78a20cf28df811830c67bfba89198b8e148c421e4e33c
7
+ data.tar.gz: eeae2b170e2268b7569f8aaa531de5e7a49a702ad99746c32c8269c2cc0679ecd4e5d054c9a189e00ab1270faa1d3762c76efcc761c1a5c0510d8a0ecd41acbe
@@ -2,21 +2,34 @@ class Querier
2
2
  PARAM_NAME_INDEX = 0
3
3
  PARAM_VALUE_INDEX = 1
4
4
 
5
- attr_reader :query_template, :query_params
5
+ attr_reader :query_execution_count, :query_template, :query_params
6
6
 
7
7
  def initialize **template_query_params
8
+ @query_execution_count = 0
8
9
  @query_params = template_query_params.dup
9
10
  end
10
11
 
11
12
  def execute
12
- ActiveRecord::Base.connection.select_all fill_query_params(query_template: @query_template, query_params: @query_params)
13
+ @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 = execution_cached_result.map! { |record| record.symbolize_keys! }
13
16
  end
14
17
 
18
+ def cached_result format: :hash
19
+ 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
+ end
29
+ end
30
+
15
31
  def structured_results
16
- query_results = self.execute
17
- structured_results = []
18
- query_results.each {|query_result| structured_results << OpenStruct.new(query_result.symbolize_keys!)}
19
- structured_results
32
+ hash_to_open_struct dataset: self.execute
20
33
  end
21
34
 
22
35
  def to_sql
@@ -28,11 +41,26 @@ class Querier
28
41
  File.open("tmp/#{file_name}", 'w') {|f| f << self.to_sql}
29
42
  end
30
43
 
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
+
47
+ unless sort_element_index.nil?
48
+ count_result = count_result.sort_by {|el| el[sort_element_index]}
49
+ count_result.reverse! if reverse_sort.eql? true
50
+ end
51
+
52
+ count_result
53
+ end
54
+
31
55
  private
56
+
57
+ def hash_to_open_struct dataset:
58
+ dataset.map {|record| OpenStruct.new(record.symbolize_keys!)}
59
+ end
32
60
 
33
- def get_param_value raw_query_param
61
+ def get_param_value raw_query_param:, quotefy_param: true
34
62
  # where's String#quote when we need it?
35
- raw_query_param.class.eql?(String) ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
63
+ raw_query_param.class.eql?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
36
64
  end
37
65
 
38
66
  def fill_query_params query_template:, query_params:
@@ -40,22 +68,16 @@ class Querier
40
68
 
41
69
  query_params.each do |query_param|
42
70
  query_param_name = query_param[PARAM_NAME_INDEX].to_s
43
- query_param_value = get_param_value(query_param[PARAM_VALUE_INDEX])
44
71
 
45
- query.gsub! /{\?#{query_param_name}}/, query_param_value
72
+ query.gsub!(/\${#{query_param_name}}/,
73
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
74
+ quotefy_param: true))
75
+
76
+ query.gsub!(/\${#{query_param_name}\/no_quote}/,
77
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
78
+ quotefy_param: false))
46
79
  end
47
80
 
48
81
  query
49
82
  end
50
-
51
- def field_group_and_count field_name:, sort_element_index: nil, reverse_sort: true
52
- count_result = @cached_results.group_by(&field_name).map {|k, v| [k, v.count]}
53
-
54
- unless sort_element_index.nil?
55
- count_result = count_result.sort_by {|el| el[sort_element_index]}
56
- count_result.reverse! if reverse_sort.eql? true
57
- end
58
-
59
- count_result
60
- end
61
83
  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.5
4
+ version: 0.0.10
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-20 00:00:00.000000000 Z
11
+ date: 2020-12-28 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.3
41
+ signing_key:
43
42
  specification_version: 4
44
43
  summary: Active Record Querier
45
44
  test_files: []