Module: Toys::Warnings
- Defined in:
- lib/toys/warnings.rb
Overview
A warning reporter that supports throttling of repeated messages and environment variable based silencing. Used in particular to display deprecation warnings.
Constant Summary collapse
- DEFAULT_MAX_COUNT =
Returns The default value for max_count.
5- SUPPRESS_WARNINGS_ENV =
Returns The name of an environment variable that, when set to a non-empty value, suppresses warnings emitted by this module.
"TOYS_SUPPRESS_WARNINGS"
Class Attribute Summary collapse
-
.max_count ⇒ Integer?
The maximum number of times a warning with the same identity will be displayed before further instances are suppressed, or nil to keep displaying each warning regardless of how many times it has repeated.
Class Method Summary collapse
-
.warn(identifier, message, uplevel: nil, max_count: :default) ⇒ Integer
Displays a warning message.
Class Attribute Details
.max_count ⇒ Integer?
The maximum number of times a warning with the same identity will be displayed before further instances are suppressed, or nil to keep displaying each warning regardless of how many times it has repeated. Defaults to DEFAULT_MAX_COUNT. Can be set using max_count=.
40 41 42 |
# File 'lib/toys/warnings.rb', line 40 def max_count @max_count end |
Class Method Details
.warn(identifier, message, uplevel: nil, max_count: :default) ⇒ Integer
Displays a warning message.
You must pass an identifier object and a message string. The identifier should identify "which" warning is being displayed, and is used to count repeated warnings so they can be throttled after a certain number of instances.
Uses the Ruby Kernel#warn method, so it is affected by $VERBOSE and
the -W0 flag. Does not currently support the category: argument to
Kernel#warn because it is not supported in Ruby 2.7, and in newer
Rubies, common values such as category: :deprecated default to silent,
whereas this method should not be silent unless explicitly requested.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/toys/warnings.rb', line 89 def warn(identifier, , uplevel: nil, max_count: :default) if max_count == :default max_count = @max_count else validate_max_count(max_count) end uplevel = validate_and_bump_uplevel(uplevel) count = next_count(identifier) = (, count, max_count) if && ::ENV[SUPPRESS_WARNINGS_ENV].to_s.empty? ::Kernel.warn(, uplevel: uplevel) end count end |