Sha256: b342daf38018415ec66561c6047184cd2bfafa801f3d35f70c193d980b136bb0

Contents?: true

Size: 1.86 KB

Versions: 9

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Yoti
  module DynamicSharingService
    # Describes a wanted attribute in a dynamic sharing policy
    class WantedAttribute
      attr_reader :name
      attr_reader :derivation
      attr_reader :constraints

      def initialize
        @constraints = []
      end

      def accept_self_asserted
        return true if @accept_self_asserted

        false
      end

      def to_json(*_args)
        as_json.to_json
      end

      def as_json(*_args)
        obj = {
          name: @name
        }
        obj[:derivation] = @derivation if derivation
        obj[:accept_self_asserted] = @accept_self_asserted if accept_self_asserted
        obj[:constraints] = @constraints.map(&:as_json) unless constraints.empty?
        obj
      end

      def self.builder
        WantedAttributeBuilder.new
      end
    end

    # Builder for WantedAttribute
    class WantedAttributeBuilder
      def initialize
        @attribute = WantedAttribute.new
      end

      #
      # @param [String] name
      #
      def with_name(name)
        @attribute.instance_variable_set(:@name, name)
        self
      end

      #
      # @param [String] derivation
      #
      def with_derivation(derivation)
        @attribute.instance_variable_set(:@derivation, derivation)
        self
      end

      #
      # @param constraint Constraint to apply to the requested attribute
      #
      def with_constraint(constraint)
        @attribute.constraints.push(constraint)
        self
      end

      #
      # @param [Bool] accept
      #
      def with_accept_self_asserted(accept = true)
        @attribute.instance_variable_set(:@accept_self_asserted, accept)
        self
      end

      def build
        raise 'Attribute name missing' if @attribute.name.nil? || @attribute.name == ''

        Marshal.load Marshal.dump @attribute
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
yoti-1.9.0 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.8.0 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.7.1 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.7.0 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.6.4 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.6.3 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.6.2 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.6.1 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb
yoti-1.6.0 lib/yoti/dynamic_share_service/policy/wanted_attribute.rb