|
|
|
|
@ -11,7 +11,11 @@ class BulkUpload::Lettings::Year2022::CsvParser
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def row_offset |
|
|
|
|
with_headers? ? 5 : 0 |
|
|
|
|
if with_headers? |
|
|
|
|
rows.find_index { |row| row[0].match(/field number/i) } + 1 |
|
|
|
|
else |
|
|
|
|
0 |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def col_offset |
|
|
|
|
@ -25,7 +29,7 @@ class BulkUpload::Lettings::Year2022::CsvParser
|
|
|
|
|
def row_parsers |
|
|
|
|
@row_parsers ||= body_rows.map do |row| |
|
|
|
|
stripped_row = row[col_offset..] |
|
|
|
|
hash = Hash[headers.zip(stripped_row)] |
|
|
|
|
hash = Hash[field_numbers.zip(stripped_row)] |
|
|
|
|
|
|
|
|
|
BulkUpload::Lettings::Year2022::RowParser.new(hash) |
|
|
|
|
end |
|
|
|
|
@ -40,21 +44,29 @@ class BulkUpload::Lettings::Year2022::CsvParser
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def column_for_field(field) |
|
|
|
|
cols[headers.find_index(field) + col_offset] |
|
|
|
|
cols[field_numbers.find_index(field) + col_offset] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def valid_field_numbers_count |
|
|
|
|
FIELDS |
|
|
|
|
field_numbers.count { |f| f != "field_blank" } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def with_headers? |
|
|
|
|
rows[0][0]&.match?(/\D+/) |
|
|
|
|
rows.map { |r| r[0] }.any? { |cell| cell&.match?(/field number/i) } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
def headers |
|
|
|
|
@headers ||= ("field_1".."field_#{FIELDS}").to_a |
|
|
|
|
def default_field_numbers |
|
|
|
|
("field_1".."field_#{FIELDS}").to_a |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def field_numbers |
|
|
|
|
@field_numbers ||= if with_headers? |
|
|
|
|
rows[row_offset - 1][col_offset..].map { |h| h.present? && h.match?(/^[0-9]+$/) ? "field_#{h}" : "field_blank" } |
|
|
|
|
else |
|
|
|
|
default_field_numbers |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def row_sep |
|
|
|
|
|