Class: ActiveRecordCSVImporter::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_csv_importer/row.rb

Overview

A Row from the CSV file.

returns a formatted version of the row based of the to proc

Instance Method Summary collapse

Instance Method Details

#csv_attributesObject

A hash with this row’s attributes



12
13
14
# File 'lib/activerecord_csv_importer/row.rb', line 12

def csv_attributes
  @csv_attributes ||= Hash[header.column_names.zip(row_array)]
end

#get_attribute(column_definition, csv_value) ⇒ Object

Set the attribute using the column_definition and the csv_value



32
33
34
35
36
37
38
# File 'lib/activerecord_csv_importer/row.rb', line 32

def get_attribute(column_definition, csv_value)
  if column_definition.to && column_definition.to.is_a?(Proc)
    column_definition.to.call(csv_value)
  else
    csv_value
  end
end

#to_aObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/activerecord_csv_importer/row.rb', line 16

def to_a
  header.columns.each_with_object([]) do |column, memo|
    value = csv_attributes[column.name]
    begin
      value = value.dup if value
    rescue TypeError
      # can't dup Symbols, Integer etc...
    end

    next if column.definition.nil?

    memo << get_attribute(column.definition, value)
  end
end