44 lines
861 B
Ruby
44 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
|