Class: Querier

Inherits:
Object
  • Object
show all
Defined in:
lib/querier.rb

Constant Summary collapse

PARAM_NAME_INDEX =
0
PARAM_VALUE_INDEX =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

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_paramsObject (readonly)

Returns the value of attribute query_params.



5
6
7
# File 'lib/querier.rb', line 5

def query_params
  @query_params
end

#query_templateObject (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

#executeObject



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_resultsObject



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_fileObject



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_sqlObject



22
23
24
# File 'lib/querier.rb', line 22

def to_sql
  fill_query_params(query_template: @query_template, query_params: @query_params)
end