Sha256: 9d63b267fee142f6bd7f072fa863c72d35dfd0432ec907977b61b29e09506e67
Contents?: true
Size: 1 KB
Versions: 11
Compression:
Stored size: 1 KB
Contents
# frozen_string_literal: true require 'csv' module Zizia ## # A parser for CSV files. A single `InputRecord` is returned for each row # parsed from the input. # # Validates the format of the CSV, generating a single error the file is # malformed. This error gives the line number and a message for the first # badly formatted row. # # @see CsvFormatValidator class CsvParser < Parser DEFAULT_VALIDATORS = [CsvFormatValidator.new].freeze EXTENSION = '.csv' class << self ## # Matches all '.csv' filenames. def match?(file:, **_opts) File.extname(file) == EXTENSION rescue TypeError false end end ## # Gives a record for each line in the .csv # # @see Parser#records def records return enum_for(:records) unless block_given? file.rewind CSV.parse(file.read, headers: true).each do |row| yield InputRecord.from(metadata: row) end rescue CSV::MalformedCSVError [] end end end
Version data entries
11 entries across 11 versions & 1 rubygems