Sha256: 3cddc3371d08ceef630f35a485330e47bfa0e7a7190779731fa75ac0b470998b

Contents?: true

Size: 1.05 KB

Versions: 2

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.name
      when :unknown then Contract::Disabled
      when :success then success
      else failure
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bcdd-result-0.8.0 lib/bcdd/result/contract/evaluator.rb
bcdd-result-0.7.0 lib/bcdd/result/contract/evaluator.rb