Class: Chronicle::ETL::CSVLoader

Inherits:
Loader
  • Object
show all
Includes:
Loaders::Helpers::StdoutHelper
Defined in:
lib/chronicle/etl/loaders/csv_loader.rb

Instance Attribute Summary

Attributes included from Registry::SelfRegistering

#connector_registration

Instance Method Summary collapse

Methods included from Loaders::Helpers::StdoutHelper

#create_stdout_temp_file, #output_to_stdout?, #write_to_stdout, #write_to_stdout_from_temp_file

Methods inherited from Loader

#initialize, #start

Methods included from Registry::SelfRegistering

#register_connector

Methods included from Loaders::Helpers::EncodingHelper

#force_utf8

Methods included from Configurable

included

Constructor Details

This class inherits a constructor from Chronicle::ETL::Loader

Instance Method Details

#finishObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chronicle/etl/loaders/csv_loader.rb', line 28

def finish
  return unless records.any?

  # headers = filtered_headers(records)
  headers = gather_headers(records)

  csv_options = {}
  if @config.headers
    csv_options[:write_headers] = @config.header_row
    csv_options[:headers] = headers
  end

  csv_output = CSV.generate(**csv_options) do |csv|
    records.each do |record|
      csv << Chronicle::Utils::HashUtils.flatten_hash(record.to_h)
        .values_at(*headers)
        .map { |value| force_utf8(value) }
    end
  end

  # TODO: just write to io directly
  if output_to_stdout?
    write_to_stdout(csv_output)
  else
    File.write(@config.output, csv_output)
  end
end

#load(record) ⇒ Object



24
25
26
# File 'lib/chronicle/etl/loaders/csv_loader.rb', line 24

def load(record)
  records << record
end

#recordsObject



20
21
22
# File 'lib/chronicle/etl/loaders/csv_loader.rb', line 20

def records
  @records ||= []
end