Class: Chef::Deprecated::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/deprecated.rb

Constant Summary collapse

BASE_URL =
"https://docs.chef.io/deprecations_".freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil, location = nil) ⇒ Base

Returns a new instance of Base.



37
38
39
40
# File 'lib/chef/deprecated.rb', line 37

def initialize(msg = nil, location = nil)
  @message = msg
  @location = location
end

Class Attribute Details

.deprecation_idObject (readonly)

Returns the value of attribute deprecation_id.



92
93
94
# File 'lib/chef/deprecated.rb', line 92

def deprecation_id
  @deprecation_id
end

.doc_pageObject (readonly)

Returns the value of attribute doc_page.



92
93
94
# File 'lib/chef/deprecated.rb', line 92

def doc_page
  @doc_page
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



35
36
37
# File 'lib/chef/deprecated.rb', line 35

def location
  @location
end

#messageObject (readonly)

Returns the value of attribute message.



35
36
37
# File 'lib/chef/deprecated.rb', line 35

def message
  @message
end

Class Method Details

.deprecation_keyString

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

Returns:



97
98
99
# File 'lib/chef/deprecated.rb', line 97

def deprecation_key
  Chef::Mixin::ConvertToClassName.convert_to_snake_case(name, "Chef::Deprecated")
end

.target(id, page = nil) ⇒ void

This method returns an undefined value.

Set the ID and documentation page path for this deprecation.

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

Examples:

class MyDeprecation < Base
  target 123, "my_deprecation.html"
end

Parameters:

  • id (Integer)

    Deprecation ID number. This must be unique among all deprecations.

  • page (String, nil) (defaults to: nil)

    Optional documentation page path. If not specified, the deprecation key is used.



114
115
116
117
# File 'lib/chef/deprecated.rb', line 114

def target(id, page = nil)
  @deprecation_id = id
  @doc_page = page || "#{deprecation_key}.html"
end

Instance Method Details



42
43
44
# File 'lib/chef/deprecated.rb', line 42

def link
  "Please see #{url} for further details and information on how to correct this problem."
end

#silenced?Boolean

Check if this deprecation has been silenced.

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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
    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

  false
end

#to_sString

Render the user-visible message for this deprecation.

Returns:



56
57
58
# 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

#urlString

Render the URL for the deprecation documentation page.

Returns:



49
50
51
# File 'lib/chef/deprecated.rb', line 49

def url
  "#{BASE_URL}#{self.class.doc_page}"
end