Class: CollinsNotify::Configuration

Inherits:
Object
  • Object
show all
Includes:
ConfigurationMixin
Defined in:
lib/collins_notify/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigurationMixin

#debug?, #error?, #fatal?, #info?, #method_missing, #sevname, #test?, #trace?, #warn?

Constructor Details

#initialize(logger, opts = {}) ⇒ Configuration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
# File 'lib/collins_notify/configuration.rb', line 9

def initialize logger, opts = {}
  @adapters = {}
  configurable.each do |key, value|
    instance_variable_set "@#{key.to_s}".to_sym, value
  end
  @logger = logger
  merge opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CollinsNotify::ConfigurationMixin

Instance Attribute Details

#adaptersObject

Returns the value of attribute adapters.



7
8
9
# File 'lib/collins_notify/configuration.rb', line 7

def adapters
  @adapters
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/collins_notify/configuration.rb', line 6

def logger
  @logger
end

Instance Method Details

#configObject

Override ConfigurationMixin#config



19
20
21
# File 'lib/collins_notify/configuration.rb', line 19

def config
  self
end

#increase_verbosityObject

Shorthand for severity assignment but silently fails if too verbose



39
40
41
42
43
44
45
46
# File 'lib/collins_notify/configuration.rb', line 39

def increase_verbosity
  if severity > -1 then
    self.severity -= 1
    if trace? then
      $DEBUG = true
    end
  end
end

#message_body?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/collins_notify/configuration.rb', line 48

def message_body?
  template? || stdin?
end

#original_to_hashObject



71
# File 'lib/collins_notify/configuration.rb', line 71

alias_method :original_to_hash, :to_hash

#resolved_templateObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/collins_notify/configuration.rb', line 52

def resolved_template
  return nil unless template
  if File.exists?(File.expand_path(template)) then
    return File.expand_path(template)
  elsif template_dir && File.exists?(File.expand_path(File.join(template_dir, template))) then
    return File.expand_path(File.join(template_dir, template))
  elsif File.exists?(File.expand_path(File.join(default_template_dir, template))) then
    return File.expand_path(File.join(default_template_dir, template))
  end
end

#stdin?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/collins_notify/configuration.rb', line 63

def stdin?
  !$stdin.tty?
end

#template?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/collins_notify/configuration.rb', line 67

def template?
  !resolved_template.nil?
end

#to_hashObject



72
73
74
# File 'lib/collins_notify/configuration.rb', line 72

def to_hash
  original_to_hash.merge(:severity_name => sevname, :adapters => adapters)
end

#valid?Boolean

Override ConfigurationMixin#valid?

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/collins_notify/configuration.rb', line 24

def valid?
  adapters = CollinsNotify::Notifier.adapters.keys
  if type.nil? || !adapters.include?(type) then
    msg = "Invalid notifier type specified. Valid notifiers: #{adapters.sort.join(', ')}"
    logger.error msg
    return false
  end
  unless message_body? then
    logger.error "No message body found in specified template or stdin"
    return false
  end
  true
end