Sha256: e2662a5afd08f173d594efd6c6c7552b545993ffeec36a85c92e70e48dd22584

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module Direct
  # This class monitors exception types with related blocks.
  class ExceptionHandler
    def initialize
      @handlers = {}
    end

    # All classes, including StandardError, for which this object
    # maintains a block to execute.
    def classes
      [StandardError, @handlers.keys.flatten].flatten
    end

    # Pass a single or multiple exception classes and the block
    # to be used to handle them.
    def monitor(*classes, &block)
      @handlers[classes.flatten] = block
    end

    # This will find the first handler given to `monitor` which matches
    # the provided exception's class and will execute it with the
    # deferred object, the exception object, and any given object to the
    # deferred object.
    def call(deferred, exception, object)
      if_none = proc { raise "No handler for this exception: #{exception.class}!" }
      result = @handlers.find(if_none) { |key, val| key.include?(exception.class) }

      result.last.call(deferred, exception, object)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
direct-2.0.1 lib/direct/exception_handler.rb