Class: Honeybadger::Plugin

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

Defined Under Namespace

Classes: Execution

Constant Summary collapse

CALLER_FILE =
Regexp.new('\A(?:\w:)?([^:]+)(?=(:\d+))').freeze
@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin


56
57
58
59
60
61
# File 'lib/honeybadger/plugin.rb', line 56

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

Instance Attribute Details

#executionsObject (readonly)

Returns the value of attribute executions


108
109
110
# File 'lib/honeybadger/plugin.rb', line 108

def executions
  @executions
end

#nameObject (readonly)

Returns the value of attribute name


108
109
110
# File 'lib/honeybadger/plugin.rb', line 108

def name
  @name
end

#requirementsObject (readonly)

Returns the value of attribute requirements


108
109
110
# File 'lib/honeybadger/plugin.rb', line 108

def requirements
  @requirements
end

Class Method Details

.instancesObject


10
11
12
# File 'lib/honeybadger/plugin.rb', line 10

def instances
  @@instances
end

.load!(config) ⇒ Object


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

def load!(config)
  instances.each_pair do |name, plugin|
    if config.load_plugin?(name)
      plugin.load!(config)
    else
      config.logger.debug(sprintf('skip plugin name=%s reason=disabled', name))
    end
  end
end

.name_from_caller(caller) ⇒ Object


31
32
33
34
35
# File 'lib/honeybadger/plugin.rb', line 31

def name_from_caller(caller)
  caller && caller[0].match(CALLER_FILE) or
    fail("Unable to determine name from caller: #{caller.inspect}")
  File.basename($1)[/[^\.]+/]
end

.register(name = nil) ⇒ Object


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

def register(name = nil)
  name ||= name_from_caller(caller) or
    raise(ArgumentError, 'Plugin name is required, but was nil.')
  instances[key = name.to_sym] and fail("Already registered: #{name}")
  instances[key] = new(name).tap { |d| d.instance_eval(&Proc.new) }
end

Instance Method Details

#executionObject


67
68
69
# File 'lib/honeybadger/plugin.rb', line 67

def execution
  @executions << Proc.new
end

#load!(config) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/honeybadger/plugin.rb', line 78

def load!(config)
  if @loaded
    config.logger.debug(sprintf('skip plugin name=%s reason=loaded', name))
    return false
  elsif ok?(config)
    config.logger.debug(sprintf('load plugin name=%s', name))
    @executions.each {|e| Execution.new(config, &e).call }
    @loaded = true
  else
    config.logger.debug(sprintf('skip plugin name=%s reason=requirement', name))
  end

  @loaded
rescue => e
  config.logger.error(sprintf("plugin error name=%s class=%s message=%s\n\t%s", name, e.class, e.message.dump, Array(e.backtrace).join("\n\t")))
  @loaded = true
  false
end

#loaded?Boolean


104
105
106
# File 'lib/honeybadger/plugin.rb', line 104

def loaded?
  @loaded
end

#ok?(config) ⇒ Boolean


71
72
73
74
75
76
# File 'lib/honeybadger/plugin.rb', line 71

def ok?(config)
  @requirements.all? {|r| Execution.new(config, &r).call }
rescue => e
  config.logger.error(sprintf("plugin error name=%s class=%s message=%s\n\t%s", name, e.class, e.message.dump, Array(e.backtrace).join("\n\t")))
  false
end

#requirementObject


63
64
65
# File 'lib/honeybadger/plugin.rb', line 63

def requirement
  @requirements << Proc.new
end

#reset!Object

Private: Used for testing only; don't normally call this. :)

Returns nothing


100
101
102
# File 'lib/honeybadger/plugin.rb', line 100

def reset!
  @loaded = false
end