Sha256: 46c36e3930fdd7712670ab8d823b4da34bd5c887d39ff31d100483c36aa90607

Contents?: true

Size: 844 Bytes

Versions: 1

Compression:

Stored size: 844 Bytes

Contents

# frozen_string_literal: true

module Solid::Result::Contract
  class TypeChecker
    attr_reader :result_type, :expectations

    def initialize(result_type, expectations:)
      @result_type = result_type

      @expectations = expectations
    end

    def allow!(type)
      expectations.type!(type)
    end

    def allow?(types)
      validate(types, expected: expectations, allow_empty: false)
    end

    def allow_success?(types)
      validate(types, expected: expectations.success, allow_empty: true)
    end

    def allow_failure?(types)
      validate(types, expected: expectations.failure, allow_empty: true)
    end

    private

    def validate(types, expected:, allow_empty:)
      (allow_empty && types.empty?) || types.any? { |type| expected.type!(type) == result_type }
    end
  end

  private_constant :TypeChecker
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solid-result-2.0.0 lib/solid/result/contract/type_checker.rb