Sha256: c7cfab4538fe78596763af4a8c042176f2e2ec7ca5cd40d2d910a571ce6e52e6

Contents?: true

Size: 1.01 KB

Versions: 13

Compression:

Stored size: 1.01 KB

Contents

module ActiveRecordCSVImporter
  # A Row from the CSV file.
  #
  # returns a formatted version of the row based of the to proc
  class Row
    include Virtus.model

    attribute :header, Header
    attribute :row_array, Array[String]

    # A hash with this row's attributes
    def csv_attributes
      @csv_attributes ||= Hash[header.column_names.zip(row_array)]
    end

    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

    # Set the attribute using the column_definition and the csv_value
    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
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
activerecord_csv_importer-0.4.0 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.3.0 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.2.3 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.2.2 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.2.1 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.2.0 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.9 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.8 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.7 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.6 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.5 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.4 lib/activerecord_csv_importer/row.rb
activerecord_csv_importer-0.1.3 lib/activerecord_csv_importer/row.rb