Class: ArcFurnace::AllFieldsCSVSink

Inherits:
Sink
  • Object
show all
Defined in:
lib/arc-furnace/all_fields_csv_sink.rb

Instance Method Summary collapse

Methods inherited from Sink

#prepare

Constructor Details

#initialize(filename:, encoding: 'UTF-8') ⇒ AllFieldsCSVSink

Returns a new instance of AllFieldsCSVSink.



8
9
10
11
12
13
# File 'lib/arc-furnace/all_fields_csv_sink.rb', line 8

def initialize(filename: , encoding: 'UTF-8')
  @tmp_file = Tempfile.new('intermediate_results', encoding: 'binary')
  @packer = MessagePack::Packer.new(tmp_file)
  @csv = CSV.open(filename, 'wb', encoding: encoding, headers: true)
  @fields = {}
end

Instance Method Details

#finalizeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/arc-furnace/all_fields_csv_sink.rb', line 15

def finalize
  packer.flush
  tmp_file.rewind

  write_header_row!

  unpacker = MessagePack::Unpacker.new(tmp_file)
  unpacker.each do |hash|
    write_row(hash)
  end

  csv.close
end

#row(hash) ⇒ Object



29
30
31
32
# File 'lib/arc-furnace/all_fields_csv_sink.rb', line 29

def row(hash)
  update_field_counts(hash)
  packer.write(hash)
end