Sha256: 303c2d6f52d864108999caa0351d7ae0ffa94f2aa5880e6b2d164f6913d1f630

Contents?: true

Size: 935 Bytes

Versions: 1

Compression:

Stored size: 935 Bytes

Contents

# frozen_string_literal: true

class BCDD::Result
  class Handler
    UNDEFINED = ::Object.new

    def initialize(result)
      @outcome = UNDEFINED

      @_type = result._type
      @result = result
    end

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

      self.outcome = block if _type.in?(types, allow_empty: false)
    end

    def failure(*types, &block)
      self.outcome = block if result.failure? && _type.in?(types, allow_empty: true)
    end

    def success(*types, &block)
      self.outcome = block if result.success? && _type.in?(types, allow_empty: true)
    end

    alias type []

    private

    attr_reader :_type, :result

    def outcome?
      @outcome != UNDEFINED
    end

    def outcome
      @outcome if outcome?
    end

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

  private_constant :Handler
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-result-0.3.0 lib/bcdd/result/handler.rb