Class: CSVPlusPlus::Writer::CSV

Inherits:
Writer
  • Object
show all
Extended by:
T::Sig
Includes:
FileBackerUpper, Merger
Defined in:
lib/csv_plus_plus/writer/csv.rb

Overview

A class that can output a Template to CSV

Instance Attribute Summary

Attributes inherited from Writer

#position

Instance Method Summary collapse

Methods included from Merger

#merge_cell_value

Methods included from FileBackerUpper

#backup_file

Constructor Details

#initialize(options, position) ⇒ CSV

Returns a new instance of CSV.

Parameters:



17
18
19
20
21
22
# File 'lib/csv_plus_plus/writer/csv.rb', line 17

def initialize(options, position)
  super(position)

  @reader = ::T.let(::CSVPlusPlus::Reader::CSV.new(options), ::CSVPlusPlus::Reader::CSV)
  @options = options
end

Instance Method Details

#evaluate_cell(cell) ⇒ Object

Turn the cell into a CSV-



45
46
47
48
49
50
51
# File 'lib/csv_plus_plus/writer/csv.rb', line 45

def evaluate_cell(cell)
  if (ast = cell.ast)
    "=#{ast.evaluate(@position)}"
  else
    cell.value
  end
end

#write(template) ⇒ Object

Write a template to CSV

Parameters:

  • template (Template)

    The template to use as input to be written. It should have been compiled by calling Compiler#compile_template



29
30
31
32
33
34
35
# File 'lib/csv_plus_plus/writer/csv.rb', line 29

def write(template)
  ::CSV.open(@options.output_filename, 'wb') do |csv|
    @position.map_rows(template.rows) do |row|
      csv << build_row(row)
    end
  end
end

#write_backupObject

Write a backup of the current spreadsheet.



39
40
41
# File 'lib/csv_plus_plus/writer/csv.rb', line 39

def write_backup
  backup_file(@options)
end