Class: Ruport::Formatter::CSV
- Inherits:
-
Ruport::Formatter
- Object
- Ruport::Formatter
- Ruport::Formatter::CSV
- Defined in:
- lib/ruport/formatter/csv.rb
Overview
This formatter implements the CSV format for Ruport’s Row, Table, Group and Grouping controllers.
Rendering Options
:style
Used for grouping (:inline,:justified,:raw)
:format_options
A hash of CSV options
:formatter
An existing CSV object to write to
:show_table_headers
True by default
:show_group_headers
True by default
Instance Attribute Summary collapse
-
#csv_writer ⇒ Object
Returns the current CSV object or creates a new one if it has not been set yet.
Attributes inherited from Ruport::Formatter
Instance Method Summary collapse
-
#apply_template ⇒ Object
Hook for setting available options using a template.
-
#build_group_body ⇒ Object
Renders the group body - uses the table controller to generate the output.
-
#build_group_header ⇒ Object
Renders the header for a group using the group name.
-
#build_grouping_body ⇒ Object
Determines the proper style to use and renders the Grouping.
-
#build_grouping_header ⇒ Object
Generates a header for the grouping using the grouped_by column and the column names.
-
#build_row(data = self.data) ⇒ Object
Produces CSV output for a data row.
-
#build_table_body ⇒ Object
Calls the row controller for each row in the Data::Table.
-
#build_table_header ⇒ Object
Generates table header by turning column_names into a CSV row.
-
#initialize ⇒ CSV
constructor
A new instance of CSV.
Methods inherited from Ruport::Formatter
build, #clear_output, #erb, formats, #method_missing, #output, renders, save_as_binary_file, #save_output, #template
Methods included from RenderingTools
#render_group, #render_grouping, #render_inline_grouping, #render_row, #render_table
Constructor Details
#initialize ⇒ CSV
Returns a new instance of CSV.
36 37 38 |
# File 'lib/ruport/formatter/csv.rb', line 36 def initialize require "csv" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Ruport::Formatter
Instance Attribute Details
#csv_writer ⇒ Object
Returns the current CSV object or creates a new one if it has not been set yet. Note that FCSV(sig) has a cache and returns the same FCSV object if writing to the same underlying output with the same options.
56 57 58 59 |
# File 'lib/ruport/formatter/csv.rb', line 56 def csv_writer @csv_writer ||= .formatter || ::CSV.instance(output, **(. || {})) end |
Instance Method Details
#apply_template ⇒ Object
Hook for setting available options using a template. See the template documentation for the available options and their format.
44 45 46 47 48 49 |
# File 'lib/ruport/formatter/csv.rb', line 44 def apply_template apply_table_format_template(template.table) apply_grouping_format_template(template.grouping) . ||= template. end |
#build_group_body ⇒ Object
Renders the group body - uses the table controller to generate the output.
92 93 94 |
# File 'lib/ruport/formatter/csv.rb', line 92 def build_group_body render_table data, .to_hash end |
#build_group_header ⇒ Object
Renders the header for a group using the group name.
86 87 88 |
# File 'lib/ruport/formatter/csv.rb', line 86 def build_group_header csv_writer << [data.name.to_s] << [] end |
#build_grouping_body ⇒ Object
Determines the proper style to use and renders the Grouping.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ruport/formatter/csv.rb', line 106 def build_grouping_body case .style when :inline render_inline_grouping() when :justified, :raw render_justified_or_raw_grouping else raise NotImplementedError, "Unknown style" end end |
#build_grouping_header ⇒ Object
Generates a header for the grouping using the grouped_by column and the column names.
99 100 101 102 103 |
# File 'lib/ruport/formatter/csv.rb', line 99 def build_grouping_header unless .style == :inline csv_writer << [data.grouped_by] + grouping_columns end end |
#build_row(data = self.data) ⇒ Object
Produces CSV output for a data row.
80 81 82 |
# File 'lib/ruport/formatter/csv.rb', line 80 def build_row(data = self.data) csv_writer << data end |
#build_table_body ⇒ Object
Calls the row controller for each row in the Data::Table
74 75 76 77 |
# File 'lib/ruport/formatter/csv.rb', line 74 def build_table_body fcsv = csv_writer data.each { |row| fcsv << row } end |
#build_table_header ⇒ Object
Generates table header by turning column_names into a CSV row. Uses the row controller to generate the actual formatted output
This method does not do anything if options.show_table_headers is false or the Data::Table has no column names.
66 67 68 69 70 71 |
# File 'lib/ruport/formatter/csv.rb', line 66 def build_table_header unless data.column_names.empty? || !.show_table_headers render_row data.column_names, :format_options => ., :formatter => csv_writer end end |