Class: Nanoc::PluginRegistry
- Inherits:
-
Object
- Object
- Nanoc::PluginRegistry
- Extended by:
- Memoization
- Defined in:
- lib/nanoc/base/plugin_registry.rb
Overview
The class responsible for keeping track of all loaded plugins, such as filters (Filter), data sources (DataSource) and VCSes (Extra::VCS).
Defined Under Namespace
Modules: PluginMethods
Class Method Summary (collapse)
-
+ (Nanoc::PluginRegistry) instance
Returns the shared PluginRegistry instance, creating it if none exists yet.
Instance Method Summary (collapse)
-
- (Array<Hash>) all
Returns a list of all plugins.
-
- (Class?) find(klass, name)
Finds the plugin that is a subclass of the given class and has the given name.
-
- (Enumerable<Class>) find_all(klass)
Returns all plugins of the given class.
-
- (Array<Symbol>) identifiers_of(superclass, klass)
An array of identifiers for the given class.
-
- (PluginRegistry) initialize
constructor
Creates a new plugin registry.
-
- (Object) named(name)
deprecated
Deprecated.
Use #find instead
-
- (void) register(superclass, class_or_name, *identifiers)
Registers the given class as a plugin.
Methods included from Memoization
Constructor Details
- (PluginRegistry) initialize
Creates a new plugin registry. This should usually not be necessary; it is recommended to use the shared instance (obtained from instance).
103 104 105 106 |
# File 'lib/nanoc/base/plugin_registry.rb', line 103 def initialize @identifiers_to_classes = {} @classes_to_identifiers = {} end |
Class Method Details
+ (Nanoc::PluginRegistry) instance
Returns the shared Nanoc::PluginRegistry instance, creating it if none exists yet.
96 97 98 |
# File 'lib/nanoc/base/plugin_registry.rb', line 96 def self.instance @instance ||= new end |
Instance Method Details
- (Array<Hash>) all
Returns a list of all plugins. The returned list of plugins is an array with array elements in the following format:
{ :class => …, :superclass => …, :identifiers => … }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/nanoc/base/plugin_registry.rb', line 173 def all plugins = [] @identifiers_to_classes.each_pair do |superclass, submap| submap.each_pair do |identifier, klass| # Find existing plugin existing_plugin = plugins.find do |p| p[:class] == klass && p[:superclass] == superclass end if existing_plugin # Add identifier to existing plugin existing_plugin[:identifiers] << identifier existing_plugin[:identifiers] = existing_plugin[:identifiers].sort_by { |s| s.to_s } else # Create new plugin plugins << { :class => klass, :superclass => superclass, :identifiers => [ identifier ] } end end end plugins end |
- (Class?) find(klass, name)
Finds the plugin that is a subclass of the given class and has the given name.
150 151 152 153 |
# File 'lib/nanoc/base/plugin_registry.rb', line 150 def find(klass, name) @identifiers_to_classes[klass] ||= {} resolve(@identifiers_to_classes[klass][name.to_sym], klass) end |
- (Enumerable<Class>) find_all(klass)
Returns all plugins of the given class.
160 161 162 163 164 165 |
# File 'lib/nanoc/base/plugin_registry.rb', line 160 def find_all(klass) @identifiers_to_classes[klass] ||= {} res = {} @identifiers_to_classes[klass].each_pair { |k, v| res[k] = resolve(v, k) } res end |
- (Array<Symbol>) identifiers_of(superclass, klass)
Returns An array of identifiers for the given class
138 139 140 |
# File 'lib/nanoc/base/plugin_registry.rb', line 138 def identifiers_of(superclass, klass) (@classes_to_identifiers[superclass] || {})[name_for_class(klass)] || [] end |
- (Object) named(name)
Use #find instead
201 202 203 |
# File 'lib/nanoc/base/plugin_registry.rb', line 201 def named(name) find(self, name) end |
- (void) register(superclass, class_or_name, *identifiers)
This method returns an undefined value.
Registers the given class as a plugin.
122 123 124 125 126 127 128 129 130 |
# File 'lib/nanoc/base/plugin_registry.rb', line 122 def register(superclass, class_or_name, *identifiers) @identifiers_to_classes[superclass] ||= {} @classes_to_identifiers[superclass] ||= {} identifiers.each do |identifier| @identifiers_to_classes[superclass][identifier.to_sym] = class_or_name (@classes_to_identifiers[superclass][name_for_class(class_or_name)] ||= []) << identifier.to_sym end end |