Class: DynamicRegistrar::Registrar
- Inherits:
-
Object
- Object
- DynamicRegistrar::Registrar
- Defined in:
- lib/dynamic_registrar/registrar.rb
Overview
Controls the registration and dispatching of callbacks
Constant Summary collapse
- @@registration_guard =
Mutex to provide for no double or overwritten registration guarantee even in multithreaded environments
Mutex.new
Instance Attribute Summary collapse
-
#default_registration_namespace ⇒ Object
readonly
The default namespace used when calling Registrar#register! without specifying a namespace.
Instance Method Summary collapse
-
#dispatch(name, namespace = nil) ⇒ Hash
Dispatch message to given callback.
-
#initialize(default_registration_namespace) ⇒ Registrar
constructor
Create a new DynamicRegistrar::Registrar.
-
#register!(name, namespace = @default_registration_namespace, &callback_proc) ⇒ Object
Register a new callback procedure.
-
#registered?(name) ⇒ Boolean
Query if a callback of given name is registered in any namespace.
-
#registered_callbacks ⇒ Object
The collection of callbacks currently registered within the Registrar.
-
#registered_in_namespace?(name, namespace) ⇒ Boolean
Query if a callback of given name is registered in given namespace.
Constructor Details
#initialize(default_registration_namespace) ⇒ Registrar
Create a new DynamicRegistrar::Registrar
25 26 27 28 |
# File 'lib/dynamic_registrar/registrar.rb', line 25 def initialize default_registration_namespace @default_registration_namespace = default_registration_namespace @registered_callbacks = Hash.new end |
Instance Attribute Details
#default_registration_namespace ⇒ Object (readonly)
The default namespace used when calling Registrar#register! without specifying a namespace
14 15 16 |
# File 'lib/dynamic_registrar/registrar.rb', line 14 def default_registration_namespace @default_registration_namespace end |
Instance Method Details
#dispatch(name, namespace = nil) ⇒ Hash
Dispatch message to given callback. All named callbacks matching the name will be run in all namespaces in indeterminate order
46 47 48 49 50 51 52 53 54 |
# File 'lib/dynamic_registrar/registrar.rb', line 46 def dispatch name, namespace = nil responses = Hash.new namespaces_to_search = namespace ? [namespace] : namespaces namespaces_to_search.each do |namespace| responses[namespace] ||= Hash.new responses[namespace][name] = @registered_callbacks[namespace][name].call if @registered_callbacks[namespace].has_key?(name) end responses end |
#register!(name, namespace = @default_registration_namespace, &callback_proc) ⇒ Object
Register a new callback procedure. This method is thread-safe.
33 34 35 36 37 38 39 |
# File 'lib/dynamic_registrar/registrar.rb', line 33 def register! name, namespace = @default_registration_namespace, &callback_proc @@registration_guard.synchronize do raise Errors::RegistrationConflictError if registered_in_namespace? name, namespace @registered_callbacks[namespace] ||= Hash.new @registered_callbacks[namespace][name] = callback_proc end end |
#registered?(name) ⇒ Boolean
Query if a callback of given name is registered in any namespace
58 59 60 61 62 63 |
# File 'lib/dynamic_registrar/registrar.rb', line 58 def registered? name registration_map = namespaces.map do |namespace| registered_in_namespace? name, namespace end registration_map.any? end |
#registered_callbacks ⇒ Object
The collection of callbacks currently registered within the Registrar
17 18 19 20 21 |
# File 'lib/dynamic_registrar/registrar.rb', line 17 def registered_callbacks @@registration_guard.synchronize do @registered_callbacks end end |
#registered_in_namespace?(name, namespace) ⇒ Boolean
Query if a callback of given name is registered in given namespace
68 69 70 |
# File 'lib/dynamic_registrar/registrar.rb', line 68 def registered_in_namespace? name, namespace @registered_callbacks.has_key?(namespace) && @registered_callbacks[namespace].has_key?(name) end |