class Threatinator::PluginLoader

Public Class Methods

new() click to toggle source
# File lib/threatinator/plugin_loader.rb, line 7
def initialize()
  @plugin_types_registry = Threatinator::Registry.new
end

Public Instance Methods

each(type = nil) { |t, name, plugin| ... } click to toggle source

Enumerates through all plugins, optionally filtered by type. @yield [type, name, plugin] @yieldparam [Symbol] type @yieldparam [Symbol] name @yieldparam [Object] plugin

# File lib/threatinator/plugin_loader.rb, line 41
def each(type = nil)
  return enum_for(:each, type) unless block_given?
  @plugin_types_registry.each do |t, type_registry|
    unless type.nil?
      next unless type.to_sym == t
    end
    type_registry.each do |name, plugin|
      yield(t, name, plugin)
    end
  end
end
get(type, name) click to toggle source

Retrieves a loaded plugin by type and name. @param [#to_sym] type @param [#to_sym] name @return [Object] plugin

# File lib/threatinator/plugin_loader.rb, line 30
def get(type, name)
  type_registry = @plugin_types_registry.get(type.to_sym)
  return nil if type_registry.nil?
  return type_registry.get(name.to_sym)
end
load_all_plugins() click to toggle source

Loads all plugins @return [Threatinator::PluginLoader] self

# File lib/threatinator/plugin_loader.rb, line 13
def load_all_plugins()
  load_plugins(:*)
  return self
end
load_plugins(type) click to toggle source

Loads all plugins of the specified type @param [#to_sym] type @return [Threatinator::PluginLoader] self

# File lib/threatinator/plugin_loader.rb, line 21
def load_plugins(type)
  load_files(find_plugin_files(type.to_sym))
  return self
end
register_plugin(type, name, plugin) click to toggle source

Registers a plugin with the provided type and name. @param [#to_sym] type @param [#to_sym] name @param [Object] plugin @raise [Threatinator::Exceptions::AlreadyRegisteredError

# File lib/threatinator/plugin_loader.rb, line 64
def register_plugin(type, name, plugin)
  type = type.to_sym
  unless type_registry = @plugin_types_registry.get(type)
    type_registry = @plugin_types_registry.register(type, Threatinator::Registry.new)
  end
  type_registry.register(name.to_sym, plugin)
end
types() click to toggle source

Returns an array of all the types of plugins that are loaded. @return [Array<Symbol>]

# File lib/threatinator/plugin_loader.rb, line 55
def types
  @plugin_types_registry.keys
end

Private Instance Methods

find_plugin_files(type) click to toggle source
# File lib/threatinator/plugin_loader.rb, line 74
def find_plugin_files(type)
  Gem.find_files("threatinator/plugins/#{type}/*.rb")
end
load_files(file_names) click to toggle source
# File lib/threatinator/plugin_loader.rb, line 89
def load_files(file_names)
  file_names.each do |file_name|
    path, type, name = split_file_name(file_name)
    next if path.nil?
    # Don't try to load unit tests as plugins :)
    next if name.end_with?("_spec")
    begin 
      require path
    rescue ::LoadError
      # TODO: Handle plugins inside of gems a bit better. We should try
      #  using only the latest version of a given Gem.
      next
    end

    register_plugin_by_name(type, name)
  end
end
register_plugin_by_name(type, name) click to toggle source
# File lib/threatinator/plugin_loader.rb, line 78
def register_plugin_by_name(type, name)
  plugin_name = "Threatinator::Plugins::#{Threatinator::Util.underscore2cc(type)}::#{Threatinator::Util.underscore2cc(name)}"
  begin
    type_obj = Threatinator::Plugins.const_get(Threatinator::Util.underscore2cc(type))
    plugin = type_obj.const_get(Threatinator::Util.underscore2cc(name))
    register_plugin(type, name, plugin)
  rescue ::NameError => e
    raise Threatinator::Exceptions::PluginLoadError.new("Failed to load plugin '#{plugin_name}'", e)
  end
end
split_file_name(file_name) click to toggle source

@return [Array, nil] an array containing the path, type, and name of the

plugin
# File lib/threatinator/plugin_loader.rb, line 109
def split_file_name(file_name)
  m = file_name.match(/(?<=(\A|\/))(?<path>threatinator\/plugins\/(?<type>[^\/]+)\/(?<name>[^\/\.]+))\.rb$/)
  return nil if m.nil?
  [m[:path], m[:type], m[:name]]
end