Sha256: dbb9360564029252f539a1b0cd7ec10f2d1163120894d3147b903201d86a32c6

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

class Solid::Result
  class Contract::ForTypesAndValues
    include Contract::Interface

    def initialize(types_and_values, config)
      @nil_as_valid_value_checking =
        Config::Options
          .with_defaults(config, :pattern_matching)
          .fetch(:nil_as_valid_value_checking)

      @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

      return value if IgnoredTypes.include?(type)

      value_checking = @types_and_values[type!(type)]

      checking_result = value_checking === value

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

      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
solid-result-2.0.0 lib/solid/result/contract/for_types_and_values.rb