Class: Briefly::Rescues

Inherits:
Object
  • Object
show all
Defined in:
lib/briefly/rescues.rb

Overview

An ordered list of facade-wide and global rescue_from registrations, queried at dispatch time. A shortcut's own handlers live on the Shortcut, not here — this holds only the unscoped handlers a single shortcut cannot voice.

Entries are stored oldest-first and matched newest-first, so the most recent registration for a given error class wins. Reads are lock-free against a frozen array; writes rebind it under a mutex, because rebinding alone would drop concurrent registrations.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeRescues

Returns a new instance of Rescues.



15
16
17
18
# File 'lib/briefly/rescues.rb', line 15

def initialize
  @entries = [].freeze
  @mutex = Mutex.new
end

Instance Method Details

#add(klass, handler) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • klass (Class)

    matched against the raised error and its subclasses

  • handler (Proc)

    called with (error, shortcut_name)

Returns:

  • (self)


24
25
26
27
# File 'lib/briefly/rescues.rb', line 24

def add(klass, handler)
  @mutex.synchronize { @entries = [*@entries, Entry.new(klass, handler)].freeze }
  self
end

#clearself

Drops every registration. Reach it as Briefly.rescues.clear to reset globally registered handlers between test examples, so a handler from one example cannot leak into the next.

Returns:

  • (self)


42
43
44
45
# File 'lib/briefly/rescues.rb', line 42

def clear
  @mutex.synchronize { @entries = [].freeze }
  self
end

#handler_for(error) ⇒ Proc?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the most recently registered handler whose class matches error, or nil.

Parameters:

  • error (Exception)

Returns:

  • (Proc, nil)

    the most recently registered handler whose class matches error, or nil



32
# File 'lib/briefly/rescues.rb', line 32

def handler_for(error) = @entries.reverse_each.find { |entry| error.is_a?(entry.klass) }&.handler

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns how many registrations the registry holds.

Returns:

  • (Integer)

    how many registrations the registry holds



36
# File 'lib/briefly/rescues.rb', line 36

def size = @entries.size