Sha256: b874137f75f5ad396f18cd1b391e4c66842214d0cf28b4120cddd8651fbfdc48

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module SmarterCSV
  module HeaderValidations
    def header_validations(headers, options)
      check_duplicate_headers(headers, options)
      check_required_headers(headers, options)
    end

    def check_duplicate_headers(headers, _options)
      header_counts = Hash.new(0)
      headers.each { |header| header_counts[header] += 1 unless header.nil? }

      duplicates = header_counts.select { |_, count| count > 1 }

      unless duplicates.empty?
        raise(SmarterCSV::DuplicateHeaders, "Duplicate Headers in CSV: #{duplicates.inspect}")
      end
    end

    require 'set'

    def check_required_headers(headers, options)
      if options[:required_keys] && options[:required_keys].is_a?(Array)
        headers_set = headers.to_set
        missing_keys = options[:required_keys].select { |k| !headers_set.include?(k) }

        unless missing_keys.empty?
          raise SmarterCSV::MissingKeys, "ERROR: missing attributes: #{missing_keys.join(',')}. Check `reader.headers` for original headers."
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smarter_csv-1.13.1 lib/smarter_csv/header_validations.rb
smarter_csv-1.13.0 lib/smarter_csv/header_validations.rb
smarter_csv-1.12.1 lib/smarter_csv/header_validations.rb
smarter_csv-1.12.0 lib/smarter_csv/header_validations.rb
smarter_csv-1.12.0.pre1 lib/smarter_csv/header_validations.rb