This formatter implements the CSV format for Ruport’s Row, Table, Group and Grouping controllers.
: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
Hook for setting available options using a template. See the template documentation for the available options and their format.
# File lib/ruport/formatter/csv.rb, line 44 def apply_template apply_table_format_template(template.table) apply_grouping_format_template(template.grouping) options.format_options ||= template.format_options end
Renders the group body - uses the table controller to generate the output.
# File lib/ruport/formatter/csv.rb, line 92 def build_group_body render_table data, options.to_hash end
Renders the header for a group using the group name.
# File lib/ruport/formatter/csv.rb, line 86 def build_group_header csv_writer << [data.name.to_s] << [] end
Determines the proper style to use and renders the Grouping.
# File lib/ruport/formatter/csv.rb, line 106 def build_grouping_body case options.style when :inline render_inline_grouping(options) when :justified, :raw render_justified_or_raw_grouping else raise NotImplementedError, "Unknown style" end end
Generates a header for the grouping using the grouped_by column and the column names.
# File lib/ruport/formatter/csv.rb, line 99 def build_grouping_header unless options.style == :inline csv_writer << [data.grouped_by] + grouping_columns end end
Produces CSV output for a data row.
# File lib/ruport/formatter/csv.rb, line 80 def build_row(data = self.data) csv_writer << data end
Calls the row controller for each row in the Data::Table
# File lib/ruport/formatter/csv.rb, line 74 def build_table_body fcsv = csv_writer data.each { |row| fcsv << row } end
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.
# File lib/ruport/formatter/csv.rb, line 66 def build_table_header unless data.column_names.empty? || !options.show_table_headers render_row data.column_names, :format_options => options.format_options, :formatter => csv_writer end end
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.
# File lib/ruport/formatter/csv.rb, line 56 def csv_writer @csv_writer ||= options.formatter || ::CSV.instance(output, options.format_options || {}) end
# File lib/ruport/formatter/csv.rb, line 36 def initialize require "csv" end