Skip to content

Commit

Permalink
Added Message#uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Alonso committed Feb 20, 2013
1 parent f03918a commit cea4ac3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ def initialize(opts={})
@header = Cucub::Message::Header.parse(opts["header"])
@body = Cucub::Message::Body.load(opts["body"])
else
opts_for_header = opts.select{|key| ["from", "to", "respond_to", "layer"].include?(key)}
opts_for_header = opts.select{|key| ["from", "to", "respond_to", "layer", "uuid"].include?(key)}
@header = Cucub::Message::Header.new(opts_for_header)
@body = Cucub::Message::Body.new(opts["action"], opts["additionals"])
end
end

def uuid
@header.uuid
end

def serialize
{
:header => @header.serialize,
Expand Down
15 changes: 12 additions & 3 deletions lib/message/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ class Header
attr_reader :from
attr_reader :to
attr_reader :respond_to
attr_reader :uuid

def initialize(opts={})
@from = opts["from"]
@to = opts["to"]
@respond_to = opts["respond_to"]
@uuid = opts["uuid"] || Cucub::Message::Header.gen_uuid(hash)
end

def self.gen_uuid(seed)
uuid = seed
uuid = -uuid if uuid < 0
uuid.to_s(35)
end

def self.attrs
[:from, :to, :respond_to]
[:from, :to, :respond_to, :uuid]
end

def serialize
self.class.attrs.inject({}) { |h, attr|
value = instance_variable_get("@#{attr}")
if value.is_a? Cucub::Reference
h[attr] = value.to_hash
elsif attr.is_a? String
elsif value.is_a? String
h[attr] = value
end
h
Expand All @@ -34,7 +42,8 @@ def self.parse(hash)
Cucub::Message::Header.new(
"from" => Cucub::Reference.new(hash["from"]),
"to" => Cucub::Reference.new(hash["to"]),
"respond_to" => respond_to
"respond_to" => respond_to,
"uuid" => hash["uuid"]
)
end

Expand Down

0 comments on commit cea4ac3

Please sign in to comment.