Class: DecisionAgent::Context
- Inherits:
-
Object
- Object
- DecisionAgent::Context
- Defined in:
- lib/decision_agent/context.rb
Overview
Immutable, thread-safe wrapper around input data passed to evaluators. Data is deep-copied and deep-frozen on construction.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Object?
Value for key, or nil if missing.
-
#fetch(key, default = nil) ⇒ Object
Value for key, or default.
-
#initialize(data) ⇒ Context
constructor
A new instance of Context.
-
#key?(key) ⇒ Boolean
Whether the key exists.
-
#to_h ⇒ Hash
The underlying frozen data hash.
Constructor Details
#initialize(data) ⇒ Context
Returns a new instance of Context.
10 11 12 13 14 15 |
# File 'lib/decision_agent/context.rb', line 10 def initialize(data) # Create a deep copy before freezing to avoid mutating the original # This is necessary for thread-safety even if it adds some overhead data_hash = data.is_a?(Hash) ? data : {} @data = deep_freeze(deep_dup(data_hash)) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/decision_agent/context.rb', line 7 def data @data end |
Instance Method Details
#==(other) ⇒ Object
41 42 43 |
# File 'lib/decision_agent/context.rb', line 41 def ==(other) other.is_a?(Context) && @data == other.data end |
#[](key) ⇒ Object?
Returns Value for key, or nil if missing.
19 20 21 |
# File 'lib/decision_agent/context.rb', line 19 def [](key) @data[key] end |
#fetch(key, default = nil) ⇒ Object
Returns Value for key, or default.
26 27 28 |
# File 'lib/decision_agent/context.rb', line 26 def fetch(key, default = nil) @data.fetch(key, default) end |
#key?(key) ⇒ Boolean
Returns Whether the key exists.
32 33 34 |
# File 'lib/decision_agent/context.rb', line 32 def key?(key) @data.key?(key) end |
#to_h ⇒ Hash
Returns The underlying frozen data hash.
37 38 39 |
# File 'lib/decision_agent/context.rb', line 37 def to_h @data end |