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.

Examples:

Scoped execution

RailsAiBridge::AppScope.with_app(my_app) do
  RailsAiBridge::AppScope.current_app # => my_app
end
RailsAiBridge::AppScope.current_app   # => Rails.application

Constant Summary collapse

APP_KEY =
:rails_ai_bridge_current_app

Class Method Summary collapse

Class Method Details

.clear_appvoid

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_appObject?

Returns the current application for the calling thread, defaulting to Rails.application when no scope is active.

Returns:

  • (Object, nil)

    the current Rails application or scoped app



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)

Parameters:

  • app (Object)

    the application to scope

Yields:

  • block to execute within the app scope

Returns:

  • (Object)

    the block result



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