module Webgen::ExtensionManager
Provides common functionality for extension manager classes.
This module is intended for mixing into an extension manager class. An example for an extension manage class is the Webgen::ContentProcessor
class which manages all content processors. Extension manager classes provide methods for registering a certain type of extension and invoking methods on them via a common interface.
It is assumed that one wants to associate one or more names with an extension object.
Public Class Methods
Create a new extension manager.
# File lib/webgen/extension_manager.rb 19 def initialize 20 @extensions = {} 21 end
Public Instance Methods
Register a new extension.
Note that this method has to be implemented by classes that include this module. It should register one or more names for an extension object by associating the names with the extension object data (should be an object that responds at least to :object) via the @extensions hash.
The simplest way to achieve this is to use the do_register
method.
# File lib/webgen/extension_manager.rb 36 def register(klass, options = {}, &block) 37 raise NotImplementedError 38 end
Return true
if an extension with the given name has been registered with this manager class.
# File lib/webgen/extension_manager.rb 109 def registered?(name) 110 @extensions.has_key?(name.to_sym) 111 end
Return the meta data of all extensions registered with this manager class.
# File lib/webgen/extension_manager.rb 114 def registered_extensions 115 @extensions.dup 116 end
Private Instance Methods
Automatically perform the necessary steps to register the extension klass
. This involves normalization of the class name and associating the extension name (derived from the class name if not set via the key :name in the options hash) with the extension.
The parameter klass
can either be a String or a Class specifying the class that should be registered.
The parameter options
allows to associate additional information with an extension. The only recognized key is :name (for the extension name). All other keys are ignored and can/should be used by the extension manager class itself.
The parameter allow_block
specifies whether the extension manager allows blocks as extensions. If this parameter is true
and a block is provided, the klass
parameter is not used!
Returns the (possibly automatically generated) name for the extension.
# File lib/webgen/extension_manager.rb 56 def do_register(klass, options = {}, allow_block = true, &block) 57 if !allow_block && block_given? 58 raise ArgumentError, "The extension manager '#{self.class.name}' does not support blocks on #register" 59 end 60 klass, klass_name = normalize_class_name(klass, !block_given?) 61 name = (options[:name] || Webgen::Utils.snake_case(klass_name)).to_sym 62 @extensions[name] = OpenStruct.new(:object => (block_given? ? block : klass)) 63 name 64 end
Return the extension data for the extension name
.
# File lib/webgen/extension_manager.rb 95 def ext_data(name) 96 @extensions[name.to_sym] 97 end
Return the registered object for the extension name
.
This method also works in the case that name
is a String referencing a class. The method assumes that @extensions is an array where the registered object is the first element!
# File lib/webgen/extension_manager.rb 86 def extension(name) 87 raise Webgen::Error.new("No extension called '#{name}' registered for the '#{self.class.name}' extension manager") unless registered?(name) 88 name = name.to_sym 89 ext = @extensions[name].object 90 ext.kind_of?(String) ? @extensions[name].object = resolve_class(ext) : ext 91 end
Return a complete class name (including the hierarchy part) based on klass
and the class name without the hierarchy part.
If the parameter do_autoload
is true
and the klass
is defined under this class, it is autoloaded by turning the class name into a path name (See Webgen::Utils.snake_case
).
# File lib/webgen/extension_manager.rb 72 def normalize_class_name(klass, do_autoload = true) 73 klass = (klass.kind_of?(Class) || klass.include?('::') ? klass : "#{self.class.name}::#{klass}") 74 klass_name = klass.to_s.split(/::/).last 75 if do_autoload && klass.to_s.start_with?(self.class.name) && klass_name =~ /^[A-Z]/ 76 self.class.autoload(klass_name.to_sym, Webgen::Utils.snake_case(klass.to_s)) 77 end 78 [klass, klass_name] 79 end
If class_or_name
is a String, it is taken as the name of a class and is resolved. Else returns class_or_name
.
# File lib/webgen/extension_manager.rb 102 def resolve_class(class_or_name) 103 String === class_or_name ? Webgen::Utils.const_for_name(class_or_name) : class_or_name 104 end