Class: Smplkit::ContextScope

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/context.rb

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

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

#callObject

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

#exitObject

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