Class: Nanoc3::Plugin

Nanoc3::Plugin is the superclass for all plugins, such as filters (Nanoc3::Filter), data sources (Nanoc3::DataSource) and VCSes (Nanoc3::Extra::VCS). Each plugin has one or more unique identifiers, and several methods in this class provides functionality for finding plugins with given identifiers.

Constants

MAP
{}

Public Visibility

Public Class Method Summary

all

Returns a list of all plugins in the following format:.

named(name)

Returns the the plugin with the given name.

register(superclass, class_or_name, *identifiers)

Registers the given class as a plugin.

Public Class Method Details

all

public all

Returns a list of all plugins in the following format:

  { :class => ..., :superclass => ..., :identifiers => ... }
[View source]


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nanoc3/base/plugin.rb', line 57

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

named

public named(name)

Returns the the plugin with the given name. Only subclasses of this class will be searched. For example, calling this method on Nanoc3::Filter will cause only Nanoc3::Filter subclasses to be searched.

[View source]


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanoc3/base/plugin.rb', line 39

def named(name)
  # Initialize
  MAP[self] ||= {}

  # Lookup
  class_or_name = MAP[self][name.to_sym]

  # Get class
  if class_or_name.is_a?(String)
    class_or_name.scan(/\w+/).inject(self) { |memo, part| memo.const_get(part) }
  else
    class_or_name
  end
end

register

public register(superclass, class_or_name, *identifiers)

Registers the given class as a plugin.

superclass:The superclass of the plugin. For example: Nanoc::Filter, Nanoc::VCS.
class_or_name: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:One or more symbols identifying the class. For example: :haml, :erb.
[View source]


28
29
30
31
32
33
34
# File 'lib/nanoc3/base/plugin.rb', line 28

def register(superclass, class_or_name, *identifiers)
  MAP[superclass] ||= {}

  identifiers.each do |identifier|
    MAP[superclass][identifier.to_sym] = class_or_name
  end
end
Generated on Sunday, August 09 2009 at 01:43:08 PM by YARD 0.2.3.2 (ruby-1.8.7).