Class: ArcFurnace::AllFieldsCSVSink
- Defined in:
- lib/arc-furnace/all_fields_csv_sink.rb
Instance Method Summary collapse
- #finalize ⇒ Object
-
#initialize(filename:, encoding: 'UTF-8', force_quotes: false) ⇒ AllFieldsCSVSink
constructor
A new instance of AllFieldsCSVSink.
- #row(hash) ⇒ Object
Methods inherited from Sink
Constructor Details
#initialize(filename:, encoding: 'UTF-8', force_quotes: false) ⇒ 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', force_quotes: false) @tmp_file = Tempfile.new('intermediate_results', encoding: 'binary') @packer = MessagePack::Packer.new(tmp_file) @csv = CSV.open(filename, 'wb', encoding: encoding, headers: true, force_quotes: force_quotes) @fields = {} end |
Instance Method Details
#finalize ⇒ Object
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 33 |
# File 'lib/arc-furnace/all_fields_csv_sink.rb', line 29 def row(hash) return unless hash.present? update_field_counts(hash) packer.write(hash) end |