Class: RobotLab::RobotMessage

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

Overview

Typed message envelope for bus-based robot communication.

RobotMessage is a Data class (immutable value object) that wraps content sent between robots via a TypedBus channel.

Examples:

Creating a new message

msg = RobotMessage.build(id: 1, from: "alice", content: "Hello")
msg.key       #=> "alice:1"
msg.reply?    #=> false

Creating a reply

reply = RobotMessage.build(
  id: 2, from: "bob",
  content: "Hi back",
  in_reply_to: "alice:1"
)
reply.reply?  #=> true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



22
23
24
# File 'lib/robot_lab/robot_message.rb', line 22

def content
  @content
end

#fromObject (readonly)

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



22
23
24
# File 'lib/robot_lab/robot_message.rb', line 22

def from
  @from
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



22
23
24
# File 'lib/robot_lab/robot_message.rb', line 22

def id
  @id
end

#in_reply_toObject (readonly)

Returns the value of attribute in_reply_to

Returns:

  • (Object)

    the current value of in_reply_to



22
23
24
# File 'lib/robot_lab/robot_message.rb', line 22

def in_reply_to
  @in_reply_to
end

Class Method Details

.build(id:, from:, content:, in_reply_to: nil) ⇒ RobotMessage

Build a RobotMessage with in_reply_to defaulting to nil.

Parameters:

  • id (Integer)

    per-robot message counter

  • from (String)

    sender’s robot name (= channel name)

  • content (String, Hash)

    message payload

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

    composite key of the message being replied to

Returns:



30
31
32
# File 'lib/robot_lab/robot_message.rb', line 30

def self.build(id:, from:, content:, in_reply_to: nil)
  new(id: id, from: from, content: content, in_reply_to: in_reply_to)
end

Instance Method Details

#keyString

Composite identity key: “from:id”

Returns:

  • (String)


37
# File 'lib/robot_lab/robot_message.rb', line 37

def key = "#{from}:#{id}"

#reply?Boolean

Whether this message is a reply to another message.

Returns:

  • (Boolean)


42
# File 'lib/robot_lab/robot_message.rb', line 42

def reply? = !in_reply_to.nil?