class Synapses::Messages::Message
@author Alexander Semyonov <al@semyonov.us>
Attributes
message_type[R]
message_type[W]
metadata[RW]
type=[W]
Public Class Methods
attribute(attr, options = {})
click to toggle source
# File lib/synapses/messages/message.rb, line 45 def self.attribute(attr, options = {}) self.attributes[attr.to_s] = options # TODO add types attr_accessor attr end
inherited(child)
click to toggle source
Calls superclass method
# File lib/synapses/messages/message.rb, line 40 def self.inherited(child) super child.attributes = attributes.dup end
message_type=(type)
click to toggle source
# File lib/synapses/messages/message.rb, line 15 def self.message_type=(type) Messages.registry[type] = self @message_type = type end
new(attributes = {}, metadata = {})
click to toggle source
# File lib/synapses/messages/message.rb, line 54 def initialize(attributes = {}, metadata = {}) @attributes = {} attributes.each do |name, value| if respond_to?((writer = "#{name}=")) send(writer, value) else @attributes[name] = value end end metadata.assert_valid_keys(:routing_key, :type) end
parse(metadata, payload)
click to toggle source
# File lib/synapses/messages/message.rb, line 50 def self.parse(metadata, payload) new(Synapses::Messages::Coders.decode(payload, metadata.content_type).merge(metadata: metadata)) end
Public Instance Methods
attributes()
click to toggle source
# File lib/synapses/messages/message.rb, line 68 def attributes self.class.attributes.inject({}) do |attributes, (name, type)| attributes[name] = public_send(name) attributes end.merge(@attributes) end
message_type()
click to toggle source
# File lib/synapses/messages/message.rb, line 79 def message_type @message_type || self.class.message_type end
Also aliased as: type
options()
click to toggle source
# File lib/synapses/messages/message.rb, line 87 def options { routing_key: routing_key, type: message_type, mandatory: mandatory, immediate: immediate, persistent: persistent, content_type: content_type } end
to_payload()
click to toggle source
# File lib/synapses/messages/message.rb, line 75 def to_payload Synapses::Messages::Coders.encode(attributes, content_type) end