Class: NeetoMonitorRuby::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/neeto_monitor_ruby/plugin.rb

Constant Summary collapse

@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin

Returns a new instance of Plugin.



34
35
36
37
38
39
# File 'lib/neeto_monitor_ruby/plugin.rb', line 34

def initialize(name)
  @name = name
  @loaded = false
  @requirements = []
  @executions = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/neeto_monitor_ruby/plugin.rb', line 32

def config
  @config
end

#executionsObject (readonly)

Returns the value of attribute executions.



32
33
34
# File 'lib/neeto_monitor_ruby/plugin.rb', line 32

def executions
  @executions
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/neeto_monitor_ruby/plugin.rb', line 32

def name
  @name
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



32
33
34
# File 'lib/neeto_monitor_ruby/plugin.rb', line 32

def requirements
  @requirements
end

Class Method Details

.instancesObject



8
9
10
# File 'lib/neeto_monitor_ruby/plugin.rb', line 8

def instances
  @@instances
end

.load!(config) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/neeto_monitor_ruby/plugin.rb', line 21

def load!(config)
  instances.each_pair do |name, plugin|
    if config.public_send("#{name}_enabled")
      plugin.load!
    else
      NeetoMonitorRuby.logger.debug("skipped plugin #{name} load because it's disabled")
    end
  end
end

.register(name = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
# File 'lib/neeto_monitor_ruby/plugin.rb', line 12

def register(name = nil, &block)
  raise(ArgumentError, "Plugin name is required, but was nil.") unless name

  key = name.to_sym
  raise("Already registered: #{name}") if instances[key]

  instances[key] = new(name).tap { |d| d.instance_eval(&block) }
end

Instance Method Details

#execution(&block) ⇒ Object



45
46
47
# File 'lib/neeto_monitor_ruby/plugin.rb', line 45

def execution(&block)
  @executions << block
end

#fulfilled?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/neeto_monitor_ruby/plugin.rb', line 68

def fulfilled?
  @requirements.all? { |block| instance_eval(&block) }
rescue => exception
  log_plugin_exception(exception)
  false
end

#load!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/neeto_monitor_ruby/plugin.rb', line 49

def load!
  if loaded?
    NeetoMonitorRuby.logger.debug("skip plugin #{name} as already loaded")
    return false
  elsif fulfilled?
    NeetoMonitorRuby.logger.debug("load plugin #{name}")
    @executions.each { |block| instance_eval(&block) }
    @loaded = true
  else
    NeetoMonitorRuby.logger.debug("skip plugin load #{name} requirement not fulfilled")
  end

  @loaded
rescue => exception
  log_plugin_exception(exception)
  @loaded = true
  false
end

#loaded?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/neeto_monitor_ruby/plugin.rb', line 75

def loaded?
  @loaded
end

#requirement(&block) ⇒ Object



41
42
43
# File 'lib/neeto_monitor_ruby/plugin.rb', line 41

def requirement(&block)
  @requirements << block
end