class Ruport::Formatter::CSV
This formatter implements the CSV format for Ruport's Row, Table, Group and Grouping controllers. It is a light wrapper around James Edward Gray II's FasterCSV.
Rendering Options¶ ↑
:style
Used for grouping (:inline,:justified,:raw)
:format_options
A hash of FasterCSV options
:formatter
An existing FasterCSV object to write to
:show_table_headers
True by default
:show_group_headers
True by default
Attributes
Public Class Methods
# File lib/ruport/formatter/csv.rb, line 37 def initialize require "fastercsv" unless RUBY_VERSION > "1.9" end
Public Instance Methods
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 45 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 93 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 87 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 107 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 100 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 81 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 75 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 67 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 FCSV 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 57 def csv_writer @csv_writer ||= options.formatter || FCSV(output, options.format_options || {}) end
Private Instance Methods
# File lib/ruport/formatter/csv.rb, line 141 def apply_grouping_format_template(t) t = (t || {}).merge(options.grouping_format || {}) options.style ||= t[:style] options.show_group_headers = t[:show_headings] if options.show_group_headers.nil? end
# File lib/ruport/formatter/csv.rb, line 135 def apply_table_format_template(t) t = (t || {}).merge(options.table_format || {}) options.show_table_headers = t[:show_headings] if options.show_table_headers.nil? end
# File lib/ruport/formatter/csv.rb, line 120 def grouping_columns data.data.to_a[0][1].column_names end
# File lib/ruport/formatter/csv.rb, line 124 def render_justified_or_raw_grouping data.each do |_,group| prefix = [group.name.to_s] group.each do |row| csv_writer << prefix + row.to_a prefix = [nil] if options.style == :justified end csv_writer << [] end end