Class: Hydra::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/hydra/message.rb

Overview

Base message object. Used to pass messages with parameters around via IO objects.

class MyMessage < Hydra::Message
  attr_accessor :my_var
  def serialize
    super(:my_var => @my_var)
  end
end
m = MyMessage.new(:my_var => 'my value')
m.my_var
  => "my value"
m.serialize
  => "{:class=>TestMessage::MyMessage, :my_var=>\"my value\"}"
Hydra::Message.build(eval(@m.serialize)).my_var
  => "my value"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Message

Create a new message. Opts is a hash where the keys are attributes of the message and the values are set to the attribute.



21
22
23
24
25
26
# File 'lib/hydra/message.rb', line 21

def initialize(opts = {})
  opts.delete :class
  opts.each do |variable,value|
    self.send("#{variable}=",value)
  end
end

Class Method Details

.build(hash) ⇒ Object

Build a message from a hash. The hash must contain the :class symbol, which is the class of the message that it will build to.



31
32
33
# File 'lib/hydra/message.rb', line 31

def self.build(hash)
  hash.delete(:class).new(hash)
end

Instance Method Details

#serialize(opts = {}) ⇒ Object

Serialize the message for output on an IO channel. This is really just a string representation of a hash with no newlines. It adds in the class automatically



38
39
40
# File 'lib/hydra/message.rb', line 38

def serialize(opts = {})
  opts.merge({:class => self.class}).inspect
end