Class: Omnes::Registry
- Inherits:
-
Object
- Object
- Omnes::Registry
- Defined in:
- lib/omnes/registry.rb
Overview
Registry of known event names
Before publishing or subscribing to an event, its name must be registered to the instance associated with the bus (see Bus#register).
Defined Under Namespace
Classes: Registration
Instance Attribute Summary collapse
-
#registrations ⇒ Object
readonly
Returns the value of attribute registrations.
Instance Method Summary collapse
-
#check_event_name(event_name) ⇒ Object
Checks whether given event name is present in the registry.
-
#event_names ⇒ Array<Symbol>
Returns an array with all registered event names.
-
#initialize(registrations: []) ⇒ Registry
constructor
A new instance of Registry.
- #register(event_name, caller_location: ) ⇒ Object private
-
#registered?(event_name) ⇒ Boolean
Returns whether a given event name is registered.
-
#registration(event_name) ⇒ Omnes::Registry::Registration?
Returns the registration, if present, for the event name.
-
#unregister(event_name) ⇒ Object
Removes an event name from the registry.
Constructor Details
#initialize(registrations: []) ⇒ Registry
Returns a new instance of Registry.
31 32 33 |
# File 'lib/omnes/registry.rb', line 31 def initialize(registrations: []) @registrations = registrations end |
Instance Attribute Details
#registrations ⇒ Object (readonly)
Returns the value of attribute registrations.
29 30 31 |
# File 'lib/omnes/registry.rb', line 29 def registrations @registrations end |
Instance Method Details
#check_event_name(event_name) ⇒ Object
Checks whether given event name is present in the registry
Use #registered? for a predicate version of it.
90 91 92 93 94 |
# File 'lib/omnes/registry.rb', line 90 def check_event_name(event_name) return if registered?(event_name) raise UnknownEventError.new(event_name: event_name, known_events: event_names) end |
#event_names ⇒ Array<Symbol>
Returns an array with all registered event names
59 60 61 |
# File 'lib/omnes/registry.rb', line 59 def event_names registrations.map(&:event_name) end |
#register(event_name, caller_location: ) ⇒ 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.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/omnes/registry.rb', line 36 def register(event_name, caller_location: caller_locations(1)[0]) raise InvalidEventNameError.new(event_name: event_name) unless valid_event_name?(event_name) registration = registration(event_name) raise AlreadyRegisteredEventError.new(event_name: event_name, registration: registration) if registration Registration.new(event_name: event_name, caller_location: caller_location).tap do |reg| @registrations << reg end end |
#registered?(event_name) ⇒ Boolean
Returns whether a given event name is registered
Use #check_event_name for a raising version of it.
79 80 81 |
# File 'lib/omnes/registry.rb', line 79 def registered?(event_name) !registration(event_name).nil? end |
#registration(event_name) ⇒ Omnes::Registry::Registration?
Returns the registration, if present, for the event name
68 69 70 |
# File 'lib/omnes/registry.rb', line 68 def registration(event_name) registrations.find { |reg| reg.event_name == event_name } end |
#unregister(event_name) ⇒ Object
Removes an event name from the registry
50 51 52 53 54 |
# File 'lib/omnes/registry.rb', line 50 def unregister(event_name) check_event_name(event_name) @registrations.delete_if { |regs| regs.event_name == event_name } end |