lib/eco/api/common/loaders/parser.rb in eco-helpers-2.6.4 vs lib/eco/api/common/loaders/parser.rb in eco-helpers-2.7.0

- old
+ new

@@ -2,11 +2,11 @@ module API module Common module Loaders class Parser < Eco::API::Common::Loaders::CaseBase # Helper class to scope what required attributes it depends on - class RequiredAttrs < Struct.new(:attr, :type, :attrs) + RequiredAttrs = Struct.new(:attr, :type, :attrs) do def active?(*input_attrs) missing(*input_attrs).empty? end def dependant?(attr) @@ -31,22 +31,28 @@ # 1. when `String`: the internal name of the field/attribute this parser/serializer manages for. # 2. when `Symbol`: the type of data this parser converts between `String` and the specific type. # @return [String] the `type` of usecase (i.e. `:sync`, `:transform`, `:import`, `:other`) def attribute(value = nil) unless value - return @attribute || raise("You should specify the 'attribute' this parser/serializer, #{self.class}, is linked to") + msg = "You should specify the 'attribute' this parser/serializer, " + msg << "#{self.class}, is linked to" + return @attribute || (raise msg) end name value @attribute = value end # Some parsers require dependencies to do their job. def dependencies(**value) @dependencies ||= {} - return @dependencies.merge({ - required_attrs: @active_when_attrs - }) unless !value.empty? + + if value.empty? + return @dependencies.merge({ + required_attrs: @active_when_attrs + }) + end + raise "Expected Hash. Given: '#{value.class}'" unless value.is_a?(Hash) @dependencies.merge!(value) end # Define or get the `phase` that the `parser` kicks in. @@ -68,62 +74,68 @@ end # Helper to build the `active_when` condition. def active_when_any(*attrs) @active_when_attrs = RequiredAttrs.new(attribute, :any, attrs) - @active_when = Proc.new do |source_data| + @active_when = proc do |source_data| keys = data_keys(source_data) attrs.any? {|key| keys.include?(key)} end end # Helper to build the `active_when` condition. def active_when_all(*attrs) @active_when_attrs = RequiredAttrs.new(attribute, :all, attrs) - @active_when = Proc.new do |source_data| + @active_when = proc do |source_data| keys = data_keys(source_data) attrs.all? {|key| keys.include?(key)} end end private # Helper to obtain the current internal named attributes of the data - # @param source_data [Array<String>, Hash] if `Array` those are already the `keys`, if `Hash` it gets the `keys` + # @param source_data [Array<String>, Hash] if `Array` those + # are already the `keys`, if `Hash` it gets the `keys` # @return [Array<String>] `keys` of `source_data` def data_keys(source_data) case source_data when Array - keys = source_data + source_data when Hash - keys = source_data.keys + source_data.keys else - keys = [] + [] end end end inheritable_class_vars :attribute, :parsing_phase, :serializing_phase - def initialize(person_parser) - raise "Expected Eco::API::Common::People::PersonParser. Given #{policies.class}" unless person_parser.is_a?(Eco::API::Common::People::PersonParser) - person_parser.define_attribute(self.attribute, dependencies: self.class.dependencies) do |attr_parser| + def initialize(person_parser) # rubocop:disable Lint/MissingSuper + msg = "Expected Eco::API::Common::People::PersonParser. Given #{person_parser.class}" + raise msg unless person_parser.is_a?(Eco::API::Common::People::PersonParser) + + person_parser.define_attribute(attribute, dependencies: self.class.dependencies) do |attr_parser| _define_parser(attr_parser) _define_serializer(attr_parser) end end # @param data [Hash] all the person data at the specified `parsing_phase`: - # - when `:internal`: the parser will receive external types (i.e. String with '|' delimiters instead of an Array). - # - when `:final`: the parser will receive the typed values (i.e. Array instread of String with '|' delimiters). + # - when `:internal`: the parser will receive external types + # (i.e. String with '|' delimiters instead of an Array). + # - when `:final`: the parser will receive the typed values + # (i.e. Array instread of String with '|' delimiters). # @param deps [Hash] the merged dependencies (default to the class object and when calling the parser). - def parser(data, deps) + def parser(_data, _deps) raise "You should implement this method" end # @param data [Hash, Ecoportal::API::V1::Person] all the person data at the specified `serializing_phase`: - # - when `:internal`: it will receive a `Hash` with the **internal values** but the types already serialized to `String`. + # - when `:internal`: it will receive a `Hash` with the **internal values** but the types already serialized + # to `String`. # - when `:final`: it will receive a `Hash` with the **internal values** and **types**. # - when `:person`: it will receive the `person` object. # @param deps [Hash] the merged dependencies (default to the class object and when calling the parser). # def serializer(data, deps) # raise "You should implement this method" @@ -135,19 +147,29 @@ end private def _define_parser(attr_parser) - if active_when = self.class.active_when - attr_parser.def_parser(self.class.parsing_phase, active_when: active_when, &self.method(:parser)) + if (active_when = self.class.active_when) + attr_parser.def_parser( + self.class.parsing_phase, + active_when: active_when, + &method(:parser) + ) else - attr_parser.def_parser(self.class.parsing_phase, &self.method(:parser)) + attr_parser.def_parser( + self.class.parsing_phase, + &method(:parser) + ) end end def _define_serializer(attr_parser) return unless respond_to?(:serializer, true) - attr_parser.def_serializer(self.class.serializing_phase, &self.method(:serializer)) + attr_parser.def_serializer( + self.class.serializing_phase, + &method(:serializer) + ) end end end end end