Class: FlatKit::Jsonl::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/flat_kit/jsonl/reader.rb

Overview

Internal: Reader class that parses and yields records from JSONL files

Instance Attribute Summary collapse

Attributes inherited from Reader

#compare_fields, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reader

create_reader_from_path, create_readers_from_paths, #format_name

Constructor Details

#initialize(source:, compare_fields: :none) ⇒ Reader

Returns a new instance of Reader.



14
15
16
17
18
# File 'lib/flat_kit/jsonl/reader.rb', line 14

def initialize(source:, compare_fields: :none)
  super
  @input = ::FlatKit::Input.from(source)
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/flat_kit/jsonl/reader.rb', line 8

def count
  @count
end

#inputObject (readonly)

Returns the value of attribute input.



8
9
10
# File 'lib/flat_kit/jsonl/reader.rb', line 8

def input
  @input
end

Class Method Details

.format_nameObject



10
11
12
# File 'lib/flat_kit/jsonl/reader.rb', line 10

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

Instance Method Details

#eachObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flat_kit/jsonl/reader.rb', line 20

def each
  while (line = input.io.gets)
    record = ::FlatKit::Jsonl::Record.new(data: line, compare_fields: compare_fields)
    @count += 1
    yield record
  end
  input.close
rescue StandardError => e
  ::FlatKit.logger.error "Error reading jsonl records from #{input.name}: #{e}"
  raise ::FlatKit::Error, e
end