Class: SimplyGenius::Atmos::PluginManager

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport
Defined in:
lib/simplygenius/atmos/plugin_manager.rb

Defined Under Namespace

Classes: OutputFilterCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugins) ⇒ PluginManager

Returns a new instance of PluginManager.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 13

def initialize(plugins)
  @plugins = []
  Array(plugins).each do |plugin|
    if plugin.is_a?(String)
      name = plugin
      plugin = SettingsHash.new
      plugin[:name] = name
    elsif plugin.is_a?(Hash)
      plugin = SettingsHash.new(plugin)
      if plugin[:name].blank?
        logger.error "Invalid plugin definition, :name missing: #{plugin}"
        next
      end
    else
      logger.error "Invalid plugin definition: #{plugin}"
      next
    end
    @plugins << plugin
  end

  @plugin_classes = Set.new
  @plugin_instances = []
  @output_filters = {}
end

Instance Attribute Details

#pluginsObject (readonly)

Returns the value of attribute plugins.



11
12
13
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 11

def plugins
  @plugins
end

Instance Method Details

#load_plugin(plugin) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 57

def load_plugin(plugin)
  begin
    name = plugin[:name]
    require_name = plugin[:require] || name.gsub('-', '/')
    logger.debug("Loading plugin #{name} as #{require_name}")
    require require_name
  rescue LoadError, StandardError => e
    logger.log_exception e, "Failed to load atmos plugin: #{name} - #{e.message}"
  end
end

#load_pluginsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 38

def load_plugins
  @plugins.each do |plugin|
    load_plugin(plugin)

    # Check for new plugin classes after each plugin load so that we can
    # initialize them with their own config hash
    Plugin.descendants.each do |plugin_class|
      begin
        if ! @plugin_classes.include?(plugin_class)
          @plugin_classes << plugin_class
          @plugin_instances << plugin_class.new(self, plugin)
        end
      rescue StandardError => e
        logger.log_exception e, "Failed to initialize plugin: #{plugin_class}"
      end
    end
  end
end

#output_filters(type, context) ⇒ Object



78
79
80
81
82
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 78

def output_filters(type, context)
  validate_output_filter_type(type)
  @output_filters[type.to_sym] ||= []
  return OutputFilterCollection.new(@output_filters[type.to_sym].collect {|clazz| clazz.new(context) })
end

#register_output_filter(type, clazz) ⇒ Object



72
73
74
75
76
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 72

def register_output_filter(type, clazz)
  validate_output_filter_type(type)
  @output_filters[type.to_sym] ||= []
  @output_filters[type.to_sym] << clazz
end

#validate_output_filter_type(type) ⇒ Object



68
69
70
# File 'lib/simplygenius/atmos/plugin_manager.rb', line 68

def validate_output_filter_type(type)
  raise "Invalid output filter type #{type}, must be one of [:stdout, :stderr]" unless [:stdout, :stderr].include?(type)
end