lib/eco/api/common/people/person_parser.rb in eco-helpers-0.9.2 vs lib/eco/api/common/people/person_parser.rb in eco-helpers-0.9.3

- old
+ new

@@ -41,16 +41,10 @@ end @all_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs end - # Lists all defined attributes, types and formats. - # @return [Array<String>] the list of defined parsers/serializers. - def list_defined - @parsers.keys - 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) @@ -74,18 +68,30 @@ def target_attrs_account(source_attrs = nil) return ACCOUNT_ATTRS if !source_attrs scoped_attrs(source_attrs, ACCOUNT_ATTRS) end + # Lists all defined attributes, types and formats. + # @return [Array<String>] the list of defined parsers/serializers. + #def list_defined + # @parsers.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`) + # @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 attribute defined parsers. def defined_attrs defined = @parsers.keys defined - (defined - all_attrs) end + # Returns a list of all the internal attributes of the model that have a parser defined & that should be active. + # @return [Array<String>] list of all attribute defined parsers that should be active. + def active_attrs(source_data) + defined_attrs.select {|attr| @parsers[attr].parser_active?(source_data)} + 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 @@ -121,12 +127,12 @@ 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.new(attr, dependencies: dependencies).tap do |parser| + # Eco::Language::Models::ParserSerializer + Eco::API::Common::People::PersonAttributeParser.new(attr, dependencies: dependencies).tap do |parser| @parsers[attr] = parser definition.call(parser) end self @@ -167,10 +173,10 @@ private def scoped_attrs(source_attrs, section_attrs) direct_attrs = source_attrs & section_attrs - parsed_attrs = @parsers.keys & section_attrs + parsed_attrs = active_attrs(source_attrs) & section_attrs (source_attrs + parsed_attrs) & (direct_attrs + parsed_attrs) end def attr_to_str(attr) attr.is_a?(Symbol)? ":#{attr.to_s}" : "#{attr.to_s}"