Class | Webgen::PluginManager |
In: |
lib/webgen/plugin.rb
|
Parent: | Object |
Once plugin classes are loaded, they are ready to get used. This class is used for instantiating plugins and their dependencies in the correct order and provide the plugins the facility for retrieving current parameter values.
logger | [RW] | The logger used by the instance and the plugin objects. |
plugin_config | [RW] | Used for retrieving current plugin parameter values. Should be set before calling init. |
plugin_loaders | [R] | Used for plugin dependency resolution. |
plugins | [R] | A hash of all instantiated plugins. |
Creates a new PluginManager instance.
# File lib/webgen/plugin.rb, line 340 340: def initialize( plugin_loaders = [], plugin_classes = [] ) 341: @logger = nil 342: @plugins = {} 343: @plugin_classes = {} 344: @plugin_loaders = plugin_loaders 345: @plugin_config = nil 346: add_plugin_classes( plugin_classes ) 347: end
Returns the plugin instance for the plugin called plugin_name.
# File lib/webgen/plugin.rb, line 377 377: def []( plugin_name ) 378: @plugins[plugin_name] 379: end
Adds all Plugin classes in the array plugins and their dependencies.
# File lib/webgen/plugin.rb, line 355 355: def add_plugin_classes( plugins ) 356: deps = dependent_plugins( plugins ) 357: (plugins + deps).each {|p| @plugin_classes[p.plugin_name] = p } 358: end
Instantiates the plugins in the correct order, except the classes which have the plugin info +:instantiate+ set to false.
# File lib/webgen/plugin.rb, line 362 362: def init 363: @plugins = {} 364: #precalculate_param_values! #TODO: maybe activate this feature 365: dep = DependencyHash.new 366: @plugin_classes.each {|name, plugin| dep[name] = plugin.config.dependencies } 367: dep.tsort.each do |plugin_name| 368: config = plugin_class_for_name( plugin_name ).config 369: unless config.infos.has_key?(:instantiate) && !config.infos[:instantiate] 370: log_msg( :debug, 'PluginManager#init') { "Creating instance of plugin #{plugin_name}" } 371: @plugins[plugin_name] = config.plugin_klass.new( self ) 372: end 373: end 374: end
Logs the result of executing block under the severity level sev_level. The parameter source identifies the source of the log message.
# File lib/webgen/plugin.rb, line 415 415: def log_msg( sev_level, source, &block ) 416: @logger.send( sev_level, source, &block ) if @logger 417: nil 418: end
Returns the options hash for the given optional library.
# File lib/webgen/plugin.rb, line 409 409: def optional_part( name ) 410: @plugin_loaders.each {|pl| return pl.optional_parts[name] if pl.optional_parts.has_key?( name ) } 411: end
Returns the parameter param for the plugin called plugin_name.
# File lib/webgen/plugin.rb, line 382 382: def param_for_plugin( plugin_name, param ) 383: plugin = plugin_class_for_name( plugin_name ) 384: raise PluginParamNotFound.new( plugin_name, param ) if plugin.nil? 385: 386: if @precalculated_param_values 387: value = @precalculated_param_values[plugin_name][param] 388: else 389: value = PluginParamValueNotFound 390: plugin.ancestor_classes.each do |plugin_klass| 391: value = get_plugin_param_value( plugin_klass.plugin_name, plugin_klass.config, param ) 392: break unless value == PluginParamValueNotFound 393: end 394: end 395: 396: if value != PluginParamValueNotFound 397: value 398: else 399: raise PluginParamNotFound.new( plugin.plugin_name, param ) 400: end 401: end
Returns the plugin class for the plugin plugin_name.
# File lib/webgen/plugin.rb, line 404 404: def plugin_class_for_name( plugin_name ) 405: @plugin_classes[plugin_name] 406: end
A list of all plugins that should get instantiated.
# File lib/webgen/plugin.rb, line 350 350: def plugin_classes 351: @plugin_classes.values 352: end
# File lib/webgen/plugin.rb, line 437 437: def dependent_plugins( classes ) 438: deps = [] 439: classes.each do |plugin| 440: plugin.config.dependencies.each do |dep| 441: p = nil 442: @plugin_loaders.each {|loader| p = loader.plugin_class_for_name( dep ); break unless p.nil? } 443: if p.nil? 444: raise PluginNotFound.new( dep, plugin.plugin_name ) 445: else 446: deps << p unless deps.include?( p ) 447: end 448: end 449: end 450: deps 451: end
# File lib/webgen/plugin.rb, line 453 453: def get_plugin_param_value( plugin_name, config, param ) 454: return PluginParamValueNotFound unless config.params.has_key?( param ) 455: 456: value = PluginParamValueNotFound 457: value = @plugin_config.param_for_plugin( plugin_name, param ) if @plugin_config 458: value = config.params[param].default if value == PluginParamValueNotFound 459: 460: value 461: end
# File lib/webgen/plugin.rb, line 424 424: def precalculate_param_values! 425: @precalculated_param_values = nil 426: precalc = Hash.new {|h,k| h[k] = Hash.new( PluginParamValueNotFound ) } 427: @plugin_classes.each do |name, klass| 428: klass.ancestor_classes.each do |a_klass| 429: a_klass.config.params.each do |p_name, p_data| 430: precalc[name][p_name] = param_for_plugin( name, p_name ) 431: end 432: end 433: end 434: @precalculated_param_values = precalc 435: end