Sha256: e7f4be4ee72f08fc904776d8200c177c6b3afe280ebdcfae60bf56f600994491

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Valigator
  module CSV
    class Error

      attr_reader :row, :type, :message



      def initialize(error)
        case error
          when Hash
            build_from_hash error
          when StandardError
            build_from_error error
        end
      end



      def ==(other)
        row == other.row && message == other.message && type == other.type
      end



      private

      def build_from_hash(error)
        build error[:type], error[:message], error[:row]
      end



      def build_from_error(error)
        build map_to_type(error.message), error.message, determine_row(error.message)
      end



      def determine_row(message)
        matches = /line (?<lineno>\d+)/.match(message)
        matches[:lineno].to_i if matches
      end



      def build(type, message, row)
        @type = type
        @row = row
        @message = message
      end



      def map_to_type(message)
        case message
          when /Missing or stray quote/
            'stray_quote'
          when /Unquoted fields do not allow/
            'line_breaks'
          when /Illegal quoting/
            'illegal_quoting'
          when /Field size exceeded/
            'field_size'
          when /Unclosed quoted field/
            'unclosed_quote'
          when /invalid byte sequence/
            'invalid_encoding'
          else
            raise ArgumentError, 'unknown type'
        end
      end


    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
valigator-csv-1.1.2 lib/valigator/csv/error.rb
valigator-csv-1.1.1 lib/valigator/csv/error.rb
valigator-csv-1.1.0 lib/valigator/csv/error.rb