Module: RailsAiBridge::AppScope
- Defined in:
- lib/rails_ai_bridge/app_scope.rb
Overview
Thread-local application scope for the runtime seam.
Provides a scoped with_app(app) block so that server tool calls, CLI
commands, resources, and tests can share a single app reference without
hardcoding Rails.application everywhere. The default falls back to
Rails.application for backward compatibility with Rails-hosted usage.
Constant Summary collapse
- APP_KEY =
:rails_ai_bridge_current_app
Class Method Summary collapse
-
.clear_app ⇒ void
Clears the app scope for the calling thread.
-
.current_app ⇒ Object?
Returns the current application for the calling thread, defaulting to
Rails.applicationwhen no scope is active. -
.with_app(app) { ... } ⇒ Object
Executes a block with
appas the current application for the calling thread.
Class Method Details
.clear_app ⇒ void
This method returns an undefined value.
Clears the app scope for the calling thread. Primarily for test cleanup.
48 49 50 |
# File 'lib/rails_ai_bridge/app_scope.rb', line 48 def clear_app Thread.current[APP_KEY] = nil end |
.current_app ⇒ Object?
Returns the current application for the calling thread, defaulting to
Rails.application when no scope is active.
41 42 43 |
# File 'lib/rails_ai_bridge/app_scope.rb', line 41 def current_app Thread.current[APP_KEY] || (defined?(Rails) ? Rails.application : nil) end |
.with_app(app) { ... } ⇒ Object
Executes a block with app as the current application for the calling
thread. Nested scopes restore the prior app on exit, including when the
block raises.
:reek:DuplicateMethodCall -- Thread.current access is the established pattern (see Registry.with_request_resolver)
29 30 31 32 33 34 35 |
# File 'lib/rails_ai_bridge/app_scope.rb', line 29 def with_app(app) previous = Thread.current[APP_KEY] Thread.current[APP_KEY] = app yield ensure Thread.current[APP_KEY] = previous end |