Sha256: c7f3b136f5c37cc031ebe05df54ec672c777698647287ccb4d13681cddbe7fdc

Contents?: true

Size: 782 Bytes

Versions: 8

Compression:

Stored size: 782 Bytes

Contents

# frozen_string_literal: true

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

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

      @expectations = expectations
    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

8 entries across 8 versions & 1 rubygems

Version Path
bcdd-result-0.13.0 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.12.0 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.11.0 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.10.0 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.9.1 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.9.0 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.8.0 lib/bcdd/result/contract/type_checker.rb
bcdd-result-0.7.0 lib/bcdd/result/contract/type_checker.rb