Class: HybridPlatformsConductor::Plugins

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, LoggerHelpers
Defined in:
lib/hybrid_platforms_conductor/plugins.rb

Overview

Give a simple and harmonized way to access to plugins, whether they are in the common repository or in other gems

Constant Summary

Constants included from LoggerHelpers

LoggerHelpers::LEVELS_MODIFIERS, LoggerHelpers::LEVELS_TO_STDERR

Instance Method Summary collapse

Methods included from LoggerHelpers

#err, #init_loggers, #log_component=, #log_debug?, #log_level=, #out, #section, #set_loggers_format, #stderr_device, #stderr_device=, #stderr_displayed?, #stdout_device, #stdout_device=, #stdout_displayed?, #stdouts_to_s, #with_progress_bar

Constructor Details

#initialize(plugins_type, init_plugin: nil, parse_gems: true, logger: Logger.new(STDOUT), logger_stderr: Logger.new(STDERR)) ⇒ Plugins

Constructor

Parameters
  • plugins_type (Symbol): Plugins type to look for

  • init_plugin (Proc or nil): Proc used to initialize the plugin from the plugin class, or nil if no initialization [default: nil]

    • Parameters
      • plugin_class (Class): The plugin class that has been found

    • Result
      • Object: Corresponding object that will be used as the plugin instance

  • parse_gems (Boolean): Do we parse plugins from gems? [default: true]

  • logger (Logger): Logger to be used [default = Logger.new(STDOUT)]

  • logger_stderr (Logger): Logger to be used for stderr [default = Logger.new(STDERR)]



25
26
27
28
29
30
31
32
33
# File 'lib/hybrid_platforms_conductor/plugins.rb', line 25

def initialize(plugins_type, init_plugin: nil, parse_gems: true, logger: Logger.new(STDOUT), logger_stderr: Logger.new(STDERR))
  init_loggers(logger, logger_stderr)
  @plugins_type = plugins_type
  @init_plugin = init_plugin
  # All the plugins classes we know of this type, per plugin ID
  # Hash<Symbol, Class>
  @plugins = {}
  register_plugins_from_gems if parse_gems
end

Instance Method Details

#[]=(plugin_id, plugin_class) ⇒ Object

Register a new plugin

Parameters
  • plugin_id (Symbol): The plugin ID to register

  • plugin_class (Class): The corresponding plugin class



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hybrid_platforms_conductor/plugins.rb', line 53

def []=(plugin_id, plugin_class)
  if @plugins.key?(plugin_id)
    log_warn "[ #{@plugins_type} ] - A plugin of type #{@plugins_type} named #{plugin_id} is already registered. Can't overwrite #{@plugins[plugin_id]} with #{plugin_class.name}. Will ignore #{plugin_class.name}."
  else
    # Set the logger in the class so that we can use it in class methods
    plugin_class.logger = @logger
    plugin_class.logger_stderr = @logger_stderr
    if plugin_class.valid?
      log_debug "[ #{@plugins_type} ] - Register #{plugin_id} to #{plugin_class.name}."
      @plugins[plugin_id] = @init_plugin.nil? ? plugin_class : @init_plugin.call(plugin_class)
    else
      log_error "[ #{@plugins_type} ] - The plugin #{plugin_id} (#{plugin_class.name}) is missing some dependencies to be activated. Will ignore it."
    end
  end
end