Class: Nanoc3::PluginRegistry
- Inherits:
-
Object
- Object
- Nanoc3::PluginRegistry
- Defined in:
- lib/nanoc3/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
-
.instance ⇒ Nanoc3::PluginRegistry
Returns the shared PluginRegistry instance, creating it if none exists yet.
Instance Method Summary collapse
-
#all ⇒ Array<Hash>
Returns a list of all plugins.
-
#find(klass, name) ⇒ Class?
Finds the plugin that is a subclass of the given class and has the given name.
-
#initialize ⇒ PluginRegistry
constructor
Creates a new plugin registry.
-
#named(name) ⇒ Object
deprecated
Deprecated.
Use #find instead
-
#register(superclass, class_or_name, *identifiers) ⇒ void
Registers the given class as a plugin.
Constructor Details
#initialize ⇒ PluginRegistry
Creates a new plugin registry. This should usually not be necessary; it is recommended to use the shared instance (obtained from instance).
75 76 77 |
# File 'lib/nanoc3/base/plugin_registry.rb', line 75 def initialize @map = {} end |
Class Method Details
.instance ⇒ Nanoc3::PluginRegistry
Returns the shared Nanoc3::PluginRegistry instance, creating it if none exists yet.
68 69 70 |
# File 'lib/nanoc3/base/plugin_registry.rb', line 68 def self.instance @instance ||= self.new end |
Instance Method Details
#all ⇒ Array<Hash>
Returns a list of all plugins. The returned list of plugins is an array with array elements in the following format:
{ :class => ..., :superclass => ..., :identifiers => ... }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/nanoc3/base/plugin_registry.rb', line 128 def all plugins = [] @map.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 |
#find(klass, name) ⇒ Class?
Finds the plugin that is a subclass of the given class and has the given name.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/nanoc3/base/plugin_registry.rb', line 107 def find(klass, name) # Initialize @map[klass] ||= {} # Lookup class_or_name = @map[klass][name.to_sym] # Get class if class_or_name.is_a?(String) class_or_name.scan(/\w+/).inject(klass) { |memo, part| memo.const_get(part) } else class_or_name end end |
#named(name) ⇒ Object
Use #find instead
156 157 158 |
# File 'lib/nanoc3/base/plugin_registry.rb', line 156 def named(name) find(self, name) end |
#register(superclass, class_or_name, *identifiers) ⇒ void
This method returns an undefined value.
Registers the given class as a plugin.
93 94 95 96 97 98 99 |
# File 'lib/nanoc3/base/plugin_registry.rb', line 93 def register(superclass, class_or_name, *identifiers) @map[superclass] ||= {} identifiers.each do |identifier| @map[superclass][identifier.to_sym] = class_or_name end end |