Sha256: 3bf82b5ce3a0e4a8ba2419b2d3aa01d90cb16792f6feeca2cae7402d6c4e87cd

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 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 }
      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

11 entries across 11 versions & 1 rubygems

Version Path
remote_table-0.2.22 lib/remote_table/file/csv.rb
remote_table-0.2.21 lib/remote_table/file/csv.rb
remote_table-0.2.20 lib/remote_table/file/csv.rb
remote_table-0.2.19 lib/remote_table/file/csv.rb
remote_table-0.2.18 lib/remote_table/file/csv.rb
remote_table-0.2.17 lib/remote_table/file/csv.rb
remote_table-0.2.16 lib/remote_table/file/csv.rb
remote_table-0.2.15 lib/remote_table/file/csv.rb
remote_table-0.2.14 lib/remote_table/file/csv.rb
remote_table-0.2.13 lib/remote_table/file/csv.rb
remote_table-0.2.12 lib/remote_table/file/csv.rb