class Eco::API::Common::People::PersonAttributeParser

Class to define a parser/serializer.

Public Instance Methods

active_when_all?(*attrs) click to toggle source

Helper to build the `active_when` condition.

# File lib/eco/api/common/people/person_attribute_parser.rb, line 64
def active_when_all?(*attrs)
  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/people/person_attribute_parser.rb, line 56
def active_when_any?(*attrs)
  Proc.new do |source_data|
    keys = data_keys(source_data)
    attrs.any? {|key| keys.include?(key)}
  end
end
def_parser(phase = :internal, active_when: nil, &block) click to toggle source

@see Eco::Language::Models::ParserSerializer#def_parser @note

- additionally, you can declare a callback `active:` to determine if when the

parser will be active/used.

- this is important to help avoiding to set values that are not present in the input entry.
- if you have doubts about using it or not, do not use it.

@param phase [Symbol] the phase when this parser should be active.

Must be one of [`:internal`, `:final`]

@param active_when [Proc] that expects a list of internal attributes or the internal entry itself. By default, an attribute paraser is active if in the entry to parse the internal attribute is present.

# File lib/eco/api/common/people/person_attribute_parser.rb, line 27
def def_parser(phase = :internal, active_when: nil, &block)
  @active_when = attribute_present(active_when)
  super(phase, &block)
end
def_serializer(phase = :person, &block) click to toggle source

@param phase [Symbol] the phase when this serializer should be active.

Must be one of [`:person,` `:final`, `:internal`]
# File lib/eco/api/common/people/person_attribute_parser.rb, line 34
def def_serializer(phase = :person, &block)
  super(phase, &block)
end
parser_active?(source_data, phase = :any) click to toggle source

Determines if for a given source data to parse, this parser should be active or not. @param phase [Symbol] the phase when this parser should be active.

Must be one of [`:internal`, `:final`]

@return [Boolean] `true` if there's parser defined and `source_data` activates it.

# File lib/eco/api/common/people/person_attribute_parser.rb, line 42
def parser_active?(source_data, phase = :any)
  (phase == :any || parser_category?(phase)) && @active_when.call(source_data)
end
required_attrs() click to toggle source

@note

- This was introduced at a later stage and might not be available for certain org-parsers configs

@return [RequiredAttrs]

# File lib/eco/api/common/people/person_attribute_parser.rb, line 12
def required_attrs
  @required_attrs ||= @dependencies[:required_attrs]
  #@required_attrs ||= RequiredAttrs.new(attr, :unkown, [attr])
end
serializer_active?(phase = :any) click to toggle source

Determines a serializer should be active or not. @param phase [Symbol] the phase when this serializer should be active.

Must be one of [`:person,` `:final`, `:internal`]

@return [Boolean] `true` if there's serializer defined.

# File lib/eco/api/common/people/person_attribute_parser.rb, line 50
def serializer_active?(phase = :any)
  (phase == :any) || serializer_category?(phase)
end

Private Instance Methods

attribute_present(active_when) click to toggle source

By default, an attribute paraser is active if in the entry to parse the internal attribute is present

# File lib/eco/api/common/people/person_attribute_parser.rb, line 75
def attribute_present(active_when)
  Proc.new do |source_data|
    data_keys(source_data).include?(self.attr) ||
    (active_when && active_when.call(source_data))
  end
end
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/people/person_attribute_parser.rb, line 85
def data_keys(source_data)
  case source_data
  when Array
    keys = source_data
  when Hash
    keys = source_data.keys
  else
    keys = []
  end
end