module Eco module API module Common module People # @attr_reader direct_attrs [Array] only those # internal attributes present in the person entry that # do **not** have an internal/external name mapping. class PersonEntryAttributeMapper include Eco::Language::AuxiliarLogger DEBUG = false @@cached_warnings = {} # rubocop:disable Style/ClassVars attr_reader :direct_attrs # Helper class tied to `PersonEntry` that allows to track which attributes of a person entry are present # and how they should be mapped between internal and external names if applicable. # This class is meant to help in providing a common interface to access entries of source # data that come in different formats. # @note # - if `data` is a `Person` object, its behaviour is `serialise`. # - if `data` is **not** a `Person` object, it does a `parse`. # - currently **in rework**, so there may be subtle differences that make it # temporarily unstable (yet it is reliable). # @param data [Hash, Ecoportal::API::V1::Person] `Person` object to # be serialized or hashed entry to be parsed (note: `CSV::Row` is accepted). # @param person_parser [Common::People::PersonParser] parser/serializer # of person attributes (it contains a set of attribute parsers). # @param attr_map [Eco::Data::Mapper] mapper to translate attribute names # from _external_ to _internal_ names and _vice versa_. # @param logger [Common::Session::Logger, ::Logger] object to manage logs. def initialize(data, person_parser:, attr_map:, logger: ::Logger.new(IO::NULL)) msg = "Constructor needs a PersonParser. Given: #{person_parser.class}" raise ArgumentError, msg unless person_parser.is_a?(Eco::API::Common::People::PersonParser) msg = "Expecting Mapper object. Given: #{attr_map.class}" raise ArgumentError, msg if attr_map && !attr_map.is_a?(Eco::Data::Mapper) @source = data @person_parser = person_parser @attr_map = attr_map @logger = logger if parsing? @external_entry = data else # SERIALIZING @person = data end end # @return [Array] only those internal attributes # present in the person entry that have an internal/external # name mapping. def aliased_attrs return @aliased_attrs if @aliased_attrs if parsing? init_attr_trackers else @aliased_attrs = @attr_map.list(:internal) end @aliased_attrs end # @return [Array] all the internally named attributes that the person entry has. def internal_attrs(data = nil) return @internal_attrs unless data || !@internal_attrs if parsing? init_attr_trackers unless @internal_attrs return data.keys & @person_parser.all_model_attrs if data else @internal_attrs = @person_parser.all_model_attrs end @internal_attrs end # @return [Array] all the attrs that are present in the person entry. def all_model_attrs(data = nil) core_attrs(data) | account_attrs(data) | details_attrs(data) end # @return [Array] core attributes that are present in the person entry. def core_attrs(data = nil) return @core_attrs unless data || !@core_attrs @person_parser.target_attrs_core(internal_attrs(data)).tap do |core_attrs| @core_attrs ||= core_attrs end end # @return [Array] schema details attributes that are present in the person entry. def details_attrs(data = nil) return @details_attrs unless data || !@details_attrs @person_parser.target_attrs_details(internal_attrs(data)).tap do |details_attrs| @details_attrs ||= details_attrs end end # @return [Array] account attributes that are present in the person entry. def account_attrs(data = nil) return @account_attrs unless data || !@account_attrs @person_parser.target_attrs_account(internal_attrs(data)).tap do |account_attrs| @account_attrs ||= account_attrs end end # To know if currently the object is in parse or serialize mode. # @return [Boolean] returns `true` if we are **parsing**, `false` otherwise. def parsing? !@source.is_a?(Ecoportal::API::V1::Person) end # To know if currently the object is in parse or serialize mode. # @return [Boolean] returns `true` if we are **serializing**, `false` otherwise. def serializing? !parsing? end # If there **no** `mapper` defined for the object, it mirrors `value`. # If there is a `mapper` defined for the object: # 1. if the `value` exists as external, translates it into an internal one. # 2. if it doesn't exist, returns `nil`. # @note # 1. the **scope of attributes** is based on all the attributes recognized by the person parser. # 2. the attributes recognized by the person parser are those of of the `Person` model # (where details attributes depend on the `schema`). # @param value [String, Array] value(s) to be translated into internal names. # @return [String, nil, Array when attr_map is avoided, it doesn't work as it should return value unless @attr_map attr = value case value when Array return value.map do |v| to_internal(v) end.compact when String if @attr_map.external?(value) attr = @attr_map.to_internal(value) elsif @attr_map.external?(value.strip) unless cached_warning("external", "spaces", value) msg = "The external person field name '#{value}' contains " msg << "additional spaces in the reference file" log(:warn) { msg } end attr = @attr_map.to_internal(value.strip) elsif [value, value.strip, value.strip.downcase].any? {|val| @attr_map.internal?(val)} unless cached_warning("external", "reversed", value) msg = "The mapper [external, internal] attribute names " msg << "may be declared reversedly for EXTERNAL attribute: '#{value}'" log(:info) { msg } end end end return unless @person_parser.all_model_attrs.include?(attr) attr end # Serializing helper also used to do a reverse mapping when parsing: # - as there could be _internal attributes_ that shared _external attributes_, # - when parsing, you use this helper to recognize the source _external attribute_ of each _internal_ one. # If there **no** `mapper` defined for the object, it mirrors `value`. # If there is a `mapper` defined for the object: # 1. if the `value` exists as _internal-, translates it into an _external_ one. # 2. if it doesn't exist, returns `nil`. # @note # 1. the **scope of attributes** is based on all the attributes defined in the current entry. # 2. the attributes recognized by the person parser are those of of the `Person` model # (where details attributes depend on the `schema`). # @param value [String, Array] value(s) to be translated or aliased into external ones. # @return [String, nil, Array WARNING: this includes ext direct attrs that may be aliased to other int attrs data_direct_attrs_raw = data_attrs & def_all_attrs # (data) direct (int) ext data attrs mapped data_direct_mapped = data_direct_attrs_raw.select { |attr| external_attr?(attr) } # (data) direct (int) ext data attrs mapped to themselves data_direct_self_mapped = data_direct_attrs_raw.select { |attr| self_mapped_attr?(attr) } # (data) direct (int) ext data attrs mapped only to another attr data_direct_renamed = data_direct_mapped - data_direct_self_mapped # (data) attributes of the data that come directly as internal attribute names data_direct_attrs = data_direct_attrs_raw - data_direct_renamed debug( data_direct_attrs, "(data_direct_attrs) attributes of the data that come directly as internal attribute names (data_direct_attrs_raw - data_direct_renamed)" # rubocop:disable Layout/LineLength ) # (def) configured as alised (int <-> ext attrs) + accept/include int attrs as ext attrs def_int_aliased_raw = def_all_attrs.select { |attr| to_external(attr) } # (def) aliasable int attrs of the input data (excludes int attrs direct as ext attrs that got renamed) def_int_aliased = def_int_aliased_raw - data_direct_renamed # (def) ext attrs of the data's aliasable int attrs (def_int_aliased) def_ext_alias = def_int_aliased.map { |attr| to_external(attr) } # (def) those ext attrs that map to multiple int attrs # def_ext_multi_alias = def_ext_alias.detect { |attr| def_ext_alias.count(attr) > 1 } # (def) those ext attrs that are direct, mapt to themselves and some other # (data) virtual attrs (external alias of non native internal attr in data): data_vi_ext_alias = data_attrs.select do |attr| !def_ext_alias.include?(attr) && @attr_map&.external?(attr) end # (data) virtual internal attrs (the internal names of those virtual attrs) data_vi_int_aliased = data_vi_ext_alias.map do |attr| # to_internal(attr) can't be used here, becauase virtual fields would get filtered out, # as they are not recognized by @parser.all_model_attrs.include?(attr) @attr_map.to_internal(attr) end.compact # (data) int attrs that come aliased in the current data # => modify aliased based on those that came directly as internal attrs in the entry data_def_int_aliased = (def_int_aliased - data_direct_attrs) | data_direct_self_mapped data_def_ext_alias = data_def_int_aliased.map { |attr| to_external(attr) } # (data) ext attrs of the data that come aliased data_ext_alias = data_def_ext_alias & data_attrs # (data) all internal attributes that come aliased, with given the entry aliased_attrs = data_def_int_aliased | data_vi_int_aliased # render in the order that defines the model @aliased_attrs = (def_all_attrs & aliased_attrs) | (aliased_attrs - def_all_attrs) debug( data_vi_int_aliased, "(data_vi_int_aliased) virtual internal attrs (internal names of those virtual attrs)" ) debug( def_int_aliased, "(def_int_aliased) aliasable int attrs of the input data (excludes int attrs direct as ext attrs that got renamed)" # rubocop:disable Layout/LineLength ) debug( def_ext_alias, "(def_ext_alias) ext attrs of the data's aliasable int attrs (def_int_aliased)" ) debug( data_def_int_aliased, "(data_def_int_aliased) int attrs that come aliased in the current data ((def_int_aliased - data_direct_attrs) | data_direct_self_mapped)" # rubocop:disable Layout/LineLength ) debug( data_ext_alias, "(data_ext_alias) ext attrs of the data that come aliased (data_def_ext_alias & data_attrs)" ) debug( aliased_attrs, "(aliased_attrs) all internal attributes that come aliased, with given the entry (data_def_int_aliased | data_vi_int_aliased)" # rubocop:disable Layout/LineLength ) # (data) all those ext attrs present that will require aliasing #data_ext_alias_all = data_def_ext_alias + data_vi_ext_alias data_ext_alias_all = data_ext_alias | data_vi_ext_alias debug( data_ext_alias_all, "(data_ext_alias_all) all those ext attrs present that will require aliasing (data_ext_alias | data_vi_ext_alias)" # rubocop:disable Layout/LineLength ) # those that are direct external to internal: data_ext_direct = data_attrs - data_ext_alias_all # (data) attributes that do not require aliasing # to avoid collisions between internal names: direct_attrs = data_ext_direct # render in the order that defines the model @direct_attrs = (def_all_attrs & direct_attrs) | (direct_attrs - def_all_attrs) # (data) attributes that are being aliased internal_attrs = @aliased_attrs | @direct_attrs # render in the order that defines the model @internal_attrs = (def_all_attrs & internal_attrs) | (internal_attrs - def_all_attrs) debug( @direct_attrs, "(@direct_attrs) attributes that do not require aliasing (data_attrs - data_ext_alias_all)" ) debug( @internal_attrs, "(@internal_attrs) aliased_attrs | @direct_attrs" ) end def attributes(value) case value when CSV::Row value&.headers when Hash value&.keys when PersonEntry @person_parser.target_attrs_core else [] end end # LOGGER def logger @logger || ::Logger.new(IO::NULL) end def cached_warning(*args) unless (exists = !!@@cached_warnings.dig(*args)) # rubocop:disable Style/DoubleNegation args.reduce(@@cached_warnings) do |cache, level| cache[level] = {} unless cache.key?(level) cache[level] end end exists end def fatal(msg) log(:fatal) { msg } raise msg end def debug(var, msg) return unless DEBUG puts "\n • #{msg}:" pp var end end end end end end