Class: Portable::Writers::Csv

Inherits:
Base
  • Object
show all
Defined in:
lib/portable/writers/csv.rb

Overview

Can write documents to a CSV file.

Instance Attribute Summary

Attributes inherited from Base

#document, #sheet_renderer

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Portable::Writers::Base

Instance Method Details

#write!(filename:, data_provider: Data::Provider.new, time: Time.now.utc) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/portable/writers/csv.rb', line 17

def write!(filename:, data_provider: Data::Provider.new, time: Time.now.utc)
  raise ArgumentError, 'filename is required' if filename.to_s.empty?

  ensure_directory_exists(filename)

  sheet_filenames = extrapolate_filenames(filename, document.sheets.length)

  document.sheets.map.with_index do |sheet, index|
    data_source    = data_provider.data_source(sheet.data_source_name)
    sheet_filename = sheet_filenames[index]

    time_in_seconds = Benchmark.measure do
      write_sheet(sheet_filename, sheet, data_source, time)
    end.real

    Result.new(sheet_filename, time_in_seconds)
  end
end