querier 0.0.6 → 0.0.7

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 +23 -7
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fcc33d6a1dadd37a5731224a5ae632d86df0a90bacad5cc0795023b337c099d
4
- data.tar.gz: 27825d9168d18d5f08ceb206ccfe863694f38795e455b8fd4f3e286256219fcc
3
+ metadata.gz: 4f6095a76853086f43c79e597c28a20917d6b0fbc0899a06e7bf7e8b4e149426
4
+ data.tar.gz: cd705a152f12b102461c116c53fcdd3df45d084304e9c68da8b23c038dea5254
5
5
  SHA512:
6
- metadata.gz: 1e807dcf2db351b1b79a3c1c9fd2db8da1e3cab636d31b9792949fd1b3b8f6158a8f7baf65bfcd1b37373fb321866fe088b50d4c838244bf3922f7d737d17797
7
- data.tar.gz: 1f0bdd06785eb2286c1ab243a2b040a79f5e91f397d4cd29072a2d4937ddf7c25a4653c74d9e5cdbee8a55cc0a17a9045dd9b036f7c133ce70783a20db532821
6
+ metadata.gz: 66791bebaf7e2821d53426ae68ed684bd0d76a5a83c7fc3c4130d417c98632cbab8a366bbaf15f4f3bc632a2119be5107b6815ace2fb11fe1a8b3d06a7a056c2
7
+ data.tar.gz: af12dd6bf950ae71ed78945258770330fb005bb3f94589dea44f2e6dce99717aca1c73b3424698a44ac589fef3340115c06d7295561dd0fc54992e88305bad95
data/lib/querier.rb CHANGED
@@ -2,21 +2,33 @@ 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)
13
15
  end
14
16
 
17
+ def cached_result format: :hash
18
+ 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'
27
+ end
28
+ end
29
+
15
30
  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
31
+ hash_to_open_struct dataset: self.execute
20
32
  end
21
33
 
22
34
  def to_sql
@@ -29,7 +41,7 @@ class Querier
29
41
  end
30
42
 
31
43
  def field_group_and_count field_name:, sort_element_index: nil, reverse_sort: true
32
- count_result = @cached_results.group_by(&field_name).map {|k, v| [k, v.count]}
44
+ count_result = self.cached_results(format: :open_struct).group_by(&field_name).map {|k, v| [k, v.count]}
33
45
 
34
46
  unless sort_element_index.nil?
35
47
  count_result = count_result.sort_by {|el| el[sort_element_index]}
@@ -40,6 +52,10 @@ class Querier
40
52
  end
41
53
 
42
54
  private
55
+
56
+ def hash_to_open_struct dataset:
57
+ dataset.map {|record| OpenStruct.new(record.symbolize_keys!)}
58
+ end
43
59
 
44
60
  def get_param_value raw_query_param
45
61
  # where's String#quote when we need it?
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-21 00:00:00.000000000 Z
11
+ date: 2018-01-29 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