class QueryResultPresenter::Presenter

Attributes

result[R]

Public Class Methods

new(result) click to toggle source
# File lib/query_result_presenter.rb, line 16
def initialize(result)
  @result = result
end

Public Instance Methods

field_values(column_name) click to toggle source

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
first_result_row() click to toggle source

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
has_results?() click to toggle source

Does this query have any results? @return [Boolean]

# File lib/query_result_presenter.rb, line 66
def has_results?
  result_count > 0
end
headers() click to toggle source

Returns the column names from the query result @return [Array]

# File lib/query_result_presenter.rb, line 47
def headers
  @result.first.keys
end
method_missing(method, *args, &block) click to toggle source

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
respond_to?(method, include_private = false) click to toggle source

Will be true if either this class or the underlying object responds to a method, false otherwise @return [Boolean]

Calls superclass method
# File lib/query_result_presenter.rb, line 84
def respond_to?(method, include_private = false)
  super || @result.respond_to?(method, include_private)
end
result_count() click to toggle source

How many rows are in the query result @return [Integer]

# File lib/query_result_presenter.rb, line 72
def result_count
  @result.count
end
to_csv_data() click to toggle source

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
to_csv_file(filename) click to toggle source

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
to_html_table() click to toggle source

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

add_html_row(row_data) click to toggle source

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
csv_data() click to toggle source

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