Class: Eco::API::Common::People::PersonParser

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

Overview

Class to define/group a set of parsers/serializers.

Direct Known Subclasses

DefaultParsers

Constant Summary collapse

CORE_ATTRS =
["id", "external_id", "email", "name", "supervisor_id", "filter_tags"]
ACCOUNT_ATTRS =
["policy_group_ids", "default_tag", "send_invites", "landing_page_id", "login_provider_ids"]
TYPE =
[:select, :text, :date, :number, :phone_number, :boolean, :multiple]
FORMAT =
[:csv, :xml, :json]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema: nil) ⇒ PersonParser

Returns a new instance of PersonParser.

Examples:

Example of usage:

person_parser = PersonParser.new(schema: schema)
person_parser.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

Parameters:

  • schema (Ecoportal::API::V1::PersonSchema, nil) (defaults to: nil)

    schema of person details that this parser will be based upon.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/eco/api/common/people/person_parser.rb', line 34

def initialize(schema: nil)
  raise "Constructor needs a PersonSchema. Given: #{schema}" if schema && !schema.is_a?(Ecoportal::API::V1::PersonSchema)
  @details_attrs = []
  @parsers       = {}
  @patch_version = 0

  if schema
    @schema = Ecoportal::API::Internal::PersonSchema.new(JSON.parse(schema.doc.to_json))
    @details_attrs  = @schema&.fields.map { |fld| fld.alt_id }
  end

  @all_attrs = CORE_ATTRS +  + @details_attrs
end

Instance Attribute Details

#all_attrsArray<String> (readonly)

all the internal name attributes, including core, account and details.

Returns:

  • (Array<String>)

    the current value of all_attrs



11
12
13
# File 'lib/eco/api/common/people/person_parser.rb', line 11

def all_attrs
  @all_attrs
end

#defined_attrsArray<String> (readonly)

Note:
  • it excludes any parser that is not in the model, such as type parsers (i.e. :boolean, :multiple)
  • the list is sorted according CORE_ATTRS + ACCOUNT_ATTRS + schema attrs

Returns a list of all the internal attributes of the model that have a parser defined.

Returns:

  • (Array<String>)

    list of all attribute defined parsers.



90
91
92
# File 'lib/eco/api/common/people/person_parser.rb', line 90

def defined_attrs
  @defined_attrs
end

#details_attrsArray<String> (readonly)

internal names of schema details attributes.

Returns:

  • (Array<String>)

    the current value of details_attrs



11
12
13
# File 'lib/eco/api/common/people/person_parser.rb', line 11

def details_attrs
  @details_attrs
end

#patch_versionObject (readonly)

Returns the value of attribute patch_version.



20
21
22
# File 'lib/eco/api/common/people/person_parser.rb', line 20

def patch_version
  @patch_version
end

#schemaEcoportal::API::V1::PersonSchema? (readonly)

schema of person details that this parser will be based upon.

Returns:

  • (Ecoportal::API::V1::PersonSchema, nil)

    the current value of schema



11
12
13
# File 'lib/eco/api/common/people/person_parser.rb', line 11

def schema
  @schema
end

Instance Method Details

#active_attrs(source_data) ⇒ Array<String>

Returns a list of all the internal attributes of the model that have a parser defined & that should be active.

Parameters:

  • (Hash, Array<String>)

Returns:

  • (Array<String>)

    list of all attribute defined parsers that should be active.



99
100
101
# File 'lib/eco/api/common/people/person_parser.rb', line 99

def active_attrs(source_data)
  defined_attrs.select {|attr| @parsers[attr].parser_active?(source_data)}
end

#define_attribute(attr, dependencies: {}) {|parser| ... } ⇒ Eco::API::Common::People::PersonParser

Helper to define and associate a parser/serializer to a type or attribute.

Parameters:

  • attr (String)

    type (Symbol) or attribute (String) to define the parser/serializer to.

  • dependencies (Hash) (defaults to: {})

    dependencies to be used when calling the parser/serializer.

Yields:

  • (parser)

    the definition of the parser.

Yield Parameters:

Returns:

Raises:

  • (Exception)

    if trying to define a parser/serializer for:

    • an unkown attribute (String)
    • an unrecognized type or format (Symbol)


138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/eco/api/common/people/person_parser.rb', line 138

def define_attribute(attr, dependencies: {}, &definition)
  if !valid?(attr)
    msg = "The attribute '#{attr_to_str(attr)}' is not part of core, account or target schema, or does not match any type: #{@details_attrs}"
    raise msg
  end
  Eco::API::Common::People::PersonAttributeParser.new(attr, dependencies: dependencies).tap do |parser|
    @parsers[attr] = parser
    definition.call(parser)
  end
  patched!
  self
end

#defined?(attr) ⇒ Boolean

Returns true if the attribute attr has parser defined, and false otherwise.

Parameters:

  • attr (String)

    internal name of an attribute.

Returns:

  • (Boolean)

    true if the attribute attr has parser defined, and false otherwise.



112
113
114
# File 'lib/eco/api/common/people/person_parser.rb', line 112

def defined?(attr)
  @parsers.key?(attr)
end

#merge(parser) ⇒ Eco::API::Common::People::PersonParser

Note:

if there are parsers with same name, it overrides the ones of the current object with them.

Helper to merge a set of parsers of another PersonParser into the current object.

Parameters:

Returns:



120
121
122
123
124
125
126
# File 'lib/eco/api/common/people/person_parser.rb', line 120

def merge(parser)
  return self if !parser
  raise "Expected a PersonParser object. Given #{parser}" if !parser.is_a?(PersonParser)
  @parsers.merge!(parser.hash)
  patched!
  self
end

#parse(attr, source, deps: {}) ⇒ Any

Note:

dependencies introduced on parse call will be merged with those defined during the initialization of the parser attr.

Call to parser source value of attribute or type attr into an internal valid value.

Parameters:

  • attr (String)

    target attribute or type to parse.

  • source (Any)

    source value to be parsed.

  • deps (Hash) (defaults to: {})

    key-value pairs of call dependencies.

Returns:

  • (Any)

    a valid internal value.

Raises:

  • (Exception)

    if there is no parser for attribute or type attr.



159
160
161
162
# File 'lib/eco/api/common/people/person_parser.rb', line 159

def parse(attr, source, deps: {})
  raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr)
  @parsers[attr].parse(source, dependencies: deps)
end

#patched!Object



48
49
50
# File 'lib/eco/api/common/people/person_parser.rb', line 48

def patched!
  @patch_version += 1
end

#serialize(attr, object, deps: {}) ⇒ Object

Note:

dependencies introduced on serialise call will be merged with those defined during the initialization of the parser/serialiser attr.

Call to serialise object value of attribute or type attr into an external valid value.

Parameters:

  • attr (String)

    target attribute or type to serialize.

  • object (Any)

    object value to be serialized.

  • deps (Hash) (defaults to: {})

    key-value pairs of call dependencies.

Returns:

  • a valid external value.

Raises:

  • (Exception)

    if there is no serialiser for attribute or type attr.



172
173
174
175
# File 'lib/eco/api/common/people/person_parser.rb', line 172

def serialize(attr, object, deps: {})
  raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr)
  @parsers[attr].serialize(object, dependencies: deps)
end

#target_attrs_account(source_attrs = nil) ⇒ Array<String>

Note:

use this helper to know which among your attributes are account ones.

Scopes source_attrs using the schema account attributes.

Parameters:

  • source_attrs (Array<String>) (defaults to: nil)

Returns:

  • (Array<String>)

    the scoped account attributes, if source_attrs is not nil. All the account attributes, otherwise.



74
75
76
77
# File 'lib/eco/api/common/people/person_parser.rb', line 74

def (source_attrs = nil)
  return  if !source_attrs
  scoped_attrs(source_attrs, )
end

#target_attrs_core(source_attrs = nil) ⇒ Array<String>

Note:

use this helper to know which among your attributes are core ones.

Scopes source_attrs using the core attributes.

Parameters:

  • source_attrs (Array<String>) (defaults to: nil)

Returns:

  • (Array<String>)

    the scoped core attributes, if source_attrs is not nil. All the core attributes, otherwise.



56
57
58
59
# File 'lib/eco/api/common/people/person_parser.rb', line 56

def target_attrs_core(source_attrs = nil)
  return CORE_ATTRS if !source_attrs
  scoped_attrs(source_attrs, CORE_ATTRS)
end

#target_attrs_details(source_attrs = nil) ⇒ Array<String>

Note:

use this helper to know which among your attributes are schema details ones.

Scopes source_attrs using the schema details attributes.

Parameters:

  • source_attrs (Array<String>) (defaults to: nil)

Returns:

  • (Array<String>)

    the scoped details attributes, if source_attrs is not nil. All the details attributes, otherwise.



65
66
67
68
# File 'lib/eco/api/common/people/person_parser.rb', line 65

def target_attrs_details(source_attrs = nil)
  return @details_attrs if !source_attrs
  scoped_attrs(source_attrs, @details_attrs)
end

#undefined_attrsArray<String>

Note:

it excludes any parser that is not in the model, such as type parsers (i.e. :boolean, :multiple)

Returns a list of all the internal attributes of the model that do not have a parser defined.

Returns:

  • (Array<String>)

    list of all attributes without a defined parser.



106
107
108
# File 'lib/eco/api/common/people/person_parser.rb', line 106

def undefined_attrs
  all_attrs - defined_attrs
end