Sha256: c62e0a4ac776dfc20231d16ec41253ded3ebc23e12be63942af4cca3cc083c93

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

class Solid::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

1 entries across 1 versions & 1 rubygems

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