Browse files

Added Message#uuid

  • Loading branch information...
1 parent f03918a commit cea4ac32bc6a0d0972d24bac74dea6a244168792 @krakatoa krakatoa committed Feb 20, 2013
Showing with 17 additions and 4 deletions.
  1. +5 −1 lib/message.rb
  2. +12 −3 lib/message/header.rb
View
6 lib/message.rb
@@ -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,
View
15 lib/message/header.rb
@@ -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
@@ -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

0 comments on commit cea4ac3

Please sign in to comment.