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.
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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#csv ⇒ Object
readonly
Returns the value of attribute csv.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
Class Method Summary collapse
-
.included(klass) ⇒ Object
Setup DSL and config object.
Instance Method Summary collapse
-
#header ⇒ Object
Initialize and return the ‘Header` for the current CSV file.
-
#initialize(*args, &block) ⇒ Object
Defines the path, file or content of the csv file.
-
#rows ⇒ Object
Initialize and return the ‘Row`s for the current CSV file.
-
#run! ⇒ Object
Run the import.
- #valid_header? ⇒ Boolean
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
63 64 65 |
# File 'lib/csv_importer.rb', line 63 def config @config end |
#csv ⇒ Object (readonly)
Returns the value of attribute csv.
63 64 65 |
# File 'lib/csv_importer.rb', line 63 def csv @csv end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
63 64 65 |
# File 'lib/csv_importer.rb', line 63 def report @report end |
Class Method Details
Instance Method Details
#header ⇒ Object
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 |
#rows ⇒ Object
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.) end |
#valid_header? ⇒ 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.) false end |