Class: FlatKit::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flat_kit/reader.rb

Overview

Public: the base class for all format readers.

A format reader needs to be able to open the appropriate file format and implement Enumerable to iterate over all the records in the file format.

If it is appropriate for the reader to be able to read from a IO object directly, that needs to be supported also.

The ::FlatKit::Reader class should never be used directly, only the reader from the appropriate format should be used.

API:

initialize(source:, compare_fields:)
each -> Yields / returns

Direct Known Subclasses

Jsonl::Reader, Xsv::Reader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Reader.



40
41
42
43
# File 'lib/flat_kit/reader.rb', line 40

def initialize(source:, compare_fields: :none)
  @source = source
  @compare_fields = resolve_compare_fields(compare_fields)
end

Instance Attribute Details

#compare_fieldsObject (readonly)

Returns the value of attribute compare_fields.



24
25
26
# File 'lib/flat_kit/reader.rb', line 24

def compare_fields
  @compare_fields
end

#sourceObject (readonly)

Returns the value of attribute source.



24
25
26
# File 'lib/flat_kit/reader.rb', line 24

def source
  @source
end

Class Method Details

.create_reader_from_path(path: "-", fallback: "auto", compare_fields: :none) ⇒ Object



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

def self.create_reader_from_path(path: "-", fallback: "auto", compare_fields: :none)
  format = ::FlatKit::Format.for_with_fallback!(path: path, fallback: fallback)
  format.reader.new(source: path, compare_fields: compare_fields)
end

.create_readers_from_paths(paths:, fallback: "auto", compare_fields: :none) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/flat_kit/reader.rb', line 31

def self.create_readers_from_paths(paths:, fallback: "auto", compare_fields: :none)
  # default to stdin if there are no paths
  paths << "-" if paths.empty?

  paths.map do |path|
    create_reader_from_path(path: path, fallback: fallback, compare_fields: compare_fields)
  end
end

Instance Method Details

#eachObject

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/flat_kit/reader.rb', line 49

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

#format_nameObject



45
46
47
# File 'lib/flat_kit/reader.rb', line 45

def format_name
  self.class.format_name
end