Skip to content

Commit

Permalink
Refactors and memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel committed Oct 1, 2021
1 parent 5e05c8f commit 021281f
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions app/uploaders/zizia/csv_manifest_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,15 @@ def validate
# One record per row
def record_count
return nil unless @parsed_csv.size

row_with_values = @parsed_csv.map { |row| row.to_hash.values.all?(&:nil?) }
row_with_values.count(false)
row_with_values = @parsed_csv.reject { |row| row.to_hash.values.all?(&:nil?) }
row_with_values.count
end

def delimiter
@delimiter ||= default_delimiter
@delimiter ||= Zizia::HyraxBasicMetadataMapper.new.delimiter
end
attr_writer :delimiter

def default_delimiter
Zizia::HyraxBasicMetadataMapper.new.delimiter
end

def valid_headers
@valid_headers ||= begin
Zizia::HyraxBasicMetadataMapper.new.headers.map do |header|
Expand All @@ -69,7 +64,6 @@ def valid_headers
def parse_csv
# Note: The 'table' method automatically turns the headers into symbols with underscores
@parsed_csv = CSV.table(csv_file.path)
# @rows = CSV.read(csv_file.path).reject { |x| x.empty? || x.all?(nil) }
rescue
@errors << 'We are unable to read this CSV file.'
end
Expand All @@ -79,8 +73,10 @@ def headers
end

def object_types
return ["work"] unless headers.include?(:object_type)
@object_types ||= @parsed_csv[:object_type].map { |object_type| map_object_type(object_type) }.compact.uniq
@object_types ||= begin
return ["work"] unless headers.include?(:object_type)
@parsed_csv[:object_type].map { |object_type| map_object_type(object_type) }.compact.uniq
end
end

def map_object_type(orig_value)
Expand Down Expand Up @@ -144,7 +140,7 @@ def missing_values
@parsed_csv.each_with_index do |row, index|
# Skip blank rows
next if row.to_hash.values.all?(&:nil?)
required_headers(object_type(row)).each do |required_header|
required_headers(row[:object_type]).each do |required_header|
next unless row[required_header].blank?
@errors << "Missing required metadata in row #{index + 2}: \"#{required_header.to_s.titleize}\" field cannot be blank"
end
Expand All @@ -153,10 +149,6 @@ def missing_values

private

def object_type(row)
row[headers.find_index(:object_type)]&.downcase
end

# Only allow valid license values expected by Hyrax.
# Otherwise the app throws an error when it displays the work.
def invalid_license
Expand Down

0 comments on commit 021281f

Please sign in to comment.