lib/remote_table/delimited.rb in remote_table-3.0.0.alpha vs lib/remote_table/delimited.rb in remote_table-3.0.0.beta
- old
+ new
@@ -12,28 +12,19 @@
else
require 'fastercsv'
Engine = ::FasterCSV
end
- PASSTHROUGH_CSV_SETTINGS = [
- :unconverted_fields,
- :col_sep,
- :row_sep,
- :return_headers,
- :header_converters,
- :quote_char,
- :converters,
- :force_quotes,
- ]
-
- # Yield each row using Ruby's CSV parser (FasterCSV on Ruby 1.8).
- def _each
+ def preprocess!
delete_harmful!
convert_eol_to_unix!
transliterate_whole_file_to_utf8!
skip_rows!
+ end
+ # Yield each row using Ruby's CSV parser (FasterCSV on Ruby 1.8).
+ def _each
Engine.new(local_copy.encoded_io, csv_options).each do |row|
some_value_present = false
if not headers
@@ -53,11 +44,10 @@
else
# represent the row as a hash
hash = ::ActiveSupport::OrderedHash.new
row.each do |k, v|
- next unless k.present?
v = v.to_s
if not some_value_present and not keep_blank_rows and v.present?
some_value_present = true
end
hash[k] = v
@@ -79,13 +69,33 @@
# * +:keep_blank_rows+ to the CSV option +:skip_blanks+
# * +:delimiter+ to the CSV option +:col_sep+
#
# @return [Hash]
def csv_options
- memo = other_options.slice(*PASSTHROUGH_CSV_SETTINGS)
- memo[:skip_blanks] = !keep_blank_rows
- memo[:headers] ||= headers
- memo[:col_sep] ||= delimiter
- memo
+ {
+ skip_blanks: !keep_blank_rows,
+ headers: headers,
+ col_sep: delimiter,
+ quote_char: quote_char,
+ }
+ end
+
+ def headers
+ return @_headers if defined?(@_headers)
+ @_headers = case @headers
+ when FalseClass, NilClass
+ false
+ when :first_row, TrueClass
+ i = 0
+ line = local_copy.encoded_io.gets
+ Engine.parse_line(line).map do |v|
+ header = v.to_s.gsub(/\s+/, ' ').strip
+ header.present? ? header : "empty_#{i+=1}"
+ end
+ when Array
+ @headers
+ else
+ raise "Invalid headers: #{headers.inspect}"
+ end
end
end
end