Class: Decidim::Admin::Import::Importer

Inherits:
Object
  • Object
show all
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

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

#collectionObject

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_linesObject

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

#prepareObject

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