Class: Omnes::Subscription
- Inherits:
-
Object
- Object
- Omnes::Subscription
- Defined in:
- lib/omnes/subscription.rb
Overview
Constant Summary collapse
- SINGLE_EVENT_MATCHER =
lambda do |subscribed, candidate| subscribed == candidate.omnes_event_name end
- ALL_EVENTS_MATCHER =
->(_candidate) { true }
Instance Attribute Summary collapse
- #callback ⇒ Object readonly private
- #id ⇒ Object readonly private
- #matcher ⇒ Object readonly private
Class Method Summary collapse
- .random_id ⇒ Object private
- .takes_publication_context?(callable) ⇒ Boolean private
Instance Method Summary collapse
- #call(event, publication_context) ⇒ Object private
-
#initialize(matcher:, callback:, id:) ⇒ Subscription
constructor
private
A new instance of Subscription.
- #matches?(candidate) ⇒ Boolean private
-
#subscriptions ⇒ Object
Returns self within a single-item array.
Constructor Details
#initialize(matcher:, callback:, id:) ⇒ Subscription
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Subscription.
41 42 43 44 45 46 47 |
# File 'lib/omnes/subscription.rb', line 41 def initialize(matcher:, callback:, id:) raise Omnes::InvalidSubscriptionNameError.new(id: id) unless id.is_a?(Symbol) @matcher = matcher @callback = callback @id = id end |
Instance Attribute Details
#callback ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
38 39 40 |
# File 'lib/omnes/subscription.rb', line 38 def callback @callback end |
#id ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
38 39 40 |
# File 'lib/omnes/subscription.rb', line 38 def id @id end |
#matcher ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
38 39 40 |
# File 'lib/omnes/subscription.rb', line 38 def matcher @matcher end |
Class Method Details
.random_id ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
28 29 30 |
# File 'lib/omnes/subscription.rb', line 28 def self.random_id SecureRandom.uuid.to_sym end |
.takes_publication_context?(callable) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
33 34 35 |
# File 'lib/omnes/subscription.rb', line 33 def self.takes_publication_context?(callable) callable.parameters.count == 2 end |
Instance Method Details
#call(event, publication_context) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/omnes/subscription.rb', line 50 def call(event, publication_context) result = nil benchmark = Benchmark.measure do # work around Ruby not being able to tell remaining arity for a curried # function (or uncurrying), because we want to be able to create subscriber # adapters partially applying the subscriber instance result = begin @callback.(event, publication_context) rescue ArgumentError @callback.(event) end end Execution.new(subscription: self, result: result, benchmark: benchmark) end |
#matches?(candidate) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
67 68 69 |
# File 'lib/omnes/subscription.rb', line 67 def matches?(candidate) matcher.(candidate) end |
#subscriptions ⇒ Object
Returns self within a single-item array
This method can be helpful to act polymorphic to an array of subscriptions from an Omnes::Subscriber, usually for testing purposes.
75 76 77 |
# File 'lib/omnes/subscription.rb', line 75 def subscriptions [self] end |