Module: Messaging::Message

Extended by:
ActiveSupport::Concern
Defined in:
lib/messaging/message.rb,
lib/messaging/message/from_json.rb

Defined Under Namespace

Modules: ClassMethods Classes: FromJson

Instance Method Summary collapse

Instance Method Details

#as_json(*_args) ⇒ Object



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

def as_json(*_args)
  attributes_as_json.merge(stream_position: stream_position)
end

#attributes_as_json(*_args) ⇒ Object

We do our own conversion for datetimes as by default they will only be stored with microseconds, which makes us loose precision when we serialize to json and back to message objects



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

def attributes_as_json(*_args)
  attributes.transform_values { |v| v.respond_to?(:nsec) ? v.iso8601(9) : v }
end

#categoryObject



129
130
131
# File 'lib/messaging/message.rb', line 129

def category
  Messaging.category(stream_category)
end

#key_attributeObject



110
111
112
# File 'lib/messaging/message.rb', line 110

def key_attribute
  self.class.key_attribute
end

#message_keyObject



106
107
108
# File 'lib/messaging/message.rb', line 106

def message_key
  attributes[key_attribute&.to_sym]
end

#message_typeObject



139
140
141
# File 'lib/messaging/message.rb', line 139

def message_type
  self.class.to_s
end

#publishObject



143
144
145
# File 'lib/messaging/message.rb', line 143

def publish
  Messaging::Publish.(message: self)
end

#stream_categoryObject



123
124
125
126
127
# File 'lib/messaging/message.rb', line 123

def stream_category
  return unless stream_name

  stream_name.split('$').first
end

#stream_idObject



133
134
135
136
137
# File 'lib/messaging/message.rb', line 133

def stream_id
  return unless stream_name

  stream_name.split('$').last
end

#stream_nameObject



118
119
120
121
# File 'lib/messaging/message.rb', line 118

def stream_name
  return unless self.class.stream_name
  instance_exec(&self.class.stream_name)
end

#topicObject



114
115
116
# File 'lib/messaging/message.rb', line 114

def topic
  self.class.topic
end