Class: JetstreamBridge::Models::Subject
- Inherits:
-
Object
- Object
- JetstreamBridge::Models::Subject
- Defined in:
- lib/jetstream_bridge/models/subject.rb
Overview
Value object representing a NATS subject
Constant Summary collapse
- WILDCARD_SINGLE =
'*'- WILDCARD_MULTI =
'>'- SEPARATOR =
'.'- INVALID_CHARS =
/[#{Regexp.escape(WILDCARD_SINGLE + WILDCARD_MULTI + SEPARATOR)}]/
Instance Attribute Summary collapse
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .destination(env:, source:, app_name:) ⇒ Object
- .dlq(env:, app_name:) ⇒ Object
-
.parse(string) ⇒ Subject
Parse a subject string into a Subject object with metadata.
-
.source(env:, app_name:, dest:) ⇒ Object
Factory methods.
-
.validate_component!(value, name) ⇒ Object
Validate a component (env, app_name, etc.) for use in subjects.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#covered_by?(patterns) ⇒ Boolean
Check if covered by any pattern in a list.
-
#dest_app ⇒ String?
Get destination application from subject.
-
#dlq? ⇒ Boolean
Check if this is a DLQ subject.
-
#env ⇒ String?
Get environment from subject (first token).
- #hash ⇒ Object
-
#initialize(value) ⇒ Subject
constructor
A new instance of Subject.
-
#matches?(pattern) ⇒ Boolean
Check if this subject matches a pattern.
-
#overlaps?(other) ⇒ Boolean
Check if this subject overlaps with another.
-
#source_app ⇒ String?
Get source application from subject.
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
22 23 24 |
# File 'lib/jetstream_bridge/models/subject.rb', line 22 def tokens @tokens end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
22 23 24 |
# File 'lib/jetstream_bridge/models/subject.rb', line 22 def value @value end |
Class Method Details
.destination(env:, source:, app_name:) ⇒ Object
38 39 40 |
# File 'lib/jetstream_bridge/models/subject.rb', line 38 def self.destination(env:, source:, app_name:) new("#{env}.#{source}.sync.#{app_name}") end |
.dlq(env:, app_name:) ⇒ Object
42 43 44 |
# File 'lib/jetstream_bridge/models/subject.rb', line 42 def self.dlq(env:, app_name:) new("#{env}.#{app_name}.sync.dlq") end |
.parse(string) ⇒ Subject
Parse a subject string into a Subject object with metadata
50 51 52 |
# File 'lib/jetstream_bridge/models/subject.rb', line 50 def self.parse(string) new(string) end |
.source(env:, app_name:, dest:) ⇒ Object
Factory methods
34 35 36 |
# File 'lib/jetstream_bridge/models/subject.rb', line 34 def self.source(env:, app_name:, dest:) new("#{env}.#{app_name}.sync.#{dest}") end |
.validate_component!(value, name) ⇒ Object
Validate a component (env, app_name, etc.) for use in subjects
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jetstream_bridge/models/subject.rb', line 117 def self.validate_component!(value, name) str = value.to_s if str.match?(INVALID_CHARS) wildcards = "#{SEPARATOR}, #{WILDCARD_SINGLE}, #{WILDCARD_MULTI}" raise ArgumentError, "#{name} cannot contain NATS wildcards (#{wildcards}): #{value.inspect}" end raise ArgumentError, "#{name} cannot be empty" if str.strip.empty? true end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
106 107 108 |
# File 'lib/jetstream_bridge/models/subject.rb', line 106 def ==(other) @value == (other.is_a?(Subject) ? other.value : other.to_s) end |
#covered_by?(patterns) ⇒ Boolean
Check if covered by any pattern in a list
98 99 100 |
# File 'lib/jetstream_bridge/models/subject.rb', line 98 def covered_by?(patterns) SubjectMatcher.covered?(Array(patterns).map(&:to_s), @value) end |
#dest_app ⇒ String?
Get destination application from subject
74 75 76 |
# File 'lib/jetstream_bridge/models/subject.rb', line 74 def dest_app @tokens[3] end |
#dlq? ⇒ Boolean
Check if this is a DLQ subject
DLQ subjects follow the pattern: #env.app.sync.dlq
83 84 85 |
# File 'lib/jetstream_bridge/models/subject.rb', line 83 def dlq? @tokens.length == 4 && @tokens[2] == 'sync' && @tokens[3] == 'dlq' end |
#env ⇒ String?
Get environment from subject (first token)
57 58 59 |
# File 'lib/jetstream_bridge/models/subject.rb', line 57 def env @tokens[0] end |
#hash ⇒ Object
112 113 114 |
# File 'lib/jetstream_bridge/models/subject.rb', line 112 def hash @value.hash end |
#matches?(pattern) ⇒ Boolean
Check if this subject matches a pattern
88 89 90 |
# File 'lib/jetstream_bridge/models/subject.rb', line 88 def matches?(pattern) SubjectMatcher.match?(pattern.to_s, @value) end |
#overlaps?(other) ⇒ Boolean
Check if this subject overlaps with another
93 94 95 |
# File 'lib/jetstream_bridge/models/subject.rb', line 93 def overlaps?(other) SubjectMatcher.overlap?(@value, other.to_s) end |
#source_app ⇒ String?
Get source application from subject
For regular subjects: #env.#source_app.sync.dest For DLQ subjects: #env.app_name.sync.dlq
67 68 69 |
# File 'lib/jetstream_bridge/models/subject.rb', line 67 def source_app @tokens[1] end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/jetstream_bridge/models/subject.rb', line 102 def to_s @value end |