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

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



37
38
39
# File 'lib/ruport/formatter/csv.rb', line 37

def initialize
  require "fastercsv" unless RUBY_VERSION > "1.9"   
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 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.



57
58
59
60
# File 'lib/ruport/formatter/csv.rb', line 57

def csv_writer
  @csv_writer ||= options.formatter ||
    FCSV(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.



45
46
47
48
49
50
# 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

#build_group_bodyObject

Renders the group body - uses the table controller to generate the output.



93
94
95
# File 'lib/ruport/formatter/csv.rb', line 93

def build_group_body
  render_table data, options.to_hash
end

#build_group_headerObject

Renders the header for a group using the group name.



87
88
89
# File 'lib/ruport/formatter/csv.rb', line 87

def build_group_header
  csv_writer << [data.name.to_s] << []
end

#build_grouping_bodyObject

Determines the proper style to use and renders the Grouping.



107
108
109
110
111
112
113
114
115
116
# 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

#build_grouping_headerObject

Generates a header for the grouping using the grouped_by column and the column names.



100
101
102
103
104
# 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

#build_row(data = self.data) ⇒ Object

Produces CSV output for a data row.



81
82
83
# File 'lib/ruport/formatter/csv.rb', line 81

def build_row(data = self.data)
  csv_writer << data
end

#build_table_bodyObject

Calls the row controller for each row in the Data::Table



75
76
77
78
# File 'lib/ruport/formatter/csv.rb', line 75

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.



67
68
69
70
71
72
# 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