class Chef::Deprecated::Base

Constants

BASE_URL

Attributes

deprecation_id[R]
doc_page[R]
location[R]
message[R]

Public Class Methods

deprecation_key() click to toggle source

Return the deprecation key as would be used with {Chef::Deprecated.create}.

@return [String]

# File lib/chef/deprecated.rb, line 99
def deprecation_key
  Chef::Mixin::ConvertToClassName.convert_to_snake_case(name, "Chef::Deprecated")
end
new(msg = nil, location = nil) click to toggle source
# File lib/chef/deprecated.rb, line 37
def initialize(msg = nil, location = nil)
  @message = msg
  @location = location
end
target(id, page = nil) click to toggle source

Set the ID and documentation page path for this deprecation.

Used in subclasses to set the data for each type of deprecation.

@example

class MyDeprecation < Base
  target 123, "my_deprecation"
end

@param id [Integer] Deprecation ID number. This must be unique among

all deprecations.

@param page [String, nil] Optional documentation page path. If not

specified, the deprecation key is used.

@return [void]

# File lib/chef/deprecated.rb, line 116
def target(id, page = nil)
  @deprecation_id = id
  @doc_page = page || deprecation_key.to_s
end

Public Instance Methods

silenced?() click to toggle source

Check if this deprecation has been silenced.

@return [Boolean]

# File lib/chef/deprecated.rb, line 63
def silenced?
  # Check if all warnings have been silenced.
  return true if Chef::Config[:silence_deprecation_warnings] == true
  # Check if this warning has been silenced by the config.
  return true if Chef::Config[:silence_deprecation_warnings].any? do |silence_spec|
    if silence_spec.is_a? Integer
      # Integers can end up matching the line number in the `location` string
      silence_spec = "CHEF-#{silence_spec}"
    else
      # Just in case someone uses a symbol in the config by mistake.
      silence_spec = silence_spec.to_s
    end
    # Check for a silence by deprecation name, or by location.
    self.class.deprecation_key == silence_spec || self.class.deprecation_id.to_s == silence_spec || "chef-#{self.class.deprecation_id}" == silence_spec.downcase || location.include?(silence_spec)
  end
  # check if this warning has been silenced by inline comment.
  return true if location =~ /^(.*?):(\d+):in/ && begin
    # Don't buffer the whole file in memory, so read it one line at a time.
    line_no = $2.to_i
    if File.exist?($1) # some stacktraces come from `eval` and not a file
      location_file = ::File.open($1)
      (line_no - 1).times { location_file.readline } # Read all the lines we don't care about.
      relevant_line = location_file.readline
      relevant_line.match?(/#.*chef:silence_deprecation($|[^:]|:#{self.class.deprecation_key})/)
    end
  end

  false
end
to_s() click to toggle source

Render the user-visible message for this deprecation.

@return [String]

# File lib/chef/deprecated.rb, line 56
def to_s
  "Deprecation CHEF-#{self.class.deprecation_id} from #{location}\n\n  #{message}\n\n#{link}"
end
url() click to toggle source

Render the URL for the deprecation documentation page.

@return [String]

# File lib/chef/deprecated.rb, line 49
def url
  "#{BASE_URL}#{self.class.doc_page}/"
end