Class: FlatKit::Writer

Inherits:
Object
  • Object
show all
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

Jsonl::Writer, Xsv::Writer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination:) ⇒ Writer

Returns a new instance of Writer.



22
23
24
# File 'lib/flat_kit/writer.rb', line 22

def initialize(destination:)
  @destination = destination
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



14
15
16
# File 'lib/flat_kit/writer.rb', line 14

def destination
  @destination
end

Class Method Details

.create_writer_from_path(path:, fallback:, reader_format:) ⇒ Object



16
17
18
19
20
# File 'lib/flat_kit/writer.rb', line 16

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

#closeObject

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/flat_kit/writer.rb', line 34

def close
  raise NotImplementedError, "#{self.class} needs to implement #close"
end

#format_nameObject



26
27
28
# File 'lib/flat_kit/writer.rb', line 26

def format_name
  self.class.format_name
end

#write(record) ⇒ Object

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/flat_kit/writer.rb', line 30

def write(record)
  raise NotImplementedError, "#{self.class} needs to implement #write"
end