Class: IOStreams::Record::Reader

Inherits:
IOStreams::Reader show all
Includes:
Enumerable
Defined in:
lib/io_streams/record/reader.rb

Overview

Converts each line of an input stream into hash for every row

Instance Attribute Summary

Attributes inherited from IOStreams::Reader

#input_stream

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IOStreams::Reader

open

Constructor Details

#initialize(line_reader, cleanse_header: true, original_file_name: nil, **args) ⇒ Reader

Create a Tabular reader to return the stream as Hash records Parse a delimited data source.

Parameters

format: [Symbol]
  :csv, :hash, :array, :json, :psv, :fixed

file_name: [String]
  When `:format` is not supplied the file name can be used to infer the required format.
  Optional. Default: nil

format_options: [Hash]
  Any specialized format specific options. For example, `:fixed` format requires the file definition.

columns [Array<String>]
  The header columns when the file does not include a header row.
  Note:
    It is recommended to keep all columns as strings to avoid any issues when persistence
    with MongoDB when it converts symbol keys to strings.

allowed_columns [Array<String>]
  List of columns to allow.
  Default: nil ( Allow all columns )
  Note:
    When supplied any columns that are rejected will be returned in the cleansed columns
    as nil so that they can be ignored during processing.

required_columns [Array<String>]
  List of columns that must be present, otherwise an Exception is raised.

skip_unknown [true|false]
  true:
    Skip columns not present in the `allowed_columns` by cleansing them to nil.
    #as_hash will skip these additional columns entirely as if they were not in the file at all.
  false:
    Raises Tabular::InvalidHeader when a column is supplied that is not in the whitelist.


60
61
62
63
64
65
66
67
68
# File 'lib/io_streams/record/reader.rb', line 60

def initialize(line_reader, cleanse_header: true, original_file_name: nil, **args)
  unless line_reader.respond_to?(:each)
    raise(ArgumentError, "Stream must be a IOStreams::Line::Reader or implement #each")
  end

  @tabular        = IOStreams::Tabular.new(file_name: original_file_name, **args)
  @line_reader    = line_reader
  @cleanse_header = cleanse_header
end

Class Method Details

.file(file_name, original_file_name: file_name, delimiter: $/, **args) ⇒ Object

When reading from a file also add the line reader stream



18
19
20
21
22
# File 'lib/io_streams/record/reader.rb', line 18

def self.file(file_name, original_file_name: file_name, delimiter: $/, **args)
  IOStreams::Line::Reader.file(file_name, original_file_name: original_file_name, delimiter: delimiter) do |io|
    yield new(io, original_file_name: original_file_name, **args)
  end
end

.stream(line_reader, **args) {|new(line_reader, **args)| ... } ⇒ Object

Read a record at a time from a line stream Note:

  • The supplied stream must already be a line stream, or a stream that responds to :each

Yields:

  • (new(line_reader, **args))


10
11
12
13
14
15
# File 'lib/io_streams/record/reader.rb', line 10

def self.stream(line_reader, **args)
  # Pass-through if already a record reader
  return yield(line_reader) if line_reader.is_a?(self.class)

  yield new(line_reader, **args)
end

Instance Method Details

#eachObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/io_streams/record/reader.rb', line 70

def each
  @line_reader.each do |line|
    if @tabular.header?
      @tabular.parse_header(line)
      @tabular.cleanse_header! if @cleanse_header
    else
      yield @tabular.record_parse(line)
    end
  end
end