Class: FlatKit::Writer
- Inherits:
-
Object
- Object
- FlatKit::Writer
- Defined in:
- lib/flat_kit/writer.rb
Overview
Public: The base class for all format writers.
A format writer will only write those Records, and on that, only those of its own format.
It must implement a #write methods takes a Record. It can convert the record to one matching its own format if it whishes. But it should in any case check the Record format to make sure it matches
See the Xsv::Writer and Jsonl::Writer for examples.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#last_position ⇒ Object
readonly
Returns the value of attribute last_position.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #current_position ⇒ Object
- #format_name ⇒ Object
-
#initialize(destination:) ⇒ Writer
constructor
A new instance of Writer.
-
#write(record) ⇒ Object
The write method MUST return a Position object detailing the location the record was written in the output stream.
Constructor Details
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
16 17 18 |
# File 'lib/flat_kit/writer.rb', line 16 def count @count end |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
16 17 18 |
# File 'lib/flat_kit/writer.rb', line 16 def destination @destination end |
#last_position ⇒ Object (readonly)
Returns the value of attribute last_position.
16 17 18 |
# File 'lib/flat_kit/writer.rb', line 16 def last_position @last_position end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
16 17 18 |
# File 'lib/flat_kit/writer.rb', line 16 def output @output end |
Class Method Details
.create_writer_from_path(path:, fallback:, reader_format:) ⇒ Object
18 19 20 21 22 |
# File 'lib/flat_kit/writer.rb', line 18 def self.create_writer_from_path(path:, fallback:, reader_format:) fallback = reader_format if fallback == "auto" format = ::FlatKit::Format.for_with_fallback!(path: path, fallback: fallback) format.writer.new(destination: path) end |
Instance Method Details
#close ⇒ Object
48 49 50 |
# File 'lib/flat_kit/writer.rb', line 48 def close output.close end |
#current_position ⇒ Object
35 36 37 38 39 |
# File 'lib/flat_kit/writer.rb', line 35 def current_position ::FlatKit::Position.new(index: @count, # since this hasn't been written yet its the right index offset: output.tell, bytesize: 0) # nothing has been written yet end |
#format_name ⇒ Object
31 32 33 |
# File 'lib/flat_kit/writer.rb', line 31 def format_name self.class.format_name end |
#write(record) ⇒ Object
The write method MUST return a Position object detailing the location the record was written in the output stream.
44 45 46 |
# File 'lib/flat_kit/writer.rb', line 44 def write(record) raise NotImplementedError, "#{self.class} needs to implement #write that returns Position" end |