Class | Webgen::PluginLoader |
In: |
lib/webgen/plugin.rb
|
Parent: | Object |
Responsible for loading plugins classes. Each PluginLoader has an array of plugin classes which it loaded. Several methods for loading plugins classes are available.
loaded_files | [R] | The files loaded by this PluginLoader instance. |
optional_parts | [R] | The optional parts managed (loaded or not) by this PluginLoader instance. |
plugin_classes | [R] | The plugin classes loaded by this PluginLoader instance. |
Creates a new PluginLoader instance. The wrapper_module is used when loading the plugins so that they do not pollute the global namespace.
# File lib/webgen/plugin.rb, line 200 200: def initialize( wrapper_module = Module.new ) 201: @plugin_classes = [] 202: @loaded_files = [] 203: @wrapper_module = wrapper_module 204: @optional_parts = {} 205: end
Checks if this PluginLoader has loaded a plugin called name.
# File lib/webgen/plugin.rb, line 250 250: def has_plugin?( name ) 251: plugin_class_for_name( name ) != nil 252: end
Loads all plugin classes which get declared in the given block. Be aware that this method does not put the classes into the wrapper module!
# File lib/webgen/plugin.rb, line 235 235: def load_from_block( &block ) 236: cont, klass = catch( :plugin_class_found ) do 237: cont, name, options = catch( :load_optional_part ) { yield } 238: if cont 239: @optional_parts[name] = options 240: cont.call 241: end 242: nil # return value for catch, means: all classes processed 243: end 244: add_plugin_class( klass ) unless klass.nil? 245: cont.call if cont 246: sort_out_base_plugins 247: end
Loads all plugin classes in the given dir and in its subdirectories. Before require is actually called the path is trimmed: if trimpath matches the beginning of the string, trimpath is deleted from it. The loaded classes are wrapped in the wrapper module and won‘t pollute the namespace.
# File lib/webgen/plugin.rb, line 211 211: def load_from_dir( dir, trimpath = '') 212: Find.find( dir ) do |file| 213: trimmedFile = file.gsub(/^#{trimpath}/, '') 214: Find.prune unless File.directory?( file ) || (/\.rb$/ =~ file) 215: load_from_file( trimmedFile ) if File.file?( file ) && /\.rb$/ =~ file 216: end 217: end
Loads all plugin classes specified in the file.The loaded classes are wrapped in the wrapper module and won‘t pollute the namespace.
# File lib/webgen/plugin.rb, line 221 221: def load_from_file( file ) 222: load_from_block do 223: cont, file = catch( :load_plugin_file? ) do 224: load_plugin( file ) 225: nil 226: end 227: do_load_file = !@loaded_files.include?( file ) && !DEFAULT_PLUGIN_LOADER.loaded_files.include?( file ) unless file.nil? 228: @loaded_files << file unless file.nil? || @loaded_files.include?( file ) 229: cont.call( @wrapper_module, do_load_file ) if cont 230: end 231: end
Returns the plugin class called name or nil if it is not found.
# File lib/webgen/plugin.rb, line 255 255: def plugin_class_for_name( name ) 256: @plugin_classes.find {|p| p.plugin_name == name} 257: end
# File lib/webgen/plugin.rb, line 263 263: def add_plugin_class( klass ) 264: @plugin_classes << klass 265: end