Class: Querier
- Inherits:
-
Object
- Object
- Querier
- Defined in:
- lib/querier.rb
Constant Summary collapse
- PARAM_NAME_INDEX =
0- PARAM_VALUE_INDEX =
1
Instance Attribute Summary collapse
-
#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
- #execute ⇒ 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.
7 8 9 |
# File 'lib/querier.rb', line 7 def initialize **template_query_params @query_params = template_query_params.dup end |
Instance Attribute Details
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
5 6 7 |
# File 'lib/querier.rb', line 5 def query_params @query_params end |
#query_template ⇒ Object (readonly)
Returns the value of attribute query_template.
5 6 7 |
# File 'lib/querier.rb', line 5 def query_template @query_template end |
Instance Method Details
#execute ⇒ Object
11 12 13 |
# File 'lib/querier.rb', line 11 def execute ActiveRecord::Base.connection.select_all fill_query_params(query_template: @query_template, query_params: @query_params) end |
#structured_results ⇒ Object
15 16 17 18 19 20 |
# File 'lib/querier.rb', line 15 def structured_results query_results = self.execute structured_results = [] query_results.each {|query_result| structured_results << OpenStruct.new(query_result.symbolize_keys!)} structured_results end |
#to_file ⇒ Object
26 27 28 29 |
# File 'lib/querier.rb', line 26 def to_file file_name = "querier #{Time.now.strftime "[%d-%m-%Y]-[%Hh %Mm %Ss]"}.sql" File.open("tmp/#{file_name}", 'w') {|f| f << self.to_sql} end |
#to_sql ⇒ Object
22 23 24 |
# File 'lib/querier.rb', line 22 def to_sql fill_query_params(query_template: @query_template, query_params: @query_params) end |