Module: Weft::DSL::Recoveries::ClassMethods
- Defined in:
- lib/weft/dsl/recoveries.rb
Instance Method Summary collapse
-
#recoveries ⇒ Object
All declared recovery entries (own + inherited), in resolution order.
-
#recovers(from:, with: nil, &block) ⇒ Object
Declare a recovery edge: how this class handles a specific error.
-
#recovery_for(exception) ⇒ Object
Find the first recovery entry whose
from:matches the given exception. -
#resolve_recovery_target(entry) ⇒ Object
Resolve the recovery entry's
with:value to a concrete target class.
Instance Method Details
#recoveries ⇒ Object
All declared recovery entries (own + inherited), in resolution order. Own entries precede inherited entries so subclass declarations take precedence over ancestor declarations. Within a class, declaration order is preserved — first-declared wins on ties.
40 41 42 43 44 45 46 |
# File 'lib/weft/dsl/recoveries.rb', line 40 def recoveries if superclass.respond_to?(:recoveries) own_recoveries + superclass.recoveries else own_recoveries.dup end end |
#recovers(from:, with: nil, &block) ⇒ Object
Declare a recovery edge: how this class handles a specific error.
recovers from: Weft::Unprocessable do |attrs, error|
{ error_messages: error. }
end
recovers from: Weft::Unauthorized, with: LoginPage
from: accepts a Class (subclass-inclusive), Integer (matched against
HTTPError#status), Range, or Array of any of the above.
with: accepts a Class (Page or Component) or Symbol (resolved against
Weft.configuration at error-handling time). Default: self.
The optional block receives |attrs, error| and returns a hash of
additional attrs that merge with the original on the recovery edge.
Symmetric with performs/transfers contracts (attrs first; error is the
recovery-specific extra). The block never returns HTML.
32 33 34 |
# File 'lib/weft/dsl/recoveries.rb', line 32 def recovers(from:, with: nil, &block) own_recoveries << { from: from, with: with, block: block } end |
#recovery_for(exception) ⇒ Object
Find the first recovery entry whose from: matches the given exception.
Returns nil if nothing matches. from: accepts Class (subclass-inclusive),
Integer (status equality — HTTPError carries .status; non-HTTPError = 500),
Range (status in range), or Array of any of the above (any element matches).
52 53 54 |
# File 'lib/weft/dsl/recoveries.rb', line 52 def recovery_for(exception) recoveries.find { |entry| recovery_matches?(entry[:from], exception) } end |
#resolve_recovery_target(entry) ⇒ Object
Resolve the recovery entry's with: value to a concrete target class.
Symbol values look up Weft.configuration.<sym> (resolved at error-handling
time so config reassignment propagates). Nil falls back to self.
59 60 61 62 63 64 65 |
# File 'lib/weft/dsl/recoveries.rb', line 59 def resolve_recovery_target(entry) case entry[:with] when Symbol then Weft.configuration.public_send(entry[:with]) when nil then self else entry[:with] end end |