Class: Jerakia::Lookup::PluginFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/lookup/pluginfactory.rb

Instance Method Summary collapse

Constructor Details

#initializePluginFactory

Returns a new instance of PluginFactory


2
3
4
5
# File 'lib/jerakia/lookup/pluginfactory.rb', line 2

def initialize
  Jerakia.log.debug('Loaded plugin handler')
  @plugin_config = Jerakia.config[:plugins] || {}
end

Instance Method Details

#create_plugin_method(name, &block) ⇒ Object


7
8
9
# File 'lib/jerakia/lookup/pluginfactory.rb', line 7

def create_plugin_method(name, &block)
  self.class.send(:define_method, name, &block)
end

#register(name, plugin) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jerakia/lookup/pluginfactory.rb', line 11

def register(name, plugin)
  begin
    require "jerakia/lookup/plugin/#{name}"
  rescue LoadError => e
    raise Jerakia::Error, "Cannot load plugin #{name}, #{e.message}"
  end

  plugin.activate(name)
  create_plugin_method(name) do
    plugin
  end
  if plugin.respond_to?('autorun')
    Jerakia.log.debug("Found autorun method for plugin #{name}, executing")

    if plugin.method('autorun').arity == 1
      plugin.autorun (@plugin_config[name.to_s] || {})
    else
      plugin.autorun
    end
  end
end