Sha256: 8ac7e7eecf23898d22278e002e9ab9a749ccc06a87bd49d140f1ffc0e2680e32

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

class BCDD::Result
  class Handler
    require_relative 'handler/allowed_types'

    UNDEFINED = ::Object.new

    def initialize(result, type_checker:)
      @allowed_types = AllowedTypes.new(type_checker)

      @outcome = UNDEFINED

      @result = result
    end

    def [](*types, &block)
      raise Error::MissingTypeArgument if types.empty?

      self.outcome = block if allowed_types.allow?(types)
    end

    def success(*types, &block)
      self.outcome = block if allowed_types.allow_success?(types) && result.success?
    end

    def failure(*types, &block)
      self.outcome = block if allowed_types.allow_failure?(types) && result.failure?
    end

    def unknown(&block)
      self.outcome = block unless outcome?
    end

    alias type []

    private

    attr_reader :result, :allowed_types

    def outcome?
      @outcome != UNDEFINED
    end

    def outcome=(block)
      @outcome = block.call(result.value, result.type) unless outcome?
    end

    def outcome
      allowed_types.all_checked? or raise Error::UnhandledTypes.build(types: allowed_types.unchecked)

      @outcome if outcome?
    end
  end

  private_constant :Handler
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bcdd-result-1.1.0 lib/bcdd/result/handler.rb
bcdd-result-1.0.0 lib/bcdd/result/handler.rb
bcdd-result-0.13.0 lib/bcdd/result/handler.rb
bcdd-result-0.12.0 lib/bcdd/result/handler.rb
bcdd-result-0.11.0 lib/bcdd/result/handler.rb
bcdd-result-0.10.0 lib/bcdd/result/handler.rb
bcdd-result-0.9.1 lib/bcdd/result/handler.rb
bcdd-result-0.9.0 lib/bcdd/result/handler.rb
bcdd-result-0.8.0 lib/bcdd/result/handler.rb
bcdd-result-0.7.0 lib/bcdd/result/handler.rb
bcdd-result-0.6.0 lib/bcdd/result/handler.rb
bcdd-result-0.5.0 lib/bcdd/result/handler.rb