Class: RobotLab::MemoryChange

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/memory_change.rb

Overview

Note:

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.

Examples:

Subscription callback

memory.subscribe(:sentiment) do |change|
  puts "Key #{change.key} changed from #{change.previous} to #{change.value}"
  puts "Written by: #{change.writer} at #{change.timestamp}"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, value:, previous: nil, writer: nil, network_name: nil, timestamp: nil, correlation_id: nil) ⇒ MemoryChange

Creates a new MemoryChange instance.

Parameters:

  • key (Symbol, String)

    the memory key that changed

  • value (Object)

    the new value

  • previous (Object, nil) (defaults to: nil)

    the previous value

  • writer (String, nil) (defaults to: nil)

    name of the robot that wrote the value

  • network_name (String, nil) (defaults to: nil)

    name of the network

  • timestamp (Time) (defaults to: nil)

    when the change occurred (defaults to now)

  • correlation_id (String, nil) (defaults to: nil)

    optional correlation ID



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 = timestamp || Time.now
  @correlation_id = correlation_id
end

Instance Attribute Details

#correlation_idObject (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

#keySymbol (readonly)

Returns the memory key that changed.

Returns:

  • (Symbol)

    the memory key that changed



34
35
36
# File 'lib/robot_lab/memory_change.rb', line 34

def key
  @key
end

#network_nameString? (readonly)

Returns name of the network.

Returns:

  • (String, nil)

    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

#previousObject? (readonly)

Returns the previous value (nil if key was created).

Returns:

  • (Object, nil)

    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

#timestampTime (readonly)

Returns when the change occurred.

Returns:

  • (Time)

    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

#valueObject (readonly)

Returns the new value.

Returns:

  • (Object)

    the new value



34
# File 'lib/robot_lab/memory_change.rb', line 34

attr_reader :key, :value, :previous, :writer, :network_name, :timestamp, :correlation_id

#writerString? (readonly)

Returns name of the robot that wrote the value.

Returns:

  • (String, nil)

    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.

Parameters:

  • hash (Hash)

    the hash representation

Returns:



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).

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


76
77
78
# File 'lib/robot_lab/memory_change.rb', line 76

def deleted?
  @value.nil? && !@previous.nil?
end

#to_hHash

Convert to hash representation.

Returns:

  • (Hash)


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.

Parameters:

  • args (Array)

    arguments passed to to_json

Returns:

  • (String)


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.

Returns:

  • (Boolean)


68
69
70
# File 'lib/robot_lab/memory_change.rb', line 68

def updated?
  !@previous.nil? && !@value.nil?
end