Class: Nanoc::PluginRegistry

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

Instance Method Summary (collapse)

Methods included from Memoization

memoize

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



99
100
101
102
# File 'lib/nanoc/base/plugin_registry.rb', line 99

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.

Returns:



92
93
94
# File 'lib/nanoc/base/plugin_registry.rb', line 92

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 => … }

Returns:

  • (Array<Hash>)

    A list of all plugins in the format described



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/nanoc/base/plugin_registry.rb', line 169

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(&: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.

Parameters:

  • klass (Class)

    The class of the plugin to return

  • name (Symbol)

    The name of the plugin to return

Returns:

  • (Class, nil)

    The plugin with the given name



146
147
148
149
# File 'lib/nanoc/base/plugin_registry.rb', line 146

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.

Parameters:

  • klass (Class)

    The class of the plugin to return

Returns:

  • (Enumerable<Class>)

    A collection of class plugins



156
157
158
159
160
161
# File 'lib/nanoc/base/plugin_registry.rb', line 156

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

Parameters:

  • superclass (Class)

    The superclass of the plugin. For example: Filter, Extra::VCS.

  • klass (Class)

    The class to get the identifiers for.

Returns:

  • (Array<Symbol>)

    An array of identifiers for the given class



134
135
136
# File 'lib/nanoc/base/plugin_registry.rb', line 134

def identifiers_of(superclass, klass)
  (@classes_to_identifiers[superclass] || {})[name_for_class(klass)] || []
end

- (Object) named(name)

Deprecated.

Use #find instead



197
198
199
# File 'lib/nanoc/base/plugin_registry.rb', line 197

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.

Parameters:

  • superclass (Class)

    The superclass of the plugin. For example: Filter, Extra::VCS.

  • class_or_name (Class, String)

    The class to register. This can be a string, in which case it will be automatically converted to a proper class at lookup. For example: Nanoc::Filters::ERB, "Nanoc::Filters::Haml".

  • identifiers (Symbol)

    One or more symbols identifying the class. For example: :haml, :erb.



118
119
120
121
122
123
124
125
126
# File 'lib/nanoc/base/plugin_registry.rb', line 118

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