Class: RuboCop::Cop::Severity

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubocop/cop/severity.rb

Overview

Severity class is simple value object about severity

Constant Summary collapse

NAMES =
%i[info refactor convention warning error fatal].freeze
CODE_TABLE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{ I: :info, R: :refactor, C: :convention,
W: :warning, E: :error, F: :fatal }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_or_code) ⇒ Severity

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Severity.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
# File 'lib/rubocop/cop/severity.rb', line 30

def initialize(name_or_code)
  name = Severity.name_from_code(name_or_code)
  raise ArgumentError, "Unknown severity: #{name}" unless NAMES.include?(name)

  @name = name.freeze
  freeze
end

Instance Attribute Details

#nameSymbol (readonly)

Returns severity. any of ‘:info`, `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.

Returns:

  • (Symbol)

    severity. any of ‘:info`, `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.



22
23
24
# File 'lib/rubocop/cop/severity.rb', line 22

def name
  @name
end

Class Method Details

.name_from_code(code) ⇒ Object



24
25
26
27
# File 'lib/rubocop/cop/severity.rb', line 24

def self.name_from_code(code)
  name = code.to_sym
  CODE_TABLE[name] || name
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
# File 'lib/rubocop/cop/severity.rb', line 62

def <=>(other)
  level <=> other.level
end

#==(other) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rubocop/cop/severity.rb', line 50

def ==(other)
  @name == if other.is_a?(Symbol)
             other
           else
             other.name
           end
end

#codeObject



42
43
44
# File 'lib/rubocop/cop/severity.rb', line 42

def code
  @name.to_s[0].upcase
end

#hashObject



58
59
60
# File 'lib/rubocop/cop/severity.rb', line 58

def hash
  @name.hash
end

#levelObject



46
47
48
# File 'lib/rubocop/cop/severity.rb', line 46

def level
  NAMES.index(name) + 1
end

#to_sObject



38
39
40
# File 'lib/rubocop/cop/severity.rb', line 38

def to_s
  @name.to_s
end