Sha256: 479cceded2b67b461977269b891a094dc9f5fa5b35ae0e93862a87a205b31c53
Contents?: true
Size: 1.3 KB
Versions: 5
Compression:
Stored size: 1.3 KB
Contents
require 'csv' module Chronicle module ETL class CSVLoader < Chronicle::ETL::Loader register_connector do |r| r.description = 'CSV' end setting :output, default: $stdout setting :headers, default: true setting :header_row, default: true def records @records ||= [] end def load(record) records << record.to_h_flattened end def finish return unless records.any? headers = build_headers(records) csv_options = {} if @config.headers csv_options[:write_headers] = @config.header_row csv_options[:headers] = headers end if @config.output.is_a?(IO) # This might seem like a duplication of the default value ($stdout) # but it's because rspec overwrites $stdout (in helper #capture) to # capture output. io = $stdout.dup else io = File.open(@config.output, "w+") end output = CSV.generate(**csv_options) do |csv| records.each do |record| csv << record .transform_keys(&:to_sym) .values_at(*headers) .map { |value| force_utf8(value) } end end io.write(output) io.close end end end end
Version data entries
5 entries across 5 versions & 1 rubygems