module Autoloaded::Warning

Prints warning messages to stderr.

@since 1.3

@api private

Attributes

io[W]

Sets the warning stream.

@param [IO] value a new value for io

Public Class Methods

changing_autoload(keywords) click to toggle source

Prints a warning message to #io concerning an existing autoloaded constant for which the autoloaded source file is being changed.

@param [Hash] keywords the parameters of the warning message @option keywords [Symbol] :constant_name the name of the constant @option keywords [String] :old_source_filename the name of the existing

autoloaded source file

@option keywords [String] :new_source_filename the name of the new

autoloaded source file

@option keywords [String] :host_source_location the file and line number of

the source establishing
autoloading

@return [Module] Warning

@raise [ArgumentError] one or more keywords are missing

# File lib/autoloaded/warning.rb, line 38
def changing_autoload(keywords)
  constant_name        = fetch(keywords, :constant_name)
  old_source_filename  = fetch(keywords, :old_source_filename)
  new_source_filename  = fetch(keywords, :new_source_filename)
  host_source_location = fetch(keywords, :host_source_location)

  message = "Existing autoload of \e[4m#{constant_name}\e[0m from "       +
            "#{old_source_filename.inspect} is being overridden to "      +
            "autoload from #{new_source_filename.inspect} -- avoid this " +
            "warning by using an \e[4monly\e[0m or an \e[4mexcept\e[0m "  +
            "specification in the block at #{host_source_location}"
  warn message
end
enable(enabling) { || ... } click to toggle source

Enables or disables warning messages depending on the specified enabling argument.

@param [Object] enabling disables warnings if nil or false

@yield if a block is given, the value of #enabled? is reset after the

block is called

@return if a block is given, the result of calling the block @return [Module] if a block is not given, Warning

@see .enabled?

# File lib/autoloaded/warning.rb, line 64
def enable(enabling)
  previous_value = instance_variable_defined?(:@disabled) && @disabled
  @disabled = not!(enabling)
  if block_given?
    begin
      return yield
    ensure
      @disabled = previous_value
    end
  end

  self
end
enabled?() click to toggle source

Indicates whether warning messages are enabled or disabled.

@return [true] if warning messages are enabled @return [false] if warning messages are enabled

@see .enable

# File lib/autoloaded/warning.rb, line 84
def enabled?
  not! @disabled
end
existing_constant(keywords) click to toggle source

Prints a warning message to #io concerning a defined constant for which autoloading is being established.

@param [Hash] keywords the parameters of the warning message @option keywords [Symbol] :constant_name the name of the constant @option keywords [String] :source_filename the name of the autoloaded

source file

@option keywords [String] :host_source_location the file and line number of

the source establishing
autoloading

@return [Module] Warning

@raise [ArgumentError] one or more keywords are missing

# File lib/autoloaded/warning.rb, line 102
def existing_constant(keywords)
  constant_name        = fetch(keywords, :constant_name)
  source_filename      = fetch(keywords, :source_filename)
  host_source_location = fetch(keywords, :host_source_location)

  message = "Existing definition of \e[4m#{constant_name}\e[0m obviates " +
            "autoloading from #{source_filename.inspect} -- avoid this "  +
            "warning by using an \e[4monly\e[0m or an \e[4mexcept\e[0m "  +
            "specification in the block at #{host_source_location}"
  warn message
end
io() click to toggle source

The warning stream. Defaults to +$stderr+.

@return [IO] the warning stream

# File lib/autoloaded/warning.rb, line 18
def io
  @io || $stderr
end

Private Class Methods

fetch(keywords, keyword) click to toggle source
# File lib/autoloaded/warning.rb, line 116
def fetch(keywords, keyword)
  keywords.fetch keyword do
    raise ::ArgumentError, "missing keyword: #{keyword}"
  end
end
not!(value) click to toggle source
# File lib/autoloaded/warning.rb, line 122
def not!(value)
  value.nil? || (value == false)
end
warn(message) click to toggle source
# File lib/autoloaded/warning.rb, line 126
def warn(message)
  warning = "\e[33m*** \e[7m WARNING \e[0m #{message}"
  io.puts(warning) if enabled?

  self
end