Class: Lepus::Producer Abstract
- Inherits:
-
Object
- Object
- Lepus::Producer
- Defined in:
- lib/lepus/producer.rb
Overview
Subclass and override #configure to implement.
The abstract base class for producers publishing messages to exchanges.
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Class Method Summary collapse
- .abstract_class=(value) ⇒ Object
- .abstract_class? ⇒ Boolean
-
.configure(opts = {}) {|definition| ... } ⇒ Lepus::Producers::Definition
Configures the producer, setting exchange and other options to be used by the publisher for sending messages.
- .definition ⇒ Object
-
.descendants ⇒ Object
:nodoc:.
- .inherited(subclass) ⇒ Object
-
.middleware_chain ⇒ Lepus::Producers::MiddlewareChain
Returns the middleware chain for this producer.
-
.publish(payload, **options) ⇒ void
Publishes a message using this producer’s configuration.
-
.publisher ⇒ Lepus::Publisher
Creates a publisher instance configured with this producer’s settings.
-
.use(middleware, opts = {}) ⇒ Lepus::Producers::MiddlewareChain
Registers a middleware to this producer’s chain.
Instance Method Summary collapse
-
#initialize ⇒ Producer
constructor
Instance methods for when you need to work with producer instances.
- #publish(message, **options) ⇒ Object
- #publisher ⇒ Object
Constructor Details
#initialize ⇒ Producer
Instance methods for when you need to work with producer instances
139 140 141 |
# File 'lib/lepus/producer.rb', line 139 def initialize @definition = self.class.definition end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
143 144 145 |
# File 'lib/lepus/producer.rb', line 143 def definition @definition end |
Class Method Details
.abstract_class=(value) ⇒ Object
14 15 16 17 |
# File 'lib/lepus/producer.rb', line 14 def abstract_class=(value) @abstract_class = value remove_instance_variable(:@definition) if instance_variable_defined?(:@definition) end |
.abstract_class? ⇒ Boolean
8 9 10 11 12 |
# File 'lib/lepus/producer.rb', line 8 def abstract_class? return @abstract_class == true if defined?(@abstract_class) instance_variable_get(:@definition).nil? end |
.configure(opts = {}) {|definition| ... } ⇒ Lepus::Producers::Definition
Configures the producer, setting exchange and other options to be used by the publisher for sending messages.
41 42 43 44 45 46 47 |
# File 'lib/lepus/producer.rb', line 41 def configure(opts = {}) raise ArgumentError, "Cannot configure an abstract class" if abstract_class? @definition = Producers::Definition.new(opts) yield(@definition) if block_given? @definition end |
.definition ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/lepus/producer.rb', line 24 def definition return if abstract_class? return @definition if defined?(@definition) name = Primitive::String.new(to_s).underscore.split("/").last @definition = Producers::Definition.new(exchange: name) end |
.descendants ⇒ Object
:nodoc:
49 50 51 52 53 54 55 |
# File 'lib/lepus/producer.rb', line 49 def descendants # :nodoc: descendants = [] ObjectSpace.each_object(singleton_class) do |k| descendants.unshift k unless k == self end descendants.uniq end |
.inherited(subclass) ⇒ Object
19 20 21 22 |
# File 'lib/lepus/producer.rb', line 19 def inherited(subclass) super subclass.abstract_class = false end |
.middleware_chain ⇒ Lepus::Producers::MiddlewareChain
Returns the middleware chain for this producer.
65 66 67 |
# File 'lib/lepus/producer.rb', line 65 def middleware_chain @middleware_chain ||= Producers::MiddlewareChain.new end |
.publish(payload, **options) ⇒ void
This method returns an undefined value.
Publishes a message using this producer’s configuration. Executes the middleware chain (global + per-producer) before publishing.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lepus/producer.rb', line 84 def publish(payload, **) if definition.nil? raise InvalidProducerConfigError, <<~ERROR The #{name} producer is not configured. Please call #{name}.configure before using #{self.class.name}.publish. ERROR end return unless Producers.enabled?(self) publish_opts = definition..merge() = (payload, publish_opts) combined_chain = MiddlewareChain.combine( Lepus.config.producer_middleware_chain, middleware_chain ) combined_chain.execute() do |msg| publisher.publish(msg.payload, **msg.) end end |
.publisher ⇒ Lepus::Publisher
Creates a publisher instance configured with this producer’s settings.
59 60 61 |
# File 'lib/lepus/producer.rb', line 59 def publisher @publisher ||= Publisher.new(definition.exchange_name, **definition.) end |
.use(middleware, opts = {}) ⇒ Lepus::Producers::MiddlewareChain
Registers a middleware to this producer’s chain.
74 75 76 |
# File 'lib/lepus/producer.rb', line 74 def use(middleware, opts = {}) middleware_chain.use(middleware, opts) end |
Instance Method Details
#publish(message, **options) ⇒ Object
149 150 151 |
# File 'lib/lepus/producer.rb', line 149 def publish(, **) self.class.publish(, **) end |