class Eco::API::Common::Loaders::Parser

Attributes

active_when[R]

Public Class Methods

active_when_all(*attrs) click to toggle source

Helper to build the `active_when` condition.

# File lib/eco/api/common/loaders/parser.rb, line 81
def active_when_all(*attrs)
  @active_when_attrs = RequiredAttrs.new(attribute, :all, attrs)
  @active_when = Proc.new do |source_data|
    keys = data_keys(source_data)
    attrs.all? {|key| keys.include?(key)}
  end
end
active_when_any(*attrs) click to toggle source

Helper to build the `active_when` condition.

# File lib/eco/api/common/loaders/parser.rb, line 72
def active_when_any(*attrs)
  @active_when_attrs = RequiredAttrs.new(attribute, :any, attrs)
  @active_when = Proc.new do |source_data|
    keys = data_keys(source_data)
    attrs.any? {|key| keys.include?(key)}
  end
end
attribute(value = nil) click to toggle source

@param value [String, Symbol] the attribute or type to be parsed/serialized

1. when `String`: the internal name of the field/attribute this parser/serializer manages for.
2. when `Symbol`: the type of data this parser converts between `String` and the specific type.

@return [String] the `type` of usecase (i.e. `:sync`, `:transform`, `:import`, `:other`)

# File lib/eco/api/common/loaders/parser.rb, line 35
def attribute(value = nil)
  unless value
    return @attribute || raise("You should specify the 'attribute' this parser/serializer, #{self.class}, is linked to")
  end
  name value
  @attribute = value
end
dependencies(**value) click to toggle source

Some parsers require dependencies to do their job.

# File lib/eco/api/common/loaders/parser.rb, line 44
def dependencies(**value)
  @dependencies ||= {}
  return @dependencies.merge({
    required_attrs: @active_when_attrs
  }) unless !value.empty?
  raise "Expected Hash. Given: '#{value.class}'" unless value.is_a?(Hash)
  @dependencies.merge!(value)
end
new(person_parser) click to toggle source
# File lib/eco/api/common/loaders/parser.rb, line 109
def initialize(person_parser)
  raise "Expected Eco::API::Common::People::PersonParser. Given #{policies.class}" unless person_parser.is_a?(Eco::API::Common::People::PersonParser)
  person_parser.define_attribute(self.attribute, dependencies: self.class.dependencies) do |attr_parser|
    _define_parser(attr_parser)
    _define_serializer(attr_parser)
  end
end
parsing_phase(phase = nil) click to toggle source

Define or get the `phase` that the `parser` kicks in. @param phase [Symbol] the phase when this parser should be active.

Must be one of [`:internal`, `:final`]
# File lib/eco/api/common/loaders/parser.rb, line 56
def parsing_phase(phase = nil)
  @parsing_phase ||= :internal
  return @parsing_phase unless phase
  @parsing_phase = phase
end
serializing_phase(phase = nil) click to toggle source

Define or get the `phase` that the `serializer` kicks in. @param phase [Symbol] the phase when this serializer should be active.

Must be one of [`:person,` `:final`, `:internal`]
# File lib/eco/api/common/loaders/parser.rb, line 65
def serializing_phase(phase = nil)
  @serializing_phase ||= :person
  return @serializing_phase unless phase
  @serializing_phase = phase
end

Private Class Methods

data_keys(source_data) click to toggle source

Helper to obtain the current internal named attributes of the data @param source_data [Array<String>, Hash] if `Array` those are already the `keys`, if `Hash` it gets the `keys` @return [Array<String>] `keys` of `source_data`

# File lib/eco/api/common/loaders/parser.rb, line 94
def data_keys(source_data)
  case source_data
  when Array
    keys = source_data
  when Hash
    keys = source_data.keys
  else
    keys = []
  end
end

Public Instance Methods

attribute() click to toggle source

@return [String, Symbol] the field/attribute or type this parser is linked to.

# File lib/eco/api/common/loaders/parser.rb, line 135
def attribute
  self.class.attribute
end
parser(data, deps) click to toggle source

@param data [Hash] all the person data at the specified `parsing_phase`:

- when `:internal`: the parser will receive external types (i.e. String with '|' delimiters instead of an Array).
- when `:final`: the parser will receive the typed values (i.e. Array instread of String with '|' delimiters).

@param deps [Hash] the merged dependencies (default to the class object and when calling the parser).

# File lib/eco/api/common/loaders/parser.rb, line 121
def parser(data, deps)
  raise "You should implement this method"
end
seralizer(data, deps) click to toggle source

@param data [Hash, Ecoportal::API::V1::Person] all the person data at the specified `serializing_phase`:

- when `:internal`: it will receive a `Hash` with the **internal values** but the types already serialized to `String`.
- when `:final`: it will receive a `Hash` with the **internal values** and **types**.
- when `:person`: it will receive the `person` object.

@param deps [Hash] the merged dependencies (default to the class object and when calling the parser).

# File lib/eco/api/common/loaders/parser.rb, line 130
def seralizer(data, deps)
  raise "You should implement this method"
end

Private Instance Methods

_define_parser(attr_parser) click to toggle source
# File lib/eco/api/common/loaders/parser.rb, line 141
def _define_parser(attr_parser)
  if active_when = self.class.active_when
    attr_parser.def_parser(self.class.parsing_phase, active_when: active_when, &self.method(:parser))
  else
    attr_parser.def_parser(self.class.parsing_phase, &self.method(:parser))
  end
end
_define_serializer(attr_parser) click to toggle source
# File lib/eco/api/common/loaders/parser.rb, line 149
def _define_serializer(attr_parser)
  attr_parser.def_serializer(self.class.serializing_phase, &self.method(:serializer))
end