Module: Warning::Processor

Included in:
Warning
Defined in:
lib/warning.rb

Instance Method Summary collapse

Instance Method Details

#clearObject

Clear all current ignored warnings and warning processors.



6
7
8
9
10
11
# File 'lib/warning.rb', line 6

def clear
  synchronize do
    @ignore.clear
    @process.clear
  end
end

#ignore(regexp, path = '') ⇒ Object

Ignore any warning messages matching the given regexp, if they start with the given path. Examples:

# Ignore all uninitialized instance variable warnings
Warning.ignore(/instance variable @\w+ not initialized/)

# Ignore all uninitialized instance variable warnings in current file
Warning.ignore(/instance variable @\w+ not initialized/, __FILE__)


21
22
23
24
25
26
# File 'lib/warning.rb', line 21

def ignore(regexp, path='')
  synchronize do 
    @ignore << [path, regexp]
  end
  nil
end

#process(path = '', &block) ⇒ Object

Handle all warnings starting with the given path, instead of the default behavior of printing them to $stderr. Examples:

# Write warning to LOGGER at level warning
Warning.process do |warning|
  LOGGER.warning(warning)
end

# Write warnings in the current file to LOGGER at level error level
Warning.process(__FILE__) do |warning|
  LOGGER.error(warning)
end


40
41
42
43
44
45
46
47
# File 'lib/warning.rb', line 40

def process(path='', &block)
  synchronize do
    @process << [path, block]
    @process.sort_by!(&:first)
    @process.reverse!
  end
  nil
end

#warn(str) ⇒ Object

Handle ignored warnings and warning processors. If the warning is not ignored and there is no warning processor setup for the warning string, then use the default behavior of writing to $stderr.



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

def warn(str)
  synchronize{@ignore.dup}.each do |path, regexp|
    if str.start_with?(path) && str =~ regexp
      return
    end
  end

  synchronize{@process.dup}.each do |path, block|
    if str.start_with?(path)
      block.call(str)
      return
    end
  end

  super
end