Sha256: e4f582773e0e330e0728a60db98610e793badddbbab7eb5379cb4b6f321baa4f

Contents?: true

Size: 1015 Bytes

Versions: 1

Compression:

Stored size: 1015 Bytes

Contents

# frozen_string_literal: true

class BCDD::Result
  class Contract::ForTypesAndValues
    include Contract::Interface

    def initialize(types_and_values)
      @types_and_values = types_and_values.transform_keys(&:to_sym)

      @types_contract = Contract::ForTypes.new(@types_and_values.keys)
    end

    def allowed_types
      @types_contract.allowed_types
    end

    def type?(type)
      @types_contract.type?(type)
    end

    def type!(type)
      @types_contract.type!(type)
    end

    def type_and_value!(data)
      type, value = data.type, data.value

      value_checking = @types_and_values[type!(type)]

      checking_result = value_checking === value

      return value if checking_result || (Contract.nil_as_valid_value_checking? && checking_result.nil?)

      raise Contract::Error::UnexpectedValue.build(type: type, value: value)
    rescue ::NoMatchingPatternError => e
      raise Contract::Error::UnexpectedValue.build(type: data.type, value: data.value, cause: e)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-result-0.7.0 lib/bcdd/result/contract/for_types_and_values.rb