Sha256: f9e5bc20e90983c4e5053cbe27bf6538756644de66301226bba6c3585b1ca690

Contents?: true

Size: 1.97 KB

Versions: 15

Compression:

Stored size: 1.97 KB

Contents

module Eco
  module API
    module Common
      module People

        # Class to define a parser/serializer.
        class PersonAttributeParser < Eco::Language::Models::ParserSerializer

          # @see Eco::Language::Models::ParserSerializer#def_parser
          # @note
          #  - additionally, you can declare a callback `active:` to determine if when the
          # parser will be active/used.
          #  - this is important to help avoiding to set values that are not present in the input entry.
          #  - if you have doubts about using it or not, do not use it.
          # @param active_when [Proc] that expects a list of internal attributes or the internal entry itself
          # By default, an attribute paraser is active if in the entry to parse the internal attribute is present.
          def def_parser(active_when: nil, &block)
            @active_when = attribute_present(active_when)
            super(&block)
          end

          # Determines if for a given source data to parse, this parser should be active or not
          # @return [Boolean] `true` if there's no callback defined or there is and evaluates `true`
          # when for the current data to parse. It returns `false` otherwise.
          def parser_active?(source_data)
            @active_when.call(source_data)
          end

          private

          # by default, an attribute paraser is active if in the entry to parse
          # the internal attribute is present
          def attribute_present(active_when)
            Proc.new do |source_data|
              case source_data
              when Array
                keys = source_data
              when Hash
                keys = source_data.keys
              else
                keys = []
              end

              (source_data && keys.include?(self.attr)) ||
              (active_when && active_when.call(source_data))
            end
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
eco-helpers-1.0.13 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.12 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.11 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.10 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.9 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.8 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.7 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.6 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.5 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.4 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.3 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-0.9.5 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-1.0.2 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-0.9.4 lib/eco/api/common/people/person_attribute_parser.rb
eco-helpers-0.9.3 lib/eco/api/common/people/person_attribute_parser.rb