Sha256: 72f11b0adbe440e4a2406eb54c9007c35f31739f32e4d52d6599a8726fc2fd4c

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

if RUBY_VERSION >= "1.9"
  require 'csv'
  ::RemoteTable::MyCSV = ::CSV
else
  require 'fastercsv'
  ::RemoteTable::MyCSV = ::FasterCSV
end

class RemoteTable
  module Csv
    def each_row(&block)
      backup_file!
      convert_file_to_utf8!
      remove_useless_characters!
      skip_rows!
      ::RemoteTable::MyCSV.foreach(path, fastercsv_options) do |row|
        ordered_hash = ActiveSupport::OrderedHash.new
        filled_values = 0
        case row
        when ::RemoteTable::MyCSV::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 }
      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

1 entries across 1 versions & 1 rubygems

Version Path
remote_table-ruby19-0.2.30 lib/remote_table/file/csv.rb