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



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.

Returns:



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

Returns:

  • (Array<Hash>)

    A list of all plugins in the format described



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.

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



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.

Parameters:

  • klass (Class)

    The class of the plugin to return

Returns:

  • (Enumerable<Class>)

    A collection of class plugins



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

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



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)

Deprecated.

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.

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.



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