Class: Nanoc3::PluginRegistry

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Constructor Details

#initializePluginRegistry

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

.instanceNanoc3::PluginRegistry

Returns the shared Nanoc3::PluginRegistry instance, creating it if none exists yet.

Returns:



68
69
70
# File 'lib/nanoc3/base/plugin_registry.rb', line 68

def self.instance
  @instance ||= self.new
end

Instance Method Details

#allArray<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 => ... }

Returns:

  • (Array<Hash>)

    A list of all plugins in the format described



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.

Parameters:

  • name (Symbol)

    The name of the plugin to return

Returns:

  • (Class, nil)

    The plugin with 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

Deprecated.

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.

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: ‘Nanoc3::Filters::ERB`, `“Nanoc3::Filters::Haml”`.

  • identifiers (Symbol)

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



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