Class: Eco::API::Common::People::BaseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/common/people/base_parser.rb

Overview

Helper class to inherit from to ease the definition of attribute parsers in a PersonParser object, mostly used in for configuration loading.

Examples:

Example of usage:

class ExampleParser < Eco::API::Common::People::BaseParser
  def process
    @parsers.define_attribute("example") do |parser|
      parser.def_parser  do |str, deps|
        i = value.to_i rescue 0
        i +=5 if deps.dig(:sum_5)
        i
      end.def_serializer do |value|
        value.to_s
      end
    end
  end
end
pparser = Eco::API::Common::People::PersonParser.new
ExampleParser.new(pparser)
pparser.parse("example","3") # out: 3
pparser.parse("example","3", deps: {sum_5: true}) # out: 8
pparser.serialise("example, 8) # out: "8"

Instance Method Summary collapse

Constructor Details

#initialize(parsers, **options) ⇒ BaseParser

Returns a new instance of BaseParser.

Parameters:



30
31
32
33
# File 'lib/eco/api/common/people/base_parser.rb', line 30

def initialize(parsers, **options)
  @parsers = parsers
  @options = options
end

Instance Method Details

#processObject

Note:

this method is called to load the definition of the attribute parser/serializer.

Method to be overriden by the child class.



37
38
39
# File 'lib/eco/api/common/people/base_parser.rb', line 37

def process
  raise "Method needs to be overriden in the child class #{self.class}"
end