Class: Eco::API::Common::People::PersonAttributeParser

Inherits:
Language::Models::ParserSerializer show all
Defined in:
lib/eco/api/common/people/person_attribute_parser.rb

Overview

Class to define a parser/serializer.

Instance Attribute Summary

Attributes inherited from Language::Models::ParserSerializer

#attr

Instance Method Summary collapse

Methods inherited from Language::Models::ParserSerializer

#def_serializer, #initialize, #parse, #serialize

Constructor Details

This class inherits a constructor from Eco::Language::Models::ParserSerializer

Instance Method Details

#active_when_all?(*attrs) ⇒ Boolean

Helper to build the active_when condition

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/eco/api/common/people/person_attribute_parser.rb', line 38

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) ⇒ Boolean

Helper to build the active_when condition

Returns:

  • (Boolean)


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

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(active_when: nil, &block) ⇒ Object

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. By default, an attribute paraser is active if in the entry to parse the internal attribute is present.

Parameters:

  • active_when (Proc) (defaults to: nil)

    that expects a list of internal attributes or the internal entry itself

See Also:



17
18
19
20
# File 'lib/eco/api/common/people/person_attribute_parser.rb', line 17

def def_parser(active_when: nil, &block)
  @active_when = attribute_present(active_when)
  super(&block)
end

#parser_active?(source_data) ⇒ Boolean

Determines if for a given source data to parse, this parser should be active or not when for the current data to parse. It returns false otherwise.

Returns:

  • (Boolean)

    true if there's no callback defined or there is and evaluates true



25
26
27
# File 'lib/eco/api/common/people/person_attribute_parser.rb', line 25

def parser_active?(source_data)
  @active_when.call(source_data)
end