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.



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

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.



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

def compare_fields
  @compare_fields
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Class Method Details

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



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

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

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



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

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

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

Instance Method Details

#eachObject

Raises:

  • (NotImplementedError)


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

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

#format_nameObject



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

def format_name
  self.class.format_name
end