Class: Decidim::Admin::Import::Creator
- Inherits:
-
Object
- Object
- Decidim::Admin::Import::Creator
- Defined in:
- lib/decidim/admin/import/creator.rb
Overview
This is an abstract class with a very naive default implementation for the importers to use. It can also serve as a superclass of your own implementation.
It is used to be run against each element of an importable collection in order to parse relevant fields. Every import should specify their own creator or this default will be used.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .localize_headers(header, locales) ⇒ Object
- .required_headers ⇒ Object
-
.resource_klass ⇒ Object
Returns the resource class to be created with the provided data.
-
.verifier_klass ⇒ Object
Returns the verifier class to be used to ensure the data is valid for the import.
Instance Method Summary collapse
- #finish! ⇒ Object
-
#initialize(data, context = nil) ⇒ Creator
constructor
Initializes the creator with a resource.
-
#produce ⇒ Object
Public: Returns a created object with the parsed data.
-
#resource_attributes ⇒ Object
Can be used to convert the data hash to the resource attributes in case the data hash to be imported has different column names than the resource object to be created of it.
Constructor Details
#initialize(data, context = nil) ⇒ Creator
Initializes the creator with a resource.
data - The data hash to parse. context - The context needed by the producer
47 48 49 50 |
# File 'lib/decidim/admin/import/creator.rb', line 47 def initialize(data, context = nil) @data = data @context = context end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
41 42 43 |
# File 'lib/decidim/admin/import/creator.rb', line 41 def data @data end |
Class Method Details
.localize_headers(header, locales) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/decidim/admin/import/creator.rb', line 30 def localize_headers(header, locales) @localize_headers ||= begin localize_headers = [] locales.each do |locale| localize_headers << "#{header}/#{locale}".to_sym end localize_headers end end |
.required_headers ⇒ Object
26 27 28 |
# File 'lib/decidim/admin/import/creator.rb', line 26 def required_headers [] end |
.resource_klass ⇒ Object
Returns the resource class to be created with the provided data.
16 17 18 |
# File 'lib/decidim/admin/import/creator.rb', line 16 def resource_klass raise NotImplementedError, "#{self.class.name} does not define resource class" end |
Instance Method Details
#finish! ⇒ Object
71 72 73 |
# File 'lib/decidim/admin/import/creator.rb', line 71 def finish! resource.save! end |
#produce ⇒ Object
Public: Returns a created object with the parsed data.
Returns a target object.
67 68 69 |
# File 'lib/decidim/admin/import/creator.rb', line 67 def produce self.class.resource_klass.new(resource_attributes) end |
#resource_attributes ⇒ Object
Can be used to convert the data hash to the resource attributes in case the data hash to be imported has different column names than the resource object to be created of it.
By default returns the data hash but can be implemented by each creator implementation.
Returns the resource attributes to be passed for the constructor.
60 61 62 |
# File 'lib/decidim/admin/import/creator.rb', line 60 def resource_attributes @data end |