Class: Nanoc2::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/plugin.rb

Overview

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

Direct Known Subclasses

BinaryFilter, DataSource, Extra::VCS, Filter, Router

Constant Summary collapse

MAP =
{}

Class Method Summary collapse

Class Method Details

.identifier(identifier = nil) ⇒ Object

Sets or returns the identifier for this plugin.

When given an identifier symbols, sets the identifier for this plugin. When given nothing, returns the identifier for this plugin.



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

def identifier(identifier=nil)
  # Initialize
  if !instance_variables.include?('@identifiers') && !instance_variables.include?(:'@identifiers')
    @identifiers = []
  end

  if identifier.nil?
    @identifiers.first
  else
    @identifiers = [ identifier ]
    register(identifier, self)
  end
end

.identifiers(*identifiers) ⇒ Object

Sets or returns the identifiers for this plugin.

When given a list of identifier symbols, sets the identifiers for this plugin. When given nothing, returns an array of identifier symbols for this plugin.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nanoc2/base/plugin.rb', line 20

def identifiers(*identifiers)
  # Initialize
  if !instance_variables.include?('@identifiers') && !instance_variables.include?(:'@identifiers')
    @identifiers = []
  end

  if identifiers.empty?
    @identifiers
  else
    @identifiers = identifiers
    @identifiers.each { |i| register(i, self) }
  end
end

.named(name) ⇒ Object

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



62
63
64
65
# File 'lib/nanoc2/base/plugin.rb', line 62

def named(name)
  MAP[self] ||= {}
  MAP[self][name.to_sym]
end

.register(name, klass) ⇒ Object

Registers the given class klass with the given name. This will allow the named method to find the class.



54
55
56
57
# File 'lib/nanoc2/base/plugin.rb', line 54

def register(name, klass)
  MAP[klass.superclass] ||= {}
  MAP[klass.superclass][name.to_sym] = klass
end