Class: Smplkit::ContextScope
- Inherits:
-
Object
- Object
- Smplkit::ContextScope
- Defined in:
- lib/smplkit/context.rb,
sig/smplkit.rbs
Overview
Returned by Smplkit::Client#set_context.
Optional to use — bare client.set_context([...]) is fire-and-forget
(typical middleware pattern). Holding the return value or using the block
form auto-reverts to the prior context on exit, useful for scoped
overrides like impersonation.
Instance Method Summary collapse
-
#call {|arg0| ... } ⇒ void
Block form support:
client.set_context([...]) { ... }. -
#exit ⇒ void
Restores the prior context.
-
#initialize(previous) ⇒ ContextScope
constructor
A new instance of ContextScope.
Constructor Details
#initialize(previous) ⇒ ContextScope
Returns a new instance of ContextScope.
37 38 39 40 |
# File 'lib/smplkit/context.rb', line 37 def initialize(previous) @previous = previous @exited = false end |
Instance Method Details
#call {|arg0| ... } ⇒ void
This method returns an undefined value.
Block form support: client.set_context([...]) { ... }.
43 44 45 46 47 |
# File 'lib/smplkit/context.rb', line 43 def call yield self ensure exit end |
#exit ⇒ void
This method returns an undefined value.
Restores the prior context. Idempotent — subsequent calls no-op.
50 51 52 53 54 55 |
# File 'lib/smplkit/context.rb', line 50 def exit return if @exited @exited = true RequestContext.reset(@previous) end |