class Reading::CSV::Load

Load is a function that parses CSV lines into item data.

Constants

VERSION

Public Class Methods

new(config_csv, config_item) click to toggle source
# File lib/reading/csv/load.rb, line 15
def initialize(config_csv, config_item)
  config_csv.to_attr_private(self)
  @config_csv       = config_csv
  @config_item       = config_item
  @cur_line          = nil
end

Public Instance Methods

call(feed = nil, close_feed: true, err_block: nil, &data_postprocess_block) click to toggle source

returns item data in the same order as they arrive from feed. if no block is given, the raw data is returned. if a block is given, the data is run through it, e.g. to create Items.

# File lib/reading/csv/load.rb, line 25
def call(feed = nil, close_feed: true, err_block: nil, &data_postprocess_block)
  feed ||= File.open(path)
  items = []
  feed.each_line do |line|
    @cur_line = line.strip
    next if header? || comment? || blank_line?
    items += ParseLine.new(config_csv, config_item)
                      .call(cur_line, &data_postprocess_block)
  rescue InvalidLineError, ValidationError => e
    err_block&.call(e)
    next
  end
  items
rescue Errno::ENOENT
  raise FileError.new(path, label: "File not found!")
rescue Errno::EISDIR
  raise FileError.new(path, label: "The library must be a file, not a directory!")
ensure
  feed&.close if close_feed && feed.respond_to?(:close)
  initialize(config_csv, config_item) # reset to pre-call state
end

Private Instance Methods

blank_line?() click to toggle source
# File lib/reading/csv/load.rb, line 57
def blank_line?
  cur_line.empty?
end
comment?() click to toggle source
# File lib/reading/csv/load.rb, line 53
def comment?
  cur_line.start_with?(comment_mark)
end
header?() click to toggle source
# File lib/reading/csv/load.rb, line 49
def header?
  cur_line.start_with?(header_first)
end