Class: Nanoc::Int::PluginRegistry Private

Inherits:
Object
  • Object
show all
Extended by:
Memoization
Defined in:
lib/nanoc/base/plugin_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The class responsible for keeping track of all loaded plugins, such as filters (Filter) and data sources (DataSource).

Defined Under Namespace

Modules: PluginMethods

Constant Summary

Constants included from Memoization

Memoization::NONE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoization

memoize

Constructor Details

#initializePluginRegistry

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.

Creates a new plugin registry. This should usually not be necessary; it is recommended to use the shared instance (obtained from instance).



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

def initialize
  @identifiers_to_classes = {}
  @classes_to_identifiers = {}
end

Class Method Details

.instanceNanoc::Int::PluginRegistry

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 the shared Nanoc::Int::PluginRegistry instance, creating it if none exists yet.

Returns:



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

def self.instance
  @instance ||= new
end

Instance Method Details

#allArray<Hash>

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



168
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
# File 'lib/nanoc/base/plugin_registry.rb', line 168

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

#find(klass, name) ⇒ Class?

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.

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



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

def find(klass, name)
  @identifiers_to_classes[klass] ||= {}
  resolve(@identifiers_to_classes[klass][name.to_sym], klass)
end

#find_all(klass) ⇒ Enumerable<Class>

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 all plugins of the given class.

Parameters:

  • klass (Class)

    The class of the plugin to return

Returns:

  • (Enumerable<Class>)

    A collection of class plugins



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

def find_all(klass)
  @identifiers_to_classes[klass] ||= {}
  res = {}
  @identifiers_to_classes[klass].each_pair { |k, v| res[k] = resolve(v, k) }
  res
end

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

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 An array of identifiers for the given class.

Parameters:

  • superclass (Class)

    The superclass of the plugin. For example: Filter.

  • klass (Class)

    The class to get the identifiers for.

Returns:

  • (Array<Symbol>)

    An array of identifiers for the given class



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

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

#register(superclass, class_or_name, *identifiers) ⇒ void

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.

This method returns an undefined value.

Registers the given class as a plugin.

Parameters:

  • superclass (Class)

    The superclass of the plugin. For example: Filter.

  • 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`.



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

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