Class: DecisionAgent::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Context

Returns a new instance of Context.

Parameters:

  • data (Hash, Object)

    Input data; non-Hash is treated as empty Hash



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

#dataObject (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.

Parameters:

  • key (Object)

    Key to look up

Returns:

  • (Object, nil)

    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.

Parameters:

  • key (Object)

    Key to look up

  • default (Object) (defaults to: nil)

    Value returned when key is missing (default: nil)

Returns:

  • (Object)

    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.

Parameters:

  • key (Object)

    Key to check

Returns:

  • (Boolean)

    Whether the key exists



32
33
34
# File 'lib/decision_agent/context.rb', line 32

def key?(key)
  @data.key?(key)
end

#to_hHash

Returns The underlying frozen data hash.

Returns:

  • (Hash)

    The underlying frozen data hash



37
38
39
# File 'lib/decision_agent/context.rb', line 37

def to_h
  @data
end