Class: Querier
- Inherits:
-
Object
- Object
- Querier
- Defined in:
- lib/querier.rb
Constant Summary collapse
- PARAM_NAME_INDEX =
0- PARAM_VALUE_INDEX =
1
Class Attribute Summary collapse
-
.active_record_class ⇒ Object
Returns the value of attribute active_record_class.
Instance Attribute Summary collapse
-
#query_execution_count ⇒ Object
readonly
Returns the value of attribute query_execution_count.
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#query_template ⇒ Object
readonly
Returns the value of attribute query_template.
Instance Method Summary collapse
- #cached_result(format: :hash) ⇒ Object
- #execute ⇒ Object
- #field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true) ⇒ Object
-
#initialize(template_query_params) ⇒ Querier
constructor
A new instance of Querier.
- #structured_results ⇒ Object
- #to_file ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(template_query_params) ⇒ Querier
Returns a new instance of Querier.
14 15 16 17 18 |
# File 'lib/querier.rb', line 14 def initialize(template_query_params) @active_record_class = self.class.active_record_class || self.class.superclass.active_record_class @query_execution_count = 0 @query_params = template_query_params.dup end |
Class Attribute Details
.active_record_class ⇒ Object
Returns the value of attribute active_record_class.
9 10 11 |
# File 'lib/querier.rb', line 9 def active_record_class @active_record_class end |
Instance Attribute Details
#query_execution_count ⇒ Object (readonly)
Returns the value of attribute query_execution_count.
12 13 14 |
# File 'lib/querier.rb', line 12 def query_execution_count @query_execution_count end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
12 13 14 |
# File 'lib/querier.rb', line 12 def query_params @query_params end |
#query_template ⇒ Object (readonly)
Returns the value of attribute query_template.
12 13 14 |
# File 'lib/querier.rb', line 12 def query_template @query_template end |
Instance Method Details
#cached_result(format: :hash) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/querier.rb', line 26 def cached_result(format: :hash) raise 'query not executed yet' if @query_execution_count.eql?(0) case format when :hash @execution_cached_result when :open_struct hash_to_open_struct(dataset: @execution_cached_result) else raise 'invalid value type' end end |
#execute ⇒ Object
20 21 22 23 24 |
# File 'lib/querier.rb', line 20 def execute @query_execution_count += 1 @execution_cached_result = @active_record_class.connection.select_all(fill_query_params(query_template: @query_template, query_params: @query_params)).map(&:symbolize_keys!) end |
#field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/querier.rb', line 52 def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true) count_result = cached_result(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] } unless sort_element_index.nil? count_result = count_result.sort_by { |el| el[sort_element_index] } count_result.reverse! if reverse_sort.eql? true end count_result end |
#structured_results ⇒ Object
39 40 41 |
# File 'lib/querier.rb', line 39 def structured_results hash_to_open_struct(dataset: execute) end |
#to_file ⇒ Object
47 48 49 50 |
# File 'lib/querier.rb', line 47 def to_file file_name = "querier #{Time.now.strftime '[%d-%m-%Y]-[%Hh %Mm %Ss]'}.sql" File.write "tmp/#{file_name}", to_sql end |
#to_sql ⇒ Object
43 44 45 |
# File 'lib/querier.rb', line 43 def to_sql fill_query_params(query_template: @query_template, query_params: @query_params) end |