Class: Braintrust::Contrib::Registry
- Inherits:
-
Object
- Object
- Braintrust::Contrib::Registry
- Includes:
- Singleton
- Defined in:
- lib/braintrust/contrib/registry.rb
Overview
Thread-safe singleton registry for integrations. Provides registration, lookup, and require-path mapping for auto-instrumentation.
Instance Method Summary collapse
-
#[](name) ⇒ Class?
Look up an integration by name.
-
#all ⇒ Array<Class>
Get all registered integrations.
-
#available ⇒ Array<Class>
Get all available integrations (target library is loaded).
-
#each {|Class| ... } ⇒ Object
Iterate over all registered integrations.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#integrations_for_require_path(path) ⇒ Array<Class>
Returns integrations associated with a require path.
-
#register(integration_class) ⇒ Object
Register an integration class with the registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
12 13 14 15 16 |
# File 'lib/braintrust/contrib/registry.rb', line 12 def initialize @integrations = {} @require_path_map = nil # Lazy cache @mutex = Mutex.new end |
Instance Method Details
#[](name) ⇒ Class?
Look up an integration by name.
30 31 32 |
# File 'lib/braintrust/contrib/registry.rb', line 30 def [](name) @integrations[name.to_sym] end |
#all ⇒ Array<Class>
Get all registered integrations.
36 37 38 |
# File 'lib/braintrust/contrib/registry.rb', line 36 def all @integrations.values end |
#available ⇒ Array<Class>
Get all available integrations (target library is loaded).
42 43 44 |
# File 'lib/braintrust/contrib/registry.rb', line 42 def available @integrations.values.select(&:available?) end |
#each {|Class| ... } ⇒ Object
Iterate over all registered integrations.
48 49 50 |
# File 'lib/braintrust/contrib/registry.rb', line 48 def each(&block) @integrations.values.each(&block) end |
#integrations_for_require_path(path) ⇒ Array<Class>
Returns integrations associated with a require path. Thread-safe with double-checked locking for performance.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/braintrust/contrib/registry.rb', line 56 def integrations_for_require_path(path) map = @require_path_map if map.nil? map = @mutex.synchronize do @require_path_map ||= build_require_path_map end end path_str = path.to_s basename = File.basename(path_str, ".rb") # Quick check: is this basename even in our map? return EMPTY_ARRAY unless map.key?(basename) # Only match top-level requires or gem entry points. # Avoid matching internal subpaths (e.g., ruby_llm/providers/anthropic). return EMPTY_ARRAY unless gem_entry_point?(path_str, basename) map.fetch(basename, EMPTY_ARRAY) end |
#register(integration_class) ⇒ Object
Register an integration class with the registry.
20 21 22 23 24 25 |
# File 'lib/braintrust/contrib/registry.rb', line 20 def register(integration_class) @mutex.synchronize do @integrations[integration_class.integration_name] = integration_class @require_path_map = nil # Invalidate cache end end |