Class: Eco::API::Common::Loaders::Parser

Inherits:
CaseBase show all
Defined in:
lib/eco/api/common/loaders/parser.rb

Defined Under Namespace

Classes: RequiredAttrs

Class Attribute Summary collapse

Attributes included from Language::AuxiliarLogger

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CaseBase

#name, name_only_once!

Methods inherited from Base

<=>, created_at, set_created_at!

Methods included from ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(person_parser) ⇒ Parser

rubocop:disable Lint/MissingSuper



120
121
122
123
124
125
126
127
128
# File 'lib/eco/api/common/loaders/parser.rb', line 120

def initialize(person_parser) # rubocop:disable Lint/MissingSuper
  msg = "Expected Eco::API::Common::People::PersonParser. Given #{person_parser.class}"
  raise msg unless person_parser.is_a?(Eco::API::Common::People::PersonParser)

  person_parser.define_attribute(attribute, dependencies: self.class.dependencies) do |attr_parser|
    _define_parser(attr_parser)
    _define_serializer(attr_parser)
  end
end

Class Attribute Details

.active_whenObject (readonly)

Returns the value of attribute active_when.



30
31
32
# File 'lib/eco/api/common/loaders/parser.rb', line 30

def active_when
  @active_when
end

Class Method Details

.active_when_all(*attrs) ⇒ Object

Helper to build the active_when condition.



91
92
93
94
95
96
97
98
# File 'lib/eco/api/common/loaders/parser.rb', line 91

def active_when_all(*attrs)
  @active_when_attrs = RequiredAttrs.new(attribute, :all, attrs)

  @active_when = proc do |source_data|
    keys = data_keys(source_data)
    attrs.all? {|key| keys.include?(key)}
  end
end

.active_when_any(*attrs) ⇒ Object

Helper to build the active_when condition.



82
83
84
85
86
87
88
# File 'lib/eco/api/common/loaders/parser.rb', line 82

def active_when_any(*attrs)
  @active_when_attrs = RequiredAttrs.new(attribute, :any, attrs)
  @active_when = proc do |source_data|
    keys = data_keys(source_data)
    attrs.any? {|key| keys.include?(key)}
  end
end

.attribute(value = nil) ⇒ String

Returns the type of usecase (i.e. :sync, :transform, :import, :other).

Parameters:

  • value (String, Symbol) (defaults to: nil)

    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.

Returns:

  • (String)

    the type of usecase (i.e. :sync, :transform, :import, :other)



36
37
38
39
40
41
42
43
44
45
# File 'lib/eco/api/common/loaders/parser.rb', line 36

def attribute(value = nil)
  unless value
    msg  = "You should specify the 'attribute' this parser/serializer, "
    msg << "#{self.class}, is linked to"
    return @attribute || (raise msg)
  end

  name value
  @attribute = value
end

.dependencies(**value) ⇒ Object

Some parsers require dependencies to do their job.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eco/api/common/loaders/parser.rb', line 48

def dependencies(**value)
  @dependencies ||= {}

  if value.empty?
    return @dependencies.merge({
      required_attrs: @active_when_attrs
    })
  end

  raise "Expected Hash. Given: '#{value.class}'" unless value.is_a?(Hash)
  @dependencies.merge!(value)
end

.parsing_phase(phase = nil) ⇒ Object

Define or get the phase that the parser kicks in.

Parameters:

  • phase (Symbol) (defaults to: nil)

    the phase when this parser should be active. Must be one of [:internal, :final]



64
65
66
67
68
69
# File 'lib/eco/api/common/loaders/parser.rb', line 64

def parsing_phase(phase = nil)
  @parsing_phase ||= :internal
  return @parsing_phase unless phase

  @parsing_phase = phase
end

.serializing_phase(phase = nil) ⇒ Object

Define or get the phase that the serializer kicks in.

Parameters:

  • phase (Symbol) (defaults to: nil)

    the phase when this serializer should be active. Must be one of [:person, :final, :internal]



74
75
76
77
78
79
# File 'lib/eco/api/common/loaders/parser.rb', line 74

def serializing_phase(phase = nil)
  @serializing_phase ||= :person
  return @serializing_phase unless phase

  @serializing_phase = phase
end

Instance Method Details

#attributeString, Symbol

Returns the field/attribute or type this parser is linked to.

Returns:

  • (String, Symbol)

    the field/attribute or type this parser is linked to.



151
152
153
# File 'lib/eco/api/common/loaders/parser.rb', line 151

def attribute
  self.class.attribute
end

#parser(_data, _deps) ⇒ Object

Parameters:

  • 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).
  • deps (Hash)

    the merged dependencies (default to the class object and when calling the parser).



136
137
138
# File 'lib/eco/api/common/loaders/parser.rb', line 136

def parser(_data, _deps)
  raise "You should implement this method"
end