Module: CsvWizard::Importer
- Defined in:
- lib/csv_wizard/importer.rb
Instance Method Summary collapse
- #column(name, required: false, default: nil) ⇒ Object
- #import(file_path, before_import: nil, after_import: nil) ⇒ Object
- #map_csv_to(model_class, &block) ⇒ Object
Instance Method Details
#column(name, required: false, default: nil) ⇒ Object
9 10 11 |
# File 'lib/csv_wizard/importer.rb', line 9 def column(name, required: false, default: nil) @mapper.map(name, to: name.downcase.to_sym, required: required, default: default) end |
#import(file_path, before_import: nil, after_import: nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/csv_wizard/importer.rb', line 13 def import(file_path, before_import: nil, after_import: nil) failed_rows = [] CSV.foreach(file_path, headers: true).with_index(2) do |row, idx| processor = RowProcessor.new(@model_class, @mapper, row.to_h, idx, before_import: before_import, after_import: after_import) success = processor.save failed_rows << { line: idx, row: row.to_h, errors: processor.errors } unless success end failed_rows end |