Class: QueryResultPresenter::Presenter
- Inherits:
-
Object
- Object
- QueryResultPresenter::Presenter
- Defined in:
- lib/query_result_presenter.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#field_values(column_name) ⇒ Array
Gets the values in every row for a specific column.
-
#first_result_row ⇒ Array
Gets the first row from the query result.
-
#has_results? ⇒ Boolean
Does this query have any results?.
-
#headers ⇒ Array
Returns the column names from the query result.
-
#initialize(result) ⇒ Presenter
constructor
A new instance of Presenter.
-
#method_missing(method, *args, &block) ⇒ Object
Will delegate to the underlying object if a method can’t be found.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Will be true if either this class or the underlying object responds to a method, false otherwise.
-
#result_count ⇒ Integer
How many rows are in the query result.
-
#to_csv_data ⇒ String
Get the csv data in the form of a string.
-
#to_csv_file(filename) ⇒ Object
Saves a query result to file.
-
#to_html_table ⇒ String
Returns the query result as an html table string.
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
#result ⇒ Object (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
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_row ⇒ Array
Gets the first row from the query result
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?
66 67 68 |
# File 'lib/query_result_presenter.rb', line 66 def has_results? result_count > 0 end |
#headers ⇒ Array
Returns the column names from the query result
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
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_count ⇒ Integer
How many rows are in the query result
72 73 74 |
# File 'lib/query_result_presenter.rb', line 72 def result_count @result.count end |
#to_csv_data ⇒ String
Get the csv data in the form of a 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
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_table ⇒ String
Returns the query result as an html table 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 |