class Decidim::TermCustomizer::Import::Parser

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.

Attributes

data[R]

Public Class Methods

new(data) click to toggle source

Initializes the parser with a resource.

data - The data hash to parse.

# File lib/decidim/term_customizer/import/parser.rb, line 19
def initialize(data)
  @data = data.except(:id, "id")
end
resource_klass() click to toggle source

Retuns the resource class to be created with the provided data.

# 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

Public Instance Methods

parse() click to toggle source

Public: Returns a parsed object with the parsed data.

Returns a target object.

# File lib/decidim/term_customizer/import/parser.rb, line 43
def parse
  self.class.resource_klass.new(resource_attributes)
end
resource_attributes() click to toggle source

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.

# File lib/decidim/term_customizer/import/parser.rb, line 36
def resource_attributes
  @data
end