Class: HammerCLI::Modules

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/modules.rb

Class Method Summary collapse

Class Method Details

.disabled_modulesObject



24
25
26
27
28
29
30
31
32
# File 'lib/hammer_cli/modules.rb', line 24

def self.disabled_modules
  HammerCLI::Settings.dump.inject([]) do |names, (mod_name, mod_config)|
    if is_module_config?(mod_config)
      mod = "hammer_cli_#{mod_name}"
      names << mod unless mod_config[:enable_module]
    end
    names
  end
end

.enabled_modulesObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hammer_cli/modules.rb', line 9

def self.enabled_modules
  modules = []
  HammerCLI::Settings.dump.inject(modules) do |names, (mod_name, mod_config)|
    if is_module_config?(mod_config)
      mod = ["hammer_cli_#{mod_name}"]
      if mod_config[:enable_module]
        names += mod
      else
        names -= mod # disable when enabled in legacy config
      end
    end
    names
  end
end

.find_by_name(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hammer_cli/modules.rb', line 40

def self.find_by_name(name)
  possible_names = [
    name.camelize,
    name.camelize.gsub("Cli", "CLI")
  ]

  possible_names.each do |n|
    return Object.const_get(n) if Object.const_defined?(n)
  end
  return nil
end

.load(name) ⇒ Object



68
69
70
71
72
# File 'lib/hammer_cli/modules.rb', line 68

def self.load(name)
  load! name
rescue Exception
  false
end

.load!(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hammer_cli/modules.rb', line 52

def self.load!(name)
  begin
    require_module(name)
  rescue Exception => e
    logger.error "Error while loading module #{name}."
    puts _("Warning: An error occured while loading module %s.") % name
    # with ModuleLoadingError we assume the error is already logged by the issuer
    logger.error e unless e.is_a?(HammerCLI::ModuleLoadingError)
    raise e
  end

  version = find_by_name(name).version
  logger.info "Extension module #{name} (#{version}) loaded."
  true
end

.load_allObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/hammer_cli/modules.rb', line 78

def self.load_all
  HammerCLI::Modules.names.each do |m|
    Modules.load(m)
  end
  loaded_for_deps = loaded_modules & disabled_modules
  unless loaded_for_deps.empty?
    message = _("Error: Some of the required modules are disabled in configuration: %s.") % loaded_for_deps.join(', ')
    raise HammerCLI::ModuleDisabledButRequired.new(message)
  end
end

.loaded_modulesObject



34
35
36
37
38
# File 'lib/hammer_cli/modules.rb', line 34

def self.loaded_modules
  Object.constants. \
    select{ |c| c.to_s =~ /\AHammerCLI[A-Z]./ && Object.const_get(c).class == Module }. \
    map{ |m| m.to_s.underscore }
end

.namesObject



5
6
7
# File 'lib/hammer_cli/modules.rb', line 5

def self.names
  enabled_modules.sort
end

.require_module(name) ⇒ Object



74
75
76
# File 'lib/hammer_cli/modules.rb', line 74

def self.require_module(name)
  require name
end