Module: RemoteTable::Csv

Defined in:
lib/remote_table/file/csv.rb

Instance Method Summary collapse

Instance Method Details

#each_row(&block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/remote_table/file/csv.rb', line 3

def each_row(&block)
  skip_rows!
  FasterCSV.parse(open(path), fastercsv_options) do |row|
    if row.respond_to?(:fields) # it's a traditional fastercsv row hash
      next if row.fields.compact.blank?
      hash = HashWithIndifferentAccess.new(row.to_hash)
    else                        # it's an array, which i think happens if you're using :headers => nil or :col_sep
      next if row.compact.blank?
      index = 0
      hash = row.inject(ActiveSupport::OrderedHash.new) { |memo, element| memo[index] = element; index += 1; memo }
    end
    yield hash
  end
ensure
  restore_rows!
end