Class: DynamicRegistrar::Registrar

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

Instance Method Summary collapse

Constructor Details

#initialize(default_registration_namespace) ⇒ Registrar

Create a new DynamicRegistrar::Registrar

Parameters:

  • default_registration_namespace (Symbol)

    The default namespace in which to register callbacks. Should not be set to nil.



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_namespaceObject (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

Parameters:

  • name (Symbol)

    The name of the callback

  • namespace (Symbol) (defaults to: nil)

    The namespace in which the named callback should be found. Optional, if omitted then all matching named callbacks in all namespaces will be executed

Returns:

  • (Hash)

    A hash whose keys are the namespaces in which callbacks were executed, and whose values are the results of those executions. If empty, then no callbacks were executed.



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.

Parameters:

  • name (Symbol)

    The name of the callback to register

  • namespace (Symbol) (defaults to: @default_registration_namespace)

    The namespace in which to register the callback



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

Parameters:

  • name (Symbol)

    The name of the callback to check

Returns:

  • (Boolean)


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_callbacksObject

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

Parameters:

  • name (Symbol)

    The name of the callback to check

  • namespace (Symbol)

    The name of the namespace in which to check

Returns:

  • (Boolean)


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