lib/io_streams/tabular/utility/csv_row.rb in iostreams-1.1.0 vs lib/io_streams/tabular/utility/csv_row.rb in iostreams-1.1.1

- old
+ new

@@ -1,6 +1,6 @@ -require 'csv' +require "csv" module IOStreams class Tabular module Utility # For parsing a single line of CSV at a time # 2 to 3 times better performance than CSV.parse_line and considerably less @@ -9,15 +9,15 @@ # Note: # This parser does not support line feeds embedded in quoted fields since # the file is broken apart based on line feeds during the upload process and # is then processed by each worker on a line by line basis. class CSVRow < ::CSV - UTF8_ENCODING = Encoding.find('UTF-8').freeze + UTF8_ENCODING = Encoding.find("UTF-8").freeze def initialize(encoding = UTF8_ENCODING) - @io = StringIO.new(''.force_encoding(encoding)) - super(@io, row_sep: '') + @io = StringIO.new("".force_encoding(encoding)) + super(@io, row_sep: "") end # Parse a single line of CSV data # Parameters # line [String] @@ -37,13 +37,11 @@ if in_extended_col # If we are continuing a previous column if part[-1] == @quote_char && part.count(@quote_char).odd? # extended column ends csv.last << part[0..-2] - if csv.last =~ @parsers[:stray_quote] - raise MalformedCSVError, "Missing or stray quote in line #{lineno + 1}" - end + raise MalformedCSVError, "Missing or stray quote in line #{lineno + 1}" if csv.last =~ @parsers[:stray_quote] csv.last.gsub!(@quote_char * 2, @quote_char) in_extended_col = false else csv.last << part @@ -57,13 +55,11 @@ csv.last << @col_sep in_extended_col = true else # regular quoted column csv << part[1..-2] - if csv.last =~ @parsers[:stray_quote] - raise MalformedCSVError, "Missing or stray quote in line #{lineno + 1}" - end + raise MalformedCSVError, "Missing or stray quote in line #{lineno + 1}" if csv.last =~ @parsers[:stray_quote] csv.last.gsub!(@quote_char * 2, @quote_char) end elsif part =~ @parsers[:quote_or_nl] # Unquoted field with bad characters. @@ -80,27 +76,23 @@ # Replace tacked on @col_sep with @row_sep if we are still in an extended # column. csv[-1][-1] = @row_sep if in_extended_col - if in_extended_col - raise MalformedCSVError, "Unclosed quoted field on line #{lineno + 1}." - end + raise MalformedCSVError, "Unclosed quoted field on line #{lineno + 1}." if in_extended_col - @lineno += 1 + @lineno += 1 # save fields unconverted fields, if needed... unconverted = csv.dup if @unconverted_fields # convert fields, if needed... csv = convert_fields(csv) unless @use_headers || @converters.empty? # parse out header rows and handle CSV::Row conversions... csv = parse_headers(csv) if @use_headers # inject unconverted fields and accessor, if requested... - if @unconverted_fields && (!csv.respond_to? :unconverted_fields) - add_unconverted_fields(csv, unconverted) - end + add_unconverted_fields(csv, unconverted) if @unconverted_fields && (!csv.respond_to? :unconverted_fields) csv end # Return the supplied array as a single line CSV string.