Sha256: 7d3f2a32f640de3b3b740b3fd896132b389f5a058bde8788daee5e9f5b49398b

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

class RemoteTable
  module Csv
    def each_row(&block)
      backup_file!
      convert_file_to_utf8!
      skip_rows!
      FasterCSV.foreach(path, fastercsv_options) do |row|
        ordered_hash = ActiveSupport::OrderedHash.new
        filled_values = 0
        case row
        when FasterCSV::Row
          row.each do |header, value|
            next if header.blank?
            value = '' if value.nil?
            ordered_hash[header] = value
            filled_values += 1 if value.present?
          end
        when Array
          index = 0
          row.each do |value|
            value = '' if value.nil?
            ordered_hash[index] = value
            filled_values += 1 if value.present?
            index += 1
          end
        else
          raise "Unexpected #{row.inspect}"
        end
        yield ordered_hash if keep_blank_rows or filled_values.nonzero?
      end
    ensure
      restore_file!
    end
    
    private
    
    def fastercsv_options
      fastercsv_options = { :skip_blanks => !keep_blank_rows, :header_converters => lambda { |k| RemoteTable::File.convert_to_utf8 k.to_s, encoding } }
      if headers == false
        fastercsv_options.merge!(:headers => nil)
      else
        fastercsv_options.merge!(:headers => :first_row)
      end
      fastercsv_options.merge!(:col_sep => delimiter) if delimiter
      fastercsv_options
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
remote_table-0.2.11 lib/remote_table/file/csv.rb
remote_table-0.2.10 lib/remote_table/file/csv.rb
remote_table-0.2.9 lib/remote_table/file/csv.rb
remote_table-0.2.8 lib/remote_table/file/csv.rb
remote_table-0.2.7 lib/remote_table/file/csv.rb
remote_table-0.2.6 lib/remote_table/file/csv.rb
remote_table-0.2.5 lib/remote_table/file/csv.rb
remote_table-0.2.4 lib/remote_table/file/csv.rb