Class: Lepus::Message::Metadata

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

Overview

Internal data class representing message metadata/properties. Provides the same interface as Bunny::MessageProperties (duck typing).

Constant Summary collapse

KNOWN_ATTRIBUTES =
%i[
  content_type content_encoding headers delivery_mode priority
  correlation_id reply_to expiration message_id timestamp
  type user_id app_id cluster_id
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Metadata

Returns a new instance of Metadata.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lepus/message/metadata.rb', line 35

def initialize(**attrs)
  @content_type = attrs[:content_type]
  @content_encoding = attrs[:content_encoding]
  @headers = attrs[:headers]
  @delivery_mode = attrs[:delivery_mode]
  @priority = attrs[:priority]
  @correlation_id = attrs[:correlation_id]
  @reply_to = attrs[:reply_to]
  @expiration = attrs[:expiration]
  @message_id = attrs[:message_id]
  @timestamp = attrs[:timestamp]
  @type = attrs[:type]
  @user_id = attrs[:user_id]
  @app_id = attrs[:app_id]
  @cluster_id = attrs[:cluster_id]
  @extra_attributes = attrs.reject { |k, _| KNOWN_ATTRIBUTES.include?(k) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Support dynamic attribute access for compatibility



80
81
82
83
84
85
# File 'lib/lepus/message/metadata.rb', line 80

def method_missing(method_name, *args)
  return super if method_name.to_s.end_with?("=")
  return super if args.any?

  self[method_name]
end

Class Method Details

.from_bunny(bunny_metadata) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lepus/message/metadata.rb', line 16

def self.from_bunny()
  new(
    content_type: .content_type,
    content_encoding: .content_encoding,
    headers: .headers,
    delivery_mode: .delivery_mode,
    priority: .priority,
    correlation_id: .correlation_id,
    reply_to: .reply_to,
    expiration: .expiration,
    message_id: .message_id,
    timestamp: .timestamp,
    type: .type,
    user_id: .user_id,
    app_id: .app_id,
    cluster_id: .cluster_id
  )
end

Instance Method Details

#[](key) ⇒ Object?

Hash-style access to properties (compatible with Bunny::MessageProperties)

Parameters:

  • key (Symbol, String)

    The property name

Returns:

  • (Object, nil)

    The property value



75
76
77
# File 'lib/lepus/message/metadata.rb', line 75

def [](key)
  to_h[key.to_sym]
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/lepus/message/metadata.rb', line 91

def eql?(other)
  return false unless other.is_a?(self.class)

  to_h == other.to_h
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/lepus/message/metadata.rb', line 87

def respond_to_missing?(method_name, include_private = false)
  !method_name.to_s.end_with?("=") || super
end

#to_hObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lepus/message/metadata.rb', line 53

def to_h
  {
    content_type: content_type,
    content_encoding: content_encoding,
    headers: headers,
    delivery_mode: delivery_mode,
    priority: priority,
    correlation_id: correlation_id,
    reply_to: reply_to,
    expiration: expiration,
    message_id: message_id,
    timestamp: timestamp,
    type: type,
    user_id: user_id,
    app_id: app_id,
    cluster_id: cluster_id
  }.merge(@extra_attributes)
end