Class: Decidim::TermCustomizer::Import::Parser
- Inherits:
-
Object
- Object
- Decidim::TermCustomizer::Import::Parser
- Defined in:
- lib/decidim/term_customizer/import/parser.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 parser or this default will be used.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.resource_klass ⇒ Object
Retuns the resource class to be created with the provided data.
Instance Method Summary collapse
-
#initialize(data) ⇒ Parser
constructor
Initializes the parser with a resource.
-
#parse ⇒ Object
Public: Returns a parsed 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) ⇒ Parser
Initializes the parser with a resource.
data - The data hash to parse.
19 20 21 |
# File 'lib/decidim/term_customizer/import/parser.rb', line 19 def initialize(data) @data = data.except(:id, "id") end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
14 15 16 |
# File 'lib/decidim/term_customizer/import/parser.rb', line 14 def data @data end |
Class Method Details
.resource_klass ⇒ Object
Retuns the resource class to be created with the provided data.
24 25 26 |
# File 'lib/decidim/term_customizer/import/parser.rb', line 24 def self.resource_klass raise NotImplementedError, "#{self.class.name} does not define resource class" end |
Instance Method Details
#parse ⇒ Object
Public: Returns a parsed object with the parsed data.
Returns a target object.
43 44 45 |
# File 'lib/decidim/term_customizer/import/parser.rb', line 43 def parse 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 parser implementation.
Returns the resource attributes to be passed for the constructor.
36 37 38 |
# File 'lib/decidim/term_customizer/import/parser.rb', line 36 def resource_attributes @data end |