Module: Legion::Settings::Overlay
- Defined in:
- lib/legion/settings/overlay.rb
Overview
Thread-local request-scoped settings overlay.
Provides block-scoped overrides that sit above global settings in the resolution order: request overlay > project .legionio.env > global settings.
Usage:
Legion::Settings.(llm: { default_model: 'claude-3-haiku' }) do
Legion::Settings[:llm][:default_model] # => 'claude-3-haiku'
end
Overlays are nestable — inner overlay merges on top of the outer one.
Constant Summary collapse
- THREAD_KEY =
:legion_settings_overlay
Class Method Summary collapse
-
.clear_overlay! ⇒ Object
Clear the thread-local overlay for the current thread.
-
.current_overlay ⇒ Hash?
Return the current thread-local overlay hash, or nil if none is active.
-
.overlay_for(key) ⇒ Object?
Resolve a top-level key against the active overlay, returning the overlay value (which may need to be merged with base) or nil when no overlay is set.
-
.with_overlay(overrides) { ... } ⇒ Object
Execute a block with the given overrides active in the current thread.
Class Method Details
.clear_overlay! ⇒ Object
Clear the thread-local overlay for the current thread.
42 43 44 |
# File 'lib/legion/settings/overlay.rb', line 42 def Thread.current[THREAD_KEY] = nil end |
.current_overlay ⇒ Hash?
Return the current thread-local overlay hash, or nil if none is active.
37 38 39 |
# File 'lib/legion/settings/overlay.rb', line 37 def Thread.current[THREAD_KEY] end |
.overlay_for(key) ⇒ Object?
Resolve a top-level key against the active overlay, returning the overlay value (which may need to be merged with base) or nil when no overlay is set.
52 53 54 55 56 57 58 59 |
# File 'lib/legion/settings/overlay.rb', line 52 def (key) = Thread.current[THREAD_KEY] return nil unless sym_key = key.to_sym str_key = key.to_s [sym_key] || [str_key] end |
.with_overlay(overrides) { ... } ⇒ Object
Execute a block with the given overrides active in the current thread. The overrides hash uses the same top-level key structure as Settings.
26 27 28 29 30 31 32 |
# File 'lib/legion/settings/overlay.rb', line 26 def (overrides) previous = Thread.current[THREAD_KEY] Thread.current[THREAD_KEY] = deep_merge(previous || {}, overrides) yield ensure Thread.current[THREAD_KEY] = previous end |