Class: Sensu::Extensions::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/extensions/loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader



18
19
20
21
22
23
24
25
26
# File 'lib/sensu/extensions/loader.rb', line 18

def initialize
  @warnings = []
  @loaded_files = []
  @extensions = {}
  Extension::CATEGORIES.each do |category|
    @extensions[category] = {}
  end
  self.class.create_category_methods
end

Instance Attribute Details

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files



16
17
18
# File 'lib/sensu/extensions/loader.rb', line 16

def loaded_files
  @loaded_files
end

#warningsObject (readonly)

Returns the value of attribute warnings



12
13
14
# File 'lib/sensu/extensions/loader.rb', line 12

def warnings
  @warnings
end

Class Method Details

.create_category_methodsObject

Create extension category accessors and methods to test the existence of extensions. Called in initialize().



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sensu/extensions/loader.rb', line 30

def self.create_category_methods
  Extension::CATEGORIES.each do |category|
    define_method(category) do
      extension_category(category)
    end
    method_name = category.to_s.chop + "_exists?"
    define_method(method_name.to_sym) do |name|
      extension_exists?(category, name)
    end
  end
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the extension object corresponding to a key, acting like a Hash object.

Parameters:

  • key (Symbol)

Returns:

  • (Object)

    value for key.



47
48
49
# File 'lib/sensu/extensions/loader.rb', line 47

def [](key)
  @extensions[key]
end

#allArray<object>

Retrieve all extension instances.

Returns:

  • (Array<object>)

    extensions.



54
55
56
57
58
59
60
# File 'lib/sensu/extensions/loader.rb', line 54

def all
  @extensions.map { |category, extensions|
    extensions.map do |name, extension|
      extension
    end
  }.flatten
end

#load_directory(directory) ⇒ Object

Load extensions from files in a directory. Files may be in nested directories.

Parameters:

  • directory (String)

    path.



80
81
82
83
84
85
86
# File 'lib/sensu/extensions/loader.rb', line 80

def load_directory(directory)
  warning("loading extension files from directory", :directory => directory)
  path = directory.gsub(/\\(?=\S)/, "/")
  Dir.glob(File.join(path, "**/*.rb")).each do |file|
    load_file(file)
  end
end

#load_file(file) ⇒ Object

Load an extension from a file.

Parameters:

  • file (String)

    path.



65
66
67
68
69
70
71
72
73
74
# File 'lib/sensu/extensions/loader.rb', line 65

def load_file(file)
  warning("loading extension file", :file => file)
  begin
    require File.expand_path(file)
    @loaded_files << file
  rescue ScriptError, StandardError => error
    warning("failed to require extension", :file => file, :error => error)
    warning("ignoring extension", :file => file)
  end
end

#load_instances(service = nil) ⇒ Object

Load instances of the loaded extensions.

Parameters:

  • sensu (String)

    service to load extensions for.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sensu/extensions/loader.rb', line 91

def load_instances(service=nil)
  service ||= sensu_service_name
  categories_to_load(service).each do |category|
    extension_type = category.to_s.chop
    Extension.const_get(extension_type.capitalize).descendants.each do |klass|
      extension = klass.new
      @extensions[category][extension.name] = extension
      warning("loaded extension", {
        :type => extension_type,
        :name => extension.name,
        :description => extension.description
      })
    end
  end
end