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_file? ⇒ Boolean
-
#prepare ⇒ Object
Import data and create resources.
- #verify ⇒ Object
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 class to be used during the import. context - A hash including component specific data.
21 22 23 24 25 26 27 |
# File 'lib/decidim/admin/import/importer.rb', line 21 def initialize(file:, reader: Readers::Base, creator: Creator, context: nil) @file = file @reader = reader @creator = creator @context = context @data_headers = [] end |
Instance Method Details
#collection ⇒ Object
Returns a collection of creators
46 47 48 |
# File 'lib/decidim/admin/import/importer.rb', line 46 def collection @collection ||= collection_data.map { |item| creator.new(item, context) } end |
#import! ⇒ Object
Save resources
41 42 43 |
# File 'lib/decidim/admin/import/importer.rb', line 41 def import! collection.map(&:finish!) end |
#invalid_file? ⇒ Boolean
50 51 52 53 54 |
# File 'lib/decidim/admin/import/importer.rb', line 50 def invalid_file? collection.blank? rescue Decidim::Admin::Import::InvalidFileError true end |
#prepare ⇒ Object
Import data and create resources
Returns an array of resources
36 37 38 |
# File 'lib/decidim/admin/import/importer.rb', line 36 def prepare @prepare ||= collection.map(&:produce) end |
#verify ⇒ Object
29 30 31 |
# File 'lib/decidim/admin/import/importer.rb', line 29 def verify verifier.valid? end |