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

Inherits:
Object
  • Object
show all
Includes:
ProcessesFileLocally
Defined in:
decidim-admin/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 class to be used during the import. context - A hash including component specific data.



23
24
25
26
27
28
29
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 23

def initialize(file:, reader: Readers::Base, creator: Creator, context: nil)
  @file = file
  @reader = reader
  @creator = creator
  @context = context
  @data_headers = []
end

Instance Method Details

#collectionObject

Returns a collection of creators



48
49
50
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 48

def collection
  @collection ||= collection_data.map { |item| creator.new(item, context) }
end

#import!Object

Save resources



43
44
45
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 43

def import!
  collection.map(&:finish!)
end

#invalid_file?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 52

def invalid_file?
  collection.blank?
rescue Decidim::Admin::Import::InvalidFileError
  true
end

#prepareObject

Import data and create resources

Returns an array of resources



38
39
40
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 38

def prepare
  @prepare ||= collection.map(&:produce)
end

#verifyObject



31
32
33
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 31

def verify
  verifier.valid?
end