Class: Ruport::Formatter::CSV

Inherits:
Ruport::Formatter show all
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

Attributes inherited from Ruport::Formatter

#data, #format, #options

Instance Method Summary collapse

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

#initializeCSV

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_writerObject

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 ||= options.formatter ||
    ::CSV.instance(output, **(options.format_options || {}))
end

Instance Method Details

#apply_templateObject

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)

  options.format_options ||= template.format_options
end

#build_group_bodyObject

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, options.to_hash
end

#build_group_headerObject

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_bodyObject

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 options.style
  when :inline
    render_inline_grouping(options)
  when :justified, :raw
    render_justified_or_raw_grouping
  else
    raise NotImplementedError, "Unknown style"
  end
end

#build_grouping_headerObject

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 options.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_bodyObject

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_headerObject

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? || !options.show_table_headers
    render_row data.column_names, :format_options => options.format_options,
                                  :formatter => csv_writer
  end
end