Class: Decidim::Admin::Import::Importer
- Inherits:
-
Object
- Object
- Decidim::Admin::Import::Importer
- Defined in:
- lib/decidim/admin/import/importer.rb
Overview
Class providing the interface and implementation of an importer. Needs a reader to be passed to the constructor which handles the import file reading depending on its type.
You can also use the ImporterFactory class to create an Importer instance.
Instance Method Summary collapse
-
#collection ⇒ Object
Returns a collection of creators.
-
#import! ⇒ Object
Save resources.
-
#initialize(file:, reader: Readers::Base, creator: Creator, context: nil) ⇒ Importer
constructor
Public: Initializes an Importer.
-
#invalid_lines ⇒ Object
Returns array of all resource indexes where validations fail.
-
#prepare ⇒ Object
Import data and create resources.
Constructor Details
#initialize(file:, reader: Readers::Base, creator: Creator, context: nil) ⇒ Importer
Public: Initializes an Importer.
file - A file with the data to be imported. reader - A Reader to be used to read the data from the file. creator - A Creator to be used during the import. context - A hash including component specific data.
19 20 21 22 23 24 |
# File 'lib/decidim/admin/import/importer.rb', line 19 def initialize(file:, reader: Readers::Base, creator: Creator, context: nil) @file = file @reader = reader @creator = creator @context = context end |
Instance Method Details
#collection ⇒ Object
Returns a collection of creators
39 40 41 |
# File 'lib/decidim/admin/import/importer.rb', line 39 def collection @collection ||= collection_data.map { |item| creator.new(item, context) } end |
#import! ⇒ Object
Save resources
34 35 36 |
# File 'lib/decidim/admin/import/importer.rb', line 34 def import! collection.map(&:finish!) end |
#invalid_lines ⇒ Object
Returns array of all resource indexes where validations fail.
44 45 46 |
# File 'lib/decidim/admin/import/importer.rb', line 44 def invalid_lines @invalid_lines ||= check_invalid_lines(prepare) end |
#prepare ⇒ Object
Import data and create resources
Returns an array of resources
29 30 31 |
# File 'lib/decidim/admin/import/importer.rb', line 29 def prepare @prepare ||= collection.map(&:produce) end |