Class: DDPlugin::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/ddplugin/registry.rb

Overview

The registry is responsible for keeping track of all loaded plugins.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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 Registry.



15
16
17
18
# File 'lib/ddplugin/registry.rb', line 15

def initialize
  @identifiers_to_classes = Hash.new { |h, k| h[k] = {}.dup }
  @classes_to_identifiers = Hash.new { |h, k| h[k] = {}.dup }
end

Class Method Details

.instanceDDPlugin::Registry

Returns the shared DDPlugin::Registry instance, creating it if none exists yet.

Returns:



10
11
12
# File 'lib/ddplugin/registry.rb', line 10

def self.instance
  @instance ||= new
end

Instance Method Details

#find(root_class, identifier) ⇒ Class?

Finds the class that is a descendant of the given class and has the given identifier.

Parameters:

  • root_class (Class)

    The root class of the class to return

  • identifier (Symbol)

    The identifier of the class to return

Returns:

  • (Class, nil)

    The class with the given identifier



57
58
59
60
61
# File 'lib/ddplugin/registry.rb', line 57

def find(root_class, identifier)
  identifier = identifier.to_sym
  @identifiers_to_classes[root_class] ||= {}
  @identifiers_to_classes[root_class][identifier]
end

#find_all(root_class) ⇒ Enumerable<Class>

Returns all classes that are registered descendants of the given class.

Parameters:

  • root_class (Class)

    The root class of the class to return

Returns:

  • (Enumerable<Class>)

    A collection of registered classes



68
69
70
71
# File 'lib/ddplugin/registry.rb', line 68

def find_all(root_class)
  @identifiers_to_classes[root_class] ||= {}
  @identifiers_to_classes[root_class].values.uniq
end

#identifiers_of(root_class, klass) ⇒ Array<Symbol>

Returns The identifiers for the given class.

Parameters:

  • root_class (Class)

    The root class of the class to find the identifiers for

  • klass (Class)

    The class to get the identifiers for

Returns:

  • (Array<Symbol>)

    The identifiers for the given class



44
45
46
47
# File 'lib/ddplugin/registry.rb', line 44

def identifiers_of(root_class, klass)
  @classes_to_identifiers[root_class] ||= {}
  @classes_to_identifiers[root_class].fetch(klass, [])
end

#register(root_class, klass, *identifiers) ⇒ void

This method returns an undefined value.

Registers the given class as a plugin.

Parameters:

  • root_class (Class)

    The root class of the class to register

  • klass (Class)

    The class to register

  • identifiers (Symbol)

    One or more symbols identifying the class



29
30
31
32
33
34
35
36
# File 'lib/ddplugin/registry.rb', line 29

def register(root_class, klass, *identifiers)
  identifiers.map(&:to_sym).each do |identifier|
    @classes_to_identifiers[root_class][klass] ||= []

    @identifiers_to_classes[root_class][identifier] = klass
    @classes_to_identifiers[root_class][klass] << identifier
  end
end