Class: FlatKit::Xsv::Writer
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Attributes inherited from Writer
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(destination:, fields: :auto, **csv_options) ⇒ Writer
constructor
A new instance of Writer.
- #write(record) ⇒ Object
Methods inherited from Writer
create_writer_from_path, #format_name
Constructor Details
#initialize(destination:, fields: :auto, **csv_options) ⇒ Writer
Returns a new instance of Writer.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/flat_kit/xsv/writer.rb', line 19 def initialize(destination:, fields: :auto, **) super(destination: destination) @fields = fields @output = ::FlatKit::Output.from(@destination) @count = 0 @we_write_the_header = nil = Writer..dup if @fields == :auto then @we_write_the_header = true else .merge!(headers: fields) @we_write_the_header = false end .merge!() @csv = CSV.new(output.io, **) end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
5 6 7 |
# File 'lib/flat_kit/xsv/writer.rb', line 5 def count @count end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/flat_kit/xsv/writer.rb', line 6 def fields @fields end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/flat_kit/xsv/writer.rb', line 4 def output @output end |
Class Method Details
.default_csv_options ⇒ Object
12 13 14 15 16 17 |
# File 'lib/flat_kit/xsv/writer.rb', line 12 def self. { headers: nil, write_headers: true } end |
.format_name ⇒ Object
8 9 10 |
# File 'lib/flat_kit/xsv/writer.rb', line 8 def self.format_name ::FlatKit::Xsv::Format.format_name end |
Instance Method Details
#close ⇒ Object
55 56 57 |
# File 'lib/flat_kit/xsv/writer.rb', line 55 def close @output.close end |
#write(record) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/flat_kit/xsv/writer.rb', line 38 def write(record) case record when FlatKit::Xsv::Record write_record(record) when FlatKit::Record converted_record = ::FlatKit::Xsv::Record.from_record(record, ordered_fields: @fields) write_record(converted_record) else raise FlatKit::Error, "Unable to write records of type #{record.class}" end rescue FlatKit::Error => fe raise fe rescue => e ::FlatKit.logger.error "Error reading jsonl records from #{output.name}: #{e}" raise ::FlatKit::Error, e end |