Class: Decidim::Admin::Import::Readers::Base

Inherits:
Object
  • Object
show all
Defined in:
decidim-admin/lib/decidim/admin/import/readers/base.rb

Overview

Abstract class with a very naive default implementation. Each importable file type should have it is own reader.

Direct Known Subclasses

CSV, JSON, XLSX

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Base

Returns a new instance of Base.



17
18
19
# File 'decidim-admin/lib/decidim/admin/import/readers/base.rb', line 17

def initialize(file)
  @file = file
end

Class Method Details

.first_data_indexObject

Defines which index of the records defines the first line of actual data. E.g. with spreadsheet formats, the first row contains column name information.



13
14
15
# File 'decidim-admin/lib/decidim/admin/import/readers/base.rb', line 13

def self.first_data_index
  0
end

Instance Method Details

#example_file(_data) ⇒ Object

The example_file should produce an example data file for the user to download and take example from to produce their import files. The data provided for the example file generation should be the same as what is returned by the read_rows method.

_data - An array of data to produce the file from

Returns an IO stream that can be saved to a file or sent to the browser to produce the import file.

Raises:

  • (NotImplementedError)


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

def example_file(_data)
  raise NotImplementedError
end

#read_rowsObject

The read_rows method should iterate over each row of the data and yield the data array of each row with the row’s index. The first row yielded with index 0 needs to contain the data headers which can be later used to map the data to correct attributes.

This needs to be implemented by the extending classes.

Returns an array of the import data where the first row should contain the columns.

Raises:

  • (NotImplementedError)


30
31
32
# File 'decidim-admin/lib/decidim/admin/import/readers/base.rb', line 30

def read_rows
  raise NotImplementedError
end