Class: Briefly::Rescues
- Inherits:
-
Object
- Object
- Briefly::Rescues
- 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
- #add(klass, handler) ⇒ self private
-
#clear ⇒ self
Drops every registration.
-
#handler_for(error) ⇒ Proc?
private
The most recently registered handler whose class matches
error, ornil. -
#initialize ⇒ Rescues
constructor
A new instance of Rescues.
-
#size ⇒ Integer
private
How many registrations the registry holds.
Constructor Details
#initialize ⇒ Rescues
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.
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 |
#clear ⇒ self
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.
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.
32 |
# File 'lib/briefly/rescues.rb', line 32 def handler_for(error) = @entries.reverse_each.find { |entry| error.is_a?(entry.klass) }&.handler |
#size ⇒ Integer
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.
36 |
# File 'lib/briefly/rescues.rb', line 36 def size = @entries.size |