Class: Takagi::EventBus::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/event_bus.rb

Overview

Event message wrapper (shareable for Ractor)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, body, headers: {}, reply_address: nil, scope: Scope::DEFAULT, freeze_body: true) ⇒ Message

Returns a new instance of Message.



36
37
38
39
40
41
42
43
# File 'lib/takagi/event_bus.rb', line 36

def initialize(address, body, headers: {}, reply_address: nil, scope: Scope::DEFAULT, freeze_body: true)
  @address = address.freeze
  @body = freeze_body ? deep_freeze(body) : body
  @headers = deep_freeze(headers)
  @reply_address = reply_address&.freeze
  @scope = Scope.normalize(scope)
  @timestamp = Time.now
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



34
35
36
# File 'lib/takagi/event_bus.rb', line 34

def address
  @address
end

#bodyObject (readonly)

Returns the value of attribute body.



34
35
36
# File 'lib/takagi/event_bus.rb', line 34

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



34
35
36
# File 'lib/takagi/event_bus.rb', line 34

def headers
  @headers
end

#reply_addressObject (readonly)

Returns the value of attribute reply_address.



34
35
36
# File 'lib/takagi/event_bus.rb', line 34

def reply_address
  @reply_address
end

#scopeObject (readonly)

Returns the value of attribute scope.



34
35
36
# File 'lib/takagi/event_bus.rb', line 34

def scope
  @scope
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



34
35
36
# File 'lib/takagi/event_bus.rb', line 34

def timestamp
  @timestamp
end

Instance Method Details

#reply(body, headers: {}) ⇒ Object

Reply to this message (request-reply pattern)



46
47
48
49
50
# File 'lib/takagi/event_bus.rb', line 46

def reply(body, headers: {})
  return unless @reply_address

  EventBus.publish(@reply_address, body, headers: headers)
end