Class: Inventory::Server::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory/server/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Loader

Returns a new instance of Loader.



9
10
11
12
13
14
15
# File 'lib/inventory/server/loader.rb', line 9

def initialize(config)
  @plugins_path = [PLUGINS_DIR]
  @plugins_path.concat config[:plugins_path] if config[:plugins_path]
  @plugins_path.each { |path|
    raise InventoryError.new "plugins_path #{path} not found" unless File.directory? path
  }
end

Instance Method Details

#classify(str) ⇒ Object

transform a snake case string into a upper camel case string



47
48
49
# File 'lib/inventory/server/loader.rb', line 47

def classify(str)
  str.split('_').collect!{ |w| w.capitalize }.join
end

#kernel_load(file) ⇒ Object

Usefull for tests



41
42
43
44
# File 'lib/inventory/server/loader.rb', line 41

def kernel_load(file)
  InventoryLogger.logger.info "Load #{file}"
  require file
end

#load_file(file) ⇒ Object

Load a ruby file



36
37
38
# File 'lib/inventory/server/loader.rb', line 36

def load_file(file)
  kernel_load(file)
end

#load_plugins(*plugins) ⇒ Object

search for plugins in the plugins_path and return a hash of plugin_filename:plugin_klass



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inventory/server/loader.rb', line 18

def load_plugins(*plugins)
  plugin_klasses = []
  plugins.each {|plugin|
    p = nil
    @plugins_path.each {|plugin_path|
      filepath = File.join plugin_path, "#{plugin}.rb"
      next unless File.file? filepath
      load_file filepath
      klass_name = classify(plugin)
      p = Object.const_get("Inventory").const_get("Server").const_get(klass_name)
    }
    raise InventoryError.new "Plugin #{plugin} not found" if !p
    plugin_klasses << p
  }
  return plugin_klasses
end