Class: QueryResultPresenter::Presenter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Presenter

Returns a new instance of Presenter.



16
17
18
# File 'lib/query_result_presenter.rb', line 16

def initialize(result)
  @result = result
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Will delegate to the underlying object if a method can’t be found



77
78
79
# File 'lib/query_result_presenter.rb', line 77

def method_missing(method, *args, &block)
  @result.send(method, *args, &block)
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



14
15
16
# File 'lib/query_result_presenter.rb', line 14

def result
  @result
end

Instance Method Details

#field_values(column_name) ⇒ Array

Gets the values in every row for a specific column

Parameters:

  • column_name (String)

Returns:

  • (Array)


54
55
56
# File 'lib/query_result_presenter.rb', line 54

def field_values(column_name)
  @result.to_a.map{ |r| r[column_name] }
end

#first_result_rowArray

Gets the first row from the query result

Returns:

  • (Array)


60
61
62
# File 'lib/query_result_presenter.rb', line 60

def first_result_row
  @result.first.values
end

#has_results?Boolean

Does this query have any results?

Returns:

  • (Boolean)


66
67
68
# File 'lib/query_result_presenter.rb', line 66

def has_results?
  result_count > 0
end

#headersArray

Returns the column names from the query result

Returns:

  • (Array)


47
48
49
# File 'lib/query_result_presenter.rb', line 47

def headers
  @result.first.keys
end

#respond_to?(method, include_private = false) ⇒ Boolean

Will be true if either this class or the underlying object responds to a method, false otherwise

Returns:

  • (Boolean)


84
85
86
# File 'lib/query_result_presenter.rb', line 84

def respond_to?(method, include_private = false)
  super || @result.respond_to?(method, include_private)
end

#result_countInteger

How many rows are in the query result

Returns:

  • (Integer)


72
73
74
# File 'lib/query_result_presenter.rb', line 72

def result_count
  @result.count
end

#to_csv_dataString

Get the csv data in the form of a string

Returns:

  • (String)


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

def to_csv_data
  has_results? ? csv_data : ""
end

#to_csv_file(filename) ⇒ Object

Saves a query result to file. Will overwrite any file with the same name

Parameters:

  • filename (String)

    the name of the file eg ‘file.csv’



29
30
31
# 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_tableString

Returns the query result as an html table string

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
# 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