Class: Warren::Message::Short

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

Overview

Light-weight interim message which can be expanded to a full payload later.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record = nil, class_name: nil, id: nil) ⇒ Short

Create a ‘short’ message, where the payload is just the class name and id. Designed for when you wish to use a delayed broadcast.

Parameters:

  • record (ActiveRecord::Base) (defaults to: nil)

    An Active Record object



21
22
23
24
25
26
27
28
29
# File 'lib/warren/message/short.rb', line 21

def initialize(record = nil, class_name: nil, id: nil)
  if record
    @class_name = record.class.name
    @id = record.id
  else
    @class_name = class_name
    @id = id
  end
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#headers{}

For compatibility. Returns an empty hash.

Returns:

  • ({})

    Empty hash



62
63
64
# File 'lib/warren/message/short.rb', line 62

def headers
  {}
end

#payloadString

The contents of the message, a string in the form:

“<ClassName>”,<id>

Returns:

  • (String)

    The payload of the message



54
55
56
# File 'lib/warren/message/short.rb', line 54

def payload
  [@class_name, @id].to_json
end

#queue(warren) ⇒ Object

Queues the message for broadcast at the end of the transaction. Actually want to make this the default behaviour, but only realised the need when doing some last minute integration tests. Will revisit this in the next version. (Or possibly post code review depending)



34
35
36
37
38
# File 'lib/warren/message/short.rb', line 34

def queue(warren)
  after_commit { warren << self }
rescue NoMethodError
  raise StandardError, '#queue depends on the after_commit_everywhere gem. Please add this to your gemfile'
end

#routing_keyString

The routing key for the message.

Returns:

  • (String)

    The routing key



44
45
46
# File 'lib/warren/message/short.rb', line 44

def routing_key
  "queue_broadcast.#{@class_name.underscore}.#{@id}"
end