Class: RobotLab::MemoryChange
- Inherits:
-
Object
- Object
- RobotLab::MemoryChange
- Defined in:
- lib/robot_lab/memory_change.rb
Overview
This class is designed to be compatible with SmartMessage::Base. When smart_message is added as a dependency, this class can inherit from SmartMessage::Base for distributed pub/sub support.
Represents a change to a Memory key
MemoryChange is passed to subscription callbacks when a key’s value changes. It provides context about what changed, who changed it, and when.
Instance Attribute Summary collapse
-
#correlation_id ⇒ Object
readonly
Returns the value of attribute correlation_id.
-
#key ⇒ Symbol
readonly
The memory key that changed.
-
#network_name ⇒ String?
readonly
Name of the network.
-
#previous ⇒ Object?
readonly
The previous value (nil if key was created).
-
#timestamp ⇒ Time
readonly
When the change occurred.
-
#value ⇒ Object
readonly
The new value.
-
#writer ⇒ String?
readonly
Name of the robot that wrote the value.
Class Method Summary collapse
-
.from_hash(hash) ⇒ MemoryChange
Reconstruct from hash.
Instance Method Summary collapse
-
#created? ⇒ Boolean
Check if this is a new key (no previous value).
-
#deleted? ⇒ Boolean
Check if the key was deleted.
-
#initialize(key:, value:, previous: nil, writer: nil, network_name: nil, timestamp: nil, correlation_id: nil) ⇒ MemoryChange
constructor
Creates a new MemoryChange instance.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_json(*args) ⇒ String
Convert to JSON.
-
#updated? ⇒ Boolean
Check if this is an update to an existing key.
Constructor Details
#initialize(key:, value:, previous: nil, writer: nil, network_name: nil, timestamp: nil, correlation_id: nil) ⇒ MemoryChange
Creates a new MemoryChange instance.
46 47 48 49 50 51 52 53 54 |
# File 'lib/robot_lab/memory_change.rb', line 46 def initialize(key:, value:, previous: nil, writer: nil, network_name: nil, timestamp: nil, correlation_id: nil) @key = key.to_sym @value = value @previous = previous @writer = writer @network_name = network_name @timestamp = || Time.now @correlation_id = correlation_id end |
Instance Attribute Details
#correlation_id ⇒ Object (readonly)
Returns the value of attribute correlation_id.
34 |
# File 'lib/robot_lab/memory_change.rb', line 34 attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id |
#key ⇒ Symbol (readonly)
Returns the memory key that changed.
34 35 36 |
# File 'lib/robot_lab/memory_change.rb', line 34 def key @key end |
#network_name ⇒ String? (readonly)
Returns name of the network.
34 |
# File 'lib/robot_lab/memory_change.rb', line 34 attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id |
#previous ⇒ Object? (readonly)
Returns the previous value (nil if key was created).
34 |
# File 'lib/robot_lab/memory_change.rb', line 34 attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id |
#timestamp ⇒ Time (readonly)
Returns when the change occurred.
34 |
# File 'lib/robot_lab/memory_change.rb', line 34 attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id |
#value ⇒ Object (readonly)
Returns the new value.
34 |
# File 'lib/robot_lab/memory_change.rb', line 34 attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id |
#writer ⇒ String? (readonly)
Returns name of the robot that wrote the value.
34 |
# File 'lib/robot_lab/memory_change.rb', line 34 attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id |
Class Method Details
.from_hash(hash) ⇒ MemoryChange
Reconstruct from hash.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/robot_lab/memory_change.rb', line 110 def self.from_hash(hash) hash = hash.transform_keys(&:to_sym) new( key: hash[:key], value: hash[:value], previous: hash[:previous], writer: hash[:writer], network_name: hash[:network_name], timestamp: hash[:timestamp] ? Time.parse(hash[:timestamp]) : nil, correlation_id: hash[:correlation_id] ) end |
Instance Method Details
#created? ⇒ Boolean
Check if this is a new key (no previous value).
60 61 62 |
# File 'lib/robot_lab/memory_change.rb', line 60 def created? @previous.nil? && !@value.nil? end |
#deleted? ⇒ Boolean
Check if the key was deleted.
76 77 78 |
# File 'lib/robot_lab/memory_change.rb', line 76 def deleted? @value.nil? && !@previous.nil? end |
#to_h ⇒ Hash
Convert to hash representation.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/robot_lab/memory_change.rb', line 84 def to_h { key: @key, value: @value, previous: @previous, writer: @writer, network_name: @network_name, timestamp: @timestamp.iso8601, correlation_id: @correlation_id }.compact end |
#to_json(*args) ⇒ String
Convert to JSON.
101 102 103 |
# File 'lib/robot_lab/memory_change.rb', line 101 def to_json(*args) to_h.to_json(*args) end |
#updated? ⇒ Boolean
Check if this is an update to an existing key.
68 69 70 |
# File 'lib/robot_lab/memory_change.rb', line 68 def updated? !@previous.nil? && !@value.nil? end |