Class: Antz::Importer
- Inherits:
-
Object
- Object
- Antz::Importer
- Defined in:
- lib/antz/importer.rb
Instance Attribute Summary collapse
-
#depends_on ⇒ Object
readonly
Returns the value of attribute depends_on.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #execute(dry_run: false) ⇒ Object
-
#initialize(name, depends_on: [], model: nil, file: nil, on_duplicate: :update) ⇒ Importer
constructor
A new instance of Importer.
Constructor Details
#initialize(name, depends_on: [], model: nil, file: nil, on_duplicate: :update) ⇒ Importer
Returns a new instance of Importer.
11 12 13 14 15 16 17 18 |
# File 'lib/antz/importer.rb', line 11 def initialize(name, depends_on: [], model: nil, file: nil, on_duplicate: :update, &) @name = name @depends_on = depends_on @model_class = model || name.to_s.singularize.camelize.constantize @file_name = file || "#{name}.csv" @on_duplicate = on_duplicate @field_mapper = FieldMapper.new(&) end |
Instance Attribute Details
#depends_on ⇒ Object (readonly)
Returns the value of attribute depends_on.
9 10 11 |
# File 'lib/antz/importer.rb', line 9 def depends_on @depends_on end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
9 10 11 |
# File 'lib/antz/importer.rb', line 9 def file_name @file_name end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
9 10 11 |
# File 'lib/antz/importer.rb', line 9 def model_class @model_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/antz/importer.rb', line 9 def name @name end |
Instance Method Details
#execute(dry_run: false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/antz/importer.rb', line 20 def execute(dry_run: false) source = Source::CsvFile.new(file_path: file_path) sink = Sink::ActiveRecord.new(model: @model_class, on_duplicate: @on_duplicate) source.each_row(batch_size: Antz.configuration.batch_size) do |batch| mapped = batch.map { |row| @field_mapper.transform(row) } if dry_run Antz.configuration.logger.info("[DRY RUN] Would write: #{mapped}") else sink.write(mapped) end end end |