class QueryResultPresenter::Presenter
Attributes
Public Class Methods
# File lib/query_result_presenter.rb, line 16 def initialize(result) @result = result end
Public Instance Methods
Gets the values in every row for a specific column @param [String] column_name @return [Array]
# File lib/query_result_presenter.rb, line 54 def field_values(column_name) @result.to_a.map{ |r| r[column_name] } end
Gets the first row from the query result @return [Array]
# File lib/query_result_presenter.rb, line 60 def first_result_row @result.first.values end
Does this query have any results? @return [Boolean]
# File lib/query_result_presenter.rb, line 66 def has_results? result_count > 0 end
Returns the column names from the query result @return [Array]
# File lib/query_result_presenter.rb, line 47 def headers @result.first.keys end
Will delegate to the underlying object if a method can’t be found
# File lib/query_result_presenter.rb, line 77 def method_missing(method, *args, &block) @result.send(method, *args, &block) end
Will be true if either this class or the underlying object responds to a method, false otherwise @return [Boolean]
# File lib/query_result_presenter.rb, line 84 def respond_to?(method, include_private = false) super || @result.respond_to?(method, include_private) end
How many rows are in the query result @return [Integer]
# File lib/query_result_presenter.rb, line 72 def result_count @result.count end
Get the csv data in the form of a string @return [String]
# File lib/query_result_presenter.rb, line 22 def to_csv_data has_results? ? csv_data : "" end
Saves a query result to file. Will overwrite any file with the same name @param [String] filename the name of the file eg ‘file.csv’
# File lib/query_result_presenter.rb, line 29 def to_csv_file(filename) File.open(filename, "w"){ |f| f.puts csv_data } end
Returns the query result as an html table string @return [String]
# File lib/query_result_presenter.rb, line 35 def to_html_table html_table = "<table>" html_table << add_html_row(headers) @result.each do |row| html_table << add_html_row(row.values) end html_table << "</table>" html_table end
Private Instance Methods
Returns an array as an html row @param [Array] row_data the array you want to turn into a row @return [String]
# File lib/query_result_presenter.rb, line 93 def add_html_row(row_data) row = "<tr>" row_data.each do |d| row << "<td>#{d}</td>" end row << "</tr>" row end
Get the csv data in the form of a string @return [String]
# File lib/query_result_presenter.rb, line 104 def csv_data csv_data = CSV.generate do |csv| csv << headers @result.each do |r| csv << r.values end end csv_data end