Module: ActiveRecordCSVImporter
- Defined in:
- lib/activerecord_csv_importer.rb,
lib/activerecord_csv_importer/dsl.rb,
lib/activerecord_csv_importer/row.rb,
lib/activerecord_csv_importer/column.rb,
lib/activerecord_csv_importer/config.rb,
lib/activerecord_csv_importer/header.rb,
lib/activerecord_csv_importer/report.rb,
lib/activerecord_csv_importer/runner.rb,
lib/activerecord_csv_importer/version.rb,
lib/activerecord_csv_importer/csv_reader.rb,
lib/activerecord_csv_importer/report_message.rb,
lib/activerecord_csv_importer/column_definition.rb
Defined Under Namespace
Modules: Dsl Classes: CSVReader, Column, ColumnDefinition, Config, Configurator, Error, Header, Report, ReportMessage, Row, Runner
Constant Summary collapse
- VERSION =
'0.2.3'.freeze
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.
48 49 50 |
# File 'lib/activerecord_csv_importer.rb', line 48 def config @config end |
#csv ⇒ Object (readonly)
Returns the value of attribute csv.
48 49 50 |
# File 'lib/activerecord_csv_importer.rb', line 48 def csv @csv end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
48 49 50 |
# File 'lib/activerecord_csv_importer.rb', line 48 def report @report end |
Class Method Details
.included(klass) ⇒ Object
Setup DSL and config object
20 21 22 23 24 25 |
# File 'lib/activerecord_csv_importer.rb', line 20 def self.included(klass) klass.extend(Dsl) klass.define_singleton_method(:config) do @config ||= Config.new end end |
Instance Method Details
#header ⇒ Object
Initialize and return the ‘Header` for the current CSV file
51 52 53 54 55 56 |
# File 'lib/activerecord_csv_importer.rb', line 51 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: .subscribers)
40 41 42 43 44 45 46 |
# File 'lib/activerecord_csv_importer.rb', line 40 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
59 60 61 62 63 |
# File 'lib/activerecord_csv_importer.rb', line 59 def rows csv.rows.map { |row_array| Row.new(header: header, row_array: row_array).to_a } end |
#run! ⇒ Object
Run the import. Return a Report.
81 82 83 84 85 86 87 88 89 |
# File 'lib/activerecord_csv_importer.rb', line 81 def run! if valid_header? @report = Runner.call(header: header, rows: rows, config: config) else @report end rescue CSV::MalformedCSVError => e @report = Report.new(status: :invalid_csv_file, parser_error: e.) end |
#valid_header? ⇒ Boolean
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/activerecord_csv_importer.rb', line 65 def valid_header? if @report.pending? if header.valid? @report = Report.new(status: :pending) else @report = Report.new( status: :invalid_header, missing_columns: header.missing_required_columns ) end end header.valid? end |