Class: Braintrust::Contrib::Context
- Inherits:
-
Object
- Object
- Braintrust::Contrib::Context
- Defined in:
- lib/braintrust/contrib/context.rb
Overview
Per-instance or per-class configuration context. Allows attaching generic configuration to specific objects or classes.
Class Method Summary collapse
-
.from(target) ⇒ Context?
Retrieve context from a target.
-
.set!(target, **options) ⇒ Context?
Set or update context on a target object.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#fetch(key, default) ⇒ Object
Get an option value with a default fallback.
-
#initialize(**options) ⇒ Context
constructor
A new instance of Context.
Constructor Details
#initialize(**options) ⇒ Context
Returns a new instance of Context.
35 36 37 |
# File 'lib/braintrust/contrib/context.rb', line 35 def initialize(**) @options = end |
Class Method Details
.from(target) ⇒ Context?
Retrieve context from a target.
30 31 32 |
# File 'lib/braintrust/contrib/context.rb', line 30 def self.from(target) target&.instance_variable_get(:@braintrust_context) end |
.set!(target, **options) ⇒ Context?
Set or update context on a target object. Creates a new context if one doesn’t exist, or updates existing context.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/braintrust/contrib/context.rb', line 13 def self.set!(target, **) return nil if .empty? if (ctx = from(target)) # Update existing context .each { |k, v| ctx[k] = v } else # Create and attach new context target.instance_variable_set(:@braintrust_context, new(**)) end ctx end |
Instance Method Details
#[](key) ⇒ Object
39 40 41 |
# File 'lib/braintrust/contrib/context.rb', line 39 def [](key) @options[key] end |
#[]=(key, value) ⇒ Object
43 44 45 |
# File 'lib/braintrust/contrib/context.rb', line 43 def []=(key, value) @options[key] = value end |
#fetch(key, default) ⇒ Object
Get an option value with a default fallback.
51 52 53 |
# File 'lib/braintrust/contrib/context.rb', line 51 def fetch(key, default) @options.fetch(key, default) end |