lib/eco/api/common/people/person_parser.rb in eco-helpers-2.0.9 vs lib/eco/api/common/people/person_parser.rb in eco-helpers-2.0.10
- old
+ new
@@ -5,11 +5,11 @@
# Class to define/group a set of parsers/serializers.
#
# @attr_reader schema [Ecoportal::API::V1::PersonSchema, nil] schema of person details that this parser will be based upon.
# @attr_reader details_attrs [Array<String>] internal names of schema details attributes.
- # @attr_reader all_attrs [Array<String>] all the internal name attributes, including _core_, _account_ and _details_.
+ # @attr_reader all_model_attrs [Array<String>] all the internal name attributes, including _core_, _account_ and _details_.
class PersonParser
extend Eco::API::Common::ClassAutoLoader
autoloads_children_of "Eco::API::Common::Loaders::Parser"
autoload_namespace_ignore "Eco::API"
@@ -17,12 +17,11 @@
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]
attr_reader :schema
- attr_reader :details_attrs, :all_attrs
- attr_reader :defined_attrs
+ attr_reader :details_attrs, :all_model_attrs
attr_reader :patch_version
# @example Example of usage:
# person_parser = PersonParser.new(schema: schema)
# person_parser.define_attribute("example") do |parser|
@@ -44,11 +43,11 @@
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 + ACCOUNT_ATTRS + @details_attrs
+ @all_model_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs
self.class.autoload_children(self)
end
def patched!
@patch_version += 1
@@ -58,10 +57,15 @@
self.class.new(schema: schema || self.schema).merge(self)
end
# @!group Scopping attributes (identifying, presence & active)
+ # All the internal name attributes, including _core_, _account_ and _details_.
+ def all_attrs(include_defined_parsers: false)
+ all_model_attrs | defined_model_attrs
+ end
+
# Scopes `source_attrs` using the _**core** attributes_.
# @note use this helper to know which among your attributes are **core** ones.
# @param source_attrs [Array<String>]
# @return [Array<String>] the scoped **core** attributes, if `source_attrs` is not `nil`. All the _core attributes_, otherwise.
def target_attrs_core(source_attrs = nil)
@@ -91,18 +95,25 @@
# @return [Array<String>] the list of defined parsers/serializers.
def defined_list
@parsers.keys
end
+ # Returns a list of all the internal attributes that have a parser defined.
+ # @note These attributes do not necessarily belong to the model. They could be virtual attributes
+ # @return [Array<String>] list of all attribute defined parsers.
+ def defined_attrs
+ defined_list - symbol_keys
+ end
+
# Returns a list of all the internal attributes of the model that have a parser defined.
# @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
- # @return [Array<String>] list of all attribute defined parsers.
- def defined_attrs
+ # @return [Array<String>] list of all attribute defined parsers in the model.
+ def defined_model_attrs
defined = @parsers.keys
- defined = (all_attrs | defined) & defined
+ defined = (all_model_attrs | defined) & defined
defined - symbol_keys
end
# Symbol keys are type or import parsers (that do not belong to the model)
# @note this was introduced to boost virtual fields to treat in different phases of the parsing process
@@ -116,11 +127,11 @@
# @param phase [Symbol] the phase when the attr parser is expected to be called.
# Can be [:internal, :final, :person]
# @param process [Symbol] either `:parse` or `:serialize`, depending if we want to parse or serialize the `attr`.
# @return [Array<String>] list of all attribute defined parsers that should be active for the given `source_data`.
def active_attrs(source_data, phase = :any, process: :parse)
- defined_attrs.select do |attr|
+ defined_model_attrs.select do |attr|
if process == :serialize
@parsers[attr].serializer_active?(phase)
else
@parsers[attr].parser_active?(source_data, phase)
end
@@ -128,12 +139,12 @@
end
# Returns a list of all the internal attributes of the model that do **not** have a parser defined.
# @note it excludes any parser that is **not** in the model, such as type parsers (i.e. :boolean, :multiple)
# @return [Array<String>] list of all attributes without a defined parser.
- def undefined_attrs
- all_attrs - defined_attrs
+ def undefined_model_attrs
+ all_model_attrs - defined_model_attrs
end
# @param attr [String] internal name of an attribute.
# @return [Boolean] `true` if the attribute `attr` has parser defined, and `false` otherwise.
def defined?(attr)
@@ -236,10 +247,10 @@
def valid?(attr)
valid_attr?(attr) || valid_type?(attr) || valid_format?(attr)
end
def valid_attr?(attr)
- attr.is_a?(String) && (!@schema || @all_attrs.include?(attr))
+ attr.is_a?(String) && (!@schema || @all_model_attrs.include?(attr))
end
def valid_type?(attr)
attr.is_a?(Symbol) && TYPE.include?(attr)
end