This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ace-of-base/lib/ace_of_base/file_importer.rb
2019-07-10 21:03:07 +12:00

43 lines
861 B
Ruby

# frozen_string_literal: true
require 'csv'
module AceOfBase
# Import a pipe-delimited file and validate it.
class FileImporter
attr_reader :records, :errors
def self.import(file)
new(file).import
end
def initialize(file)
@errors = {}
@records = []
@file = file
end
def import
@file.each_line.with_index do |line, line_no|
# Skip the header record.
next if line_no.zero? && line =~ /\APROJECT|SHOT|VERSION|STATUS|FINISH_DATE|INTERNAL_BID|CREATED_DATE\Z/i
record = line.chomp.split('|')
imported_record = Record.new(record)
next @errors[line_no] = imported_record.errors unless imported_record.valid?
@records << imported_record
end
self
end
def valid?
errors.empty?
end
private
attr_reader :file
end
end