Class: ARCSVImporter::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_csv_importer/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, import_class, *row_order) ⇒ Importer

Returns a new instance of Importer.



5
6
7
8
9
# File 'lib/ar_csv_importer/importer.rb', line 5

def initialize(file_path, import_class, *row_order)
  @file_path = file_path
  @import_class = import_class
  @row_order = row_order.flatten
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



3
4
5
# File 'lib/ar_csv_importer/importer.rb', line 3

def file_path
  @file_path
end

#import_classObject

Returns the value of attribute import_class.



3
4
5
# File 'lib/ar_csv_importer/importer.rb', line 3

def import_class
  @import_class
end

#row_orderObject

Returns the value of attribute row_order.



3
4
5
# File 'lib/ar_csv_importer/importer.rb', line 3

def row_order
  @row_order
end

Instance Method Details

#import(raise_validation_errors = false) ⇒ Object



11
12
13
14
15
16
# File 'lib/ar_csv_importer/importer.rb', line 11

def import(raise_validation_errors = false)
  meth = raise_validation_errors ? :create! : :create
  CSV.foreach(file_path) do |row|
    import_class.send(meth, attrs_from_row(row))
  end
end