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

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


21
22
23
24
25
26
27
# File 'decidim-admin/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

#collectionObject

Returns a collection of creators


46
47
48
# File 'decidim-admin/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 'decidim-admin/lib/decidim/admin/import/importer.rb', line 41

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

#invalid_file?Boolean

Returns:

  • (Boolean)

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

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

#prepareObject

Import data and create resources

Returns an array of resources


36
37
38
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 36

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

#verifyObject


29
30
31
# File 'decidim-admin/lib/decidim/admin/import/importer.rb', line 29

def verify
  verifier.valid?
end