Class: Omnes::Registry

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#registrationsObject (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.

Parameters:

  • event_name (Symbol)

Raises:



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_namesArray<Symbol>

Returns an array with all registered event names

Returns:

  • (Array<Symbol>)


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.

Parameters:

  • event_name (Symbol)

Returns:

  • (Boolean)


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

Parameters:

  • event_name (Symbol)

Returns:



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

Parameters:

  • event_name (Symbol)


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