Class: Tabledata::Presenters::CSV

Inherits:
Tabledata::Presenter show all
Defined in:
lib/tabledata/presenters/csv.rb

Constant Summary collapse

OptionMapping =
{
  column_separator: :col_sep,
  row_separator:    :row_sep,
  quote_char:       :quote_character,
}

Instance Attribute Summary

Attributes inherited from Tabledata::Presenter

#options, #table, #tables

Instance Method Summary collapse

Methods inherited from Tabledata::Presenter

add_presenter!, #initialize, #multiple_tables?, present, presenter_exists?, replace_presenter!, #single_table?

Constructor Details

This class inherits a constructor from Tabledata::Presenter

Instance Method Details

#csv_optionsObject


15
16
17
18
19
20
# File 'lib/tabledata/presenters/csv.rb', line 15

def csv_options
  options = ::CSV::DEFAULT_OPTIONS.dup
  @options.each do |k,v| options[OptionMapping.fetch(k,k)] = v end

  options
end

#string(options = nil) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/tabledata/presenters/csv.rb', line 22

def string(options=nil)
  ::CSV.generate(csv_options) do |csv|
    @table.each_row do |row|
      csv << row.to_a
    end
  end
end

#write(path, options = nil) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/tabledata/presenters/csv.rb', line 30

def write(path, options=nil)
  ::CSV.open(path, 'wb', csv_options) do |csv|
    @table.each_row do |row|
      csv << row.to_a
    end
  end
end