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

Inherits:
Object
  • Object
show all
Extended by:
ClassAutoLoader
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", "contractor_organization_id", "freemium"]
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, :xls]

Instance Attribute Summary collapse

Scopping attributes (identifying, presence & active) collapse

Defining attributes collapse

Launching parser/serializer collapse

Instance Method Summary collapse

Methods included from ClassAutoLoader

_autoload_namespace, autoload_children, autoload_class?, autoload_namespace, autoload_namespace_ignore, autoloaded_children, autoloaded_class, autoloaded_namespaces, autoloads_children_of, known_class!, known_classes, new_classes, unloaded_children

Methods included from ClassHelpers

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

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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eco/api/common/people/person_parser.rb', line 37

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_model_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs
  self.class.autoload_children(self)
end

Instance Attribute Details

#all_model_attrsArray<String> (readonly)

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

Returns:

  • (Array<String>)

    the current value of all_model_attrs



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

def all_model_attrs
  @all_model_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.



23
24
25
# File 'lib/eco/api/common/people/person_parser.rb', line 23

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, phase = :any, process: :parse) ⇒ Array<String>

Returns a list of all the internal attributes of the model that have a parser defined & that should be active. Can be [:internal, :final, :person]

Parameters:

  • source_data (Hash, Array<String>)

    the data that we scope for parsing

  • phase (Symbol) (defaults to: :any)

    the phase when the attr parser is expected to be called.

  • process (Symbol) (defaults to: :parse)

    either :parse or :serialize, depending if we want to parse or serialize the attr.

Returns:

  • (Array<String>)

    list of all attribute defined parsers that should be active for the given source_data.



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

def active_attrs(source_data, phase = :any, process: :parse)
  defined_model_attrs.select do |attr|
    if process == :serialize
      @parsers[attr].serializer_active?(phase)
    else
      @parsers[attr].parser_active?(source_data, phase)
    end
  end
end

#all_attrs(include_defined_parsers: false) ⇒ Object

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



68
69
70
71
# File 'lib/eco/api/common/people/person_parser.rb', line 68

def all_attrs(include_defined_parsers: false)
  return all_model_attrs | defined_model_attrs if include_defined_parsers
  all_model_attrs
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 unknown attribute (String)
    • an unrecognized type or format (Symbol)


184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/eco/api/common/people/person_parser.rb', line 184

def define_attribute(attr, dependencies: {}, &definition)
  unless 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.



156
157
158
# File 'lib/eco/api/common/people/person_parser.rb', line 156

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

#defined_attrsArray<String>

Note:

These attributes do not necessarily belong to the model. They could be virtual attributes

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

Returns:

  • (Array<String>)

    list of all attribute defined parsers.



109
110
111
# File 'lib/eco/api/common/people/person_parser.rb', line 109

def defined_attrs
  defined_list - symbol_keys
end

#defined_listArray<String>

Lists all defined attributes, types and formats.

Returns:

  • (Array<String>)

    the list of defined parsers/serializers.



102
103
104
# File 'lib/eco/api/common/people/person_parser.rb', line 102

def defined_list
  @parsers.keys
end

#defined_model_attrsArray<String>

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 in the model.



118
119
120
121
122
# File 'lib/eco/api/common/people/person_parser.rb', line 118

def defined_model_attrs
  defined     = @parsers.keys
  defined     = (all_model_attrs | defined) & defined
  defined - symbol_keys
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:



167
168
169
170
171
172
173
# File 'lib/eco/api/common/people/person_parser.rb', line 167

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

#new(schema: nil) ⇒ Object



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

def new(schema: nil)
  self.class.new(schema: schema || self.schema).merge(self)
end

#parse(attr, source, phase = :internal, 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.

  • phase (Symbol) (defaults to: :internal)

    the phase when the attr parser is expected to be called. Must be [:internal, :final]

  • 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.



210
211
212
213
# File 'lib/eco/api/common/people/person_parser.rb', line 210

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

#patched!Object



52
53
54
# File 'lib/eco/api/common/people/person_parser.rb', line 52

def patched!
  @patch_version += 1
end

#required_attrsArray<Eco::API::Common::Loaders::Parser::RequiredAttrs>



63
64
65
# File 'lib/eco/api/common/people/person_parser.rb', line 63

def required_attrs
  @parsers.values_at(*all_attrs(include_defined_parsers: true)).compact.map(&:required_attrs).compact
end

#serialize(attr, object, phase = :person, 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.

  • phase (Symbol) (defaults to: :person)

    the phase when the attr serializer is expected to be called. Must be [:internal, :final, :person]

  • 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.



225
226
227
228
# File 'lib/eco/api/common/people/person_parser.rb', line 225

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

#symbol_keysArray<Symbol>

Note:

this was introduced to boost virtual fields to treat in different phases of the parsing process

Symbol keys are type or import parsers (that do not belong to the model)

Returns:

  • (Array<Symbol>)

    all the parsers defined as Symbol



127
128
129
# File 'lib/eco/api/common/people/person_parser.rb', line 127

def symbol_keys
  @parsers.keys.select {|k| k.is_a?(Symbol)}
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.



95
96
97
98
# File 'lib/eco/api/common/people/person_parser.rb', line 95

def (source_attrs = nil)
  return ACCOUNT_ATTRS if !source_attrs
  scoped_attrs(source_attrs, ACCOUNT_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.



77
78
79
80
# File 'lib/eco/api/common/people/person_parser.rb', line 77

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.



86
87
88
89
# File 'lib/eco/api/common/people/person_parser.rb', line 86

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

#undefined_model_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.



150
151
152
# File 'lib/eco/api/common/people/person_parser.rb', line 150

def undefined_model_attrs
  all_model_attrs - defined_model_attrs
end