Sha256: 47a54c7cc131b2083852a4be58109c9ba20cbe10289d5458aced120927dbfd9d

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module ExperianConsumerView
  module Transformers
    module Attributes
      # Base mpdule for Attribute Transformers.
      # Mixin to Attribute Transformer classes with +extend ExperianConsumerView::Transformers::Attributes::Base+.
      #
      # Expects the class to provide two constants:
      # - +ATTRIBUTE_NAME+ - the name of the attribute, as returned by the ConsumerView API, which the class can
      #     transform.
      # - +CODE_MAP+ - a hash whose keys are all the String codes which the ConsumerView API may return for the
      #     attribute in question, and whose values are the what the attribute should be mapped to when the matching
      #     code is returned.
      #
      # This module will then provide two class-level methods:
      # - +attribute_name+ - simply returns the value of the +ATTRIBUTE_NAME+ constant.
      # - +transform_attribute+ - transforms the given +value+ based on the +CODE_MAP+, or raises a
      #     +AttributeValueUnrecognisedError+ if the value is not foung in the +CODE_MAP+.
      module Base
        def attribute_name
          self::ATTRIBUTE_NAME
        end

        def transform_attribute(value)
          return self::CODE_MAP[value] if self::CODE_MAP[value]

          raise ExperianConsumerView::Errors::AttributeValueUnrecognisedError
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
experian_consumer_view-1.2.0 lib/experian_consumer_view/transformers/attributes/base.rb
experian_consumer_view-1.1.0 lib/experian_consumer_view/transformers/attributes/base.rb
experian_consumer_view-1.0.0 lib/experian_consumer_view/transformers/attributes/base.rb