lib/eco/api/common/people/person_parser.rb in eco-helpers-1.0.8 vs lib/eco/api/common/people/person_parser.rb in eco-helpers-1.0.9
- old
+ new
@@ -15,11 +15,12 @@
FORMAT = [:csv, :xml, :json]
attr_reader :schema
attr_reader :details_attrs, :all_attrs
attr_reader :defined_attrs
-
+ attr_reader :patch_version
+
# @example 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
@@ -31,20 +32,25 @@
# end
# @param schema [Ecoportal::API::V1::PersonSchema, nil] schema of person details that this parser will be based upon.
def initialize(schema: nil)
raise "Constructor needs a PersonSchema. Given: #{schema}" if schema && !schema.is_a?(Ecoportal::API::V1::PersonSchema)
@details_attrs = []
- @parsers = {}
+ @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 + ACCOUNT_ATTRS + @details_attrs
end
+ def patched!
+ @patch_version += 1
+ 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)
@@ -113,10 +119,11 @@
# @return [Eco::API::Common::People::PersonParser] the current object (to ease chainig).
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
# Helper to define and associate a parser/serializer to a type or attribute.
@@ -131,15 +138,14 @@
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::Language::Models::ParserSerializer
Eco::API::Common::People::PersonAttributeParser.new(attr, dependencies: dependencies).tap do |parser|
@parsers[attr] = parser
definition.call(parser)
end
-
+ patched!
self
end
# Call to parser `source` value of attribute or type `attr` into an internal valid value.
# @note dependencies introduced on `parse` call will be merged with those defined during the