Sha256: 06be1568acc24f84c50551e7214e291fcdb9171ef2ee375f2c772569809b9470

Contents?: true

Size: 1.05 KB

Versions: 8

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

class BCDD::Result
  class Contract::Evaluator
    include Contract::Interface

    attr_reader :allowed_types, :success, :failure

    def initialize(success, failure)
      @success = success
      @failure = failure

      @allowed_types = (success.allowed_types | failure.allowed_types).freeze
    end

    def type?(type)
      success_disabled = success == Contract::Disabled
      failure_disabled = failure == Contract::Disabled

      return Contract::Disabled.type?(type) if success_disabled && failure_disabled

      (!success_disabled && success.type?(type)) || (!failure_disabled && failure.type?(type))
    end

    def type!(type)
      return type if type?(type)

      raise Contract::Error::UnexpectedType.build(type: type, allowed_types: allowed_types)
    end

    def type_and_value!(data)
      self.for(data).type_and_value!(data)
    end

    private

    def for(data)
      case data.kind
      when :unknown then Contract::Disabled
      when :success then success
      else failure
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bcdd-result-1.1.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-1.0.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.13.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.12.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.11.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.10.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.9.1 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.9.0 lib/bcdd/result/contract/evaluator.rb