Module: CSVImporter

Defined in:
lib/csv_importer.rb,
lib/csv_importer/dsl.rb,
lib/csv_importer/row.rb,
lib/csv_importer/column.rb,
lib/csv_importer/config.rb,
lib/csv_importer/header.rb,
lib/csv_importer/report.rb,
lib/csv_importer/runner.rb,
lib/csv_importer/version.rb,
lib/csv_importer/csv_reader.rb,
lib/csv_importer/report_message.rb,
lib/csv_importer/column_definition.rb

Overview

A class that includes CSVImporter inherit its DSL and methods.

Example:

class ImportUserCSV
  include CSVImporter

  model User

  column :email
end

report = ImportUserCSV.new(file: my_csv).run!
puts report.message

Defined Under Namespace

Modules: Dsl Classes: CSVReader, Column, ColumnDefinition, Config, Configurator, Error, Header, Report, ReportMessage, Row, Runner

Constant Summary collapse

VERSION =
"0.8.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



63
64
65
# File 'lib/csv_importer.rb', line 63

def config
  @config
end

#csvObject (readonly)

Returns the value of attribute csv.



63
64
65
# File 'lib/csv_importer.rb', line 63

def csv
  @csv
end

#reportObject (readonly)

Returns the value of attribute report.



63
64
65
# File 'lib/csv_importer.rb', line 63

def report
  @report
end

Class Method Details

.included(klass) ⇒ Object

Setup DSL and config object



34
35
36
37
38
39
# File 'lib/csv_importer.rb', line 34

def self.included(klass)
  klass.extend(Dsl)
  klass.define_singleton_method(:config) do
    @config ||= Config.new
  end
end

Instance Method Details

#headerObject

Initialize and return the ‘Header` for the current CSV file



66
67
68
# File 'lib/csv_importer.rb', line 66

def header
  @header ||= Header.new(column_definitions: config.column_definitions, column_names: csv.header)
end

#initialize(*args, &block) ⇒ Object

Defines the path, file or content of the csv file. Also allows you to overwrite the configuration at runtime.

Example:

.new(file: my_csv_file)
.new(path: "subscribers.csv", model: newsletter.subscribers)


55
56
57
58
59
60
61
# File 'lib/csv_importer.rb', line 55

def initialize(*args, &block)
  @csv = CSVReader.new(*args)
  @config = self.class.config.dup
  @config.attributes = args.last
  @report = Report.new
  Configurator.new(@config).instance_exec(&block) if block
end

#rowsObject

Initialize and return the ‘Row`s for the current CSV file



71
72
73
74
75
76
# File 'lib/csv_importer.rb', line 71

def rows
  csv.rows.map.with_index(2) do |row_array, line_number|
    Row.new(header: header, line_number: line_number, row_array: row_array, model_klass: config.model,
            identifiers: config.identifiers, after_build_blocks: config.after_build_blocks)
  end
end

#run!Object

Run the import. Return a Report.



94
95
96
97
98
99
100
101
102
103
# File 'lib/csv_importer.rb', line 94

def run!
  if valid_header?
    @report = Runner.call(rows: rows, when_invalid: config.when_invalid,
                          after_save_blocks: config.after_save_blocks, report: @report)
  else
    @report
  end
rescue CSV::MalformedCSVError => e
  @report = Report.new(status: :invalid_csv_file, parser_error: e.message)
end

#valid_header?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/csv_importer.rb', line 78

def valid_header?
  if @report.pending?
    if header.valid?
      @report = Report.new(status: :pending, extra_columns: header.extra_columns)
    else
      @report = Report.new(status: :invalid_header, missing_columns: header.missing_required_columns, extra_columns: header.extra_columns)
    end
  end

  header.valid?
rescue CSV::MalformedCSVError => e
  @report = Report.new(status: :invalid_csv_file, parser_error: e.message)
  false
end