Class: FlatKit::Jsonl::Writer

Inherits:
Writer
  • Object
show all
Defined in:
lib/flat_kit/jsonl/writer.rb

Instance Attribute Summary collapse

Attributes inherited from Writer

#destination

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Writer

create_writer_from_path, #format_name

Constructor Details

#initialize(destination:) ⇒ Writer

Returns a new instance of Writer.



11
12
13
14
15
# File 'lib/flat_kit/jsonl/writer.rb', line 11

def initialize(destination:)
  super
  @output = ::FlatKit::Output.from(@destination)
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



5
6
7
# File 'lib/flat_kit/jsonl/writer.rb', line 5

def count
  @count
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/flat_kit/jsonl/writer.rb', line 4

def output
  @output
end

Class Method Details

.format_nameObject



7
8
9
# File 'lib/flat_kit/jsonl/writer.rb', line 7

def self.format_name
  ::FlatKit::Jsonl::Format.format_name
end

Instance Method Details

#closeObject



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

def close
  @output.close
end

#write(record) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flat_kit/jsonl/writer.rb', line 17

def write(record)
  case record
  when FlatKit::Jsonl::Record
    write_record(record)
  when FlatKit::Record
    converted_record = ::FlatKit::Jsonl::Record.from_record(record)
    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

#write_record(record) ⇒ Object



38
39
40
41
42
# File 'lib/flat_kit/jsonl/writer.rb', line 38

def write_record(record)
  # enforces ending in newlin if it doesn't already have one
  output.io.puts record.to_s
  @count += 1
end