Sha256: 2e8ecd887146f229ed4c7802d8a80b2696b23b9bf875e54ff5cba53895123fa5

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Parametric
  # Adapt legacy policies to the new policy interface
  class PolicyAdapter
    class PolicyRunner
      def initialize(policy, key, value, payload, context)
        @policy, @key, @raw_value, @payload, @context = policy, key, value, payload, context
      end

      # The PolicyRunner interface
      # @return [Boolean]
      def eligible?
        @policy.eligible?(@raw_value, @key, @payload)
      end

      # If a policy is not #eligible?, use this to decide if its key
      # should still be included in output hash.
      #
      # @return [Boolean]
      def include_non_eligible_in_ouput?
        false
      end

      # @return [Boolean]
      def valid?
        @policy.valid?(value, @key, @payload)
      end

      # @return [Any]
      def value
        @value ||= @policy.coerce(@raw_value, @key, @context)
      end

      # @return [String]
      def message
        @policy.message
      end
    end

    def initialize(policy)
      @policy = policy
    end

    # The PolicyFactory interface
    # Buld a Policy Runner, which is instantiated
    # for each field when resolving a schema
    # @param key [Symbol]
    # @param value [Any]
    # @option payload [Hash]
    # @option context [Parametric::Context]
    # @return [PolicyRunner]
    def build(key, value, payload:, context:)
      PolicyRunner.new(@policy, key, value, payload, context)
    end

    def meta_data
      @policy.meta_data
    end

    def key
      @policy.key
    end

    def ==(other)
      key == other.key && meta_data == other.meta_data
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parametric-0.2.21 lib/parametric/policy_adapter.rb