Class: Philotic::Message

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

Direct Known Subclasses

DummyMessage, Logging::Message

Constant Summary collapse

NotInstantiableError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routables = {}, payloads = {}, connection = nil) ⇒ Message

Returns a new instance of Message.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/philotic/message.rb', line 32

def initialize(routables={}, payloads={}, connection=nil)
  raise NotInstantiableError if self.class == Philotic::Message
  self.timestamp               = Time.now.to_i
  self.philotic_firehose       = true
  self.connection              = connection
  self.philotic_serializations = self.connection.config.serializations

  # dynamically insert any passed in routables into both attr_routable
  # and attr_payload
  # result:  ability to arbitrarily send a easily routable hash
  # over into the bus
  _set_routables_or_payloads(:routable, routables)
  _set_routables_or_payloads(:payload, payloads)

  @published = false
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/philotic/message.rb', line 9

def connection
  @connection
end

#delivery_infoObject

Returns the value of attribute delivery_info.



9
10
11
# File 'lib/philotic/message.rb', line 9

def delivery_info
  @delivery_info
end

#publish_errorObject

Returns the value of attribute publish_error.



9
10
11
# File 'lib/philotic/message.rb', line 9

def publish_error
  @publish_error
end

#published=(value) ⇒ Object (writeonly)

Sets the attribute published

Parameters:

  • value

    the value to set the attribute published to.



10
11
12
# File 'lib/philotic/message.rb', line 10

def published=(value)
  @published = value
end

Class Method Details

.attr_payload(*names) ⇒ Object



26
27
28
29
# File 'lib/philotic/message.rb', line 26

def attr_payload(*names)
  attr_payload_accessors.merge(names)
  attr_accessor(*names)
end

.attr_payload_accessorsObject



17
18
19
# File 'lib/philotic/message.rb', line 17

def attr_payload_accessors
  @attr_payload_accessors ||= Set.new
end

.attr_routable(*names) ⇒ Object



21
22
23
24
# File 'lib/philotic/message.rb', line 21

def attr_routable(*names)
  attr_routable_accessors.merge(names)
  attr_accessor(*names)
end

.attr_routable_accessorsObject



13
14
15
# File 'lib/philotic/message.rb', line 13

def attr_routable_accessors
  @attr_routable_accessors ||= Set.new
end

.inherited(sub_class) ⇒ Object



49
50
51
52
53
# File 'lib/philotic/message.rb', line 49

def self.inherited(sub_class)
  sub_class.attr_routable(*Philotic::PHILOTIC_HEADERS)
  sub_class.attr_routable(*self.attr_routable_accessors.dup)
  sub_class.attr_payload(*self.attr_payload_accessors.dup)
end

.publish(*args) ⇒ Object



77
78
79
80
# File 'lib/philotic/message.rb', line 77

def self.publish(*args)
  message_class = self == Philotic::Message ? Class.new(self) : self
  message_class.new(*args).publish
end

Instance Method Details

#attributesObject



94
95
96
# File 'lib/philotic/message.rb', line 94

def attributes
  payload.merge headers
end

#delivery_tagObject



82
83
84
# File 'lib/philotic/message.rb', line 82

def delivery_tag
  delivery_info && delivery_info.delivery_tag
end

#headersObject



90
91
92
# File 'lib/philotic/message.rb', line 90

def headers
  _payload_or_headers(:routable)
end

#metadataObject



98
99
100
# File 'lib/philotic/message.rb', line 98

def 
   ||= {}
end

#metadata=(options) ⇒ Object



102
103
104
105
# File 'lib/philotic/message.rb', line 102

def metadata=(options)
   ||= {}
  .merge! options
end

#payloadObject



86
87
88
# File 'lib/philotic/message.rb', line 86

def payload
  _payload_or_headers(:payload)
end

#publishObject



73
74
75
# File 'lib/philotic/message.rb', line 73

def publish
  connection.publish self
end

#published?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/philotic/message.rb', line 69

def published?
  !!@published
end