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 =

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.

[:refactor, :convention, :warning, :error, :fatal]
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.

{ R: :refactor, C: :convention,
W: :warning, E: :error, F: :fatal }

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.



32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/severity.rb', line 32

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

Instance Attribute Details

#nameSymbol (readonly)

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

Returns:

  • (Symbol)

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



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

def name
  @name
end

Class Method Details

.name_from_code(code) ⇒ Object

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.



26
27
28
29
# File 'lib/rubocop/cop/severity.rb', line 26

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

Instance Method Details

#<=>(other) ⇒ Object

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.



71
72
73
# File 'lib/rubocop/cop/severity.rb', line 71

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

#==(other) ⇒ Object

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.



57
58
59
60
61
62
63
# File 'lib/rubocop/cop/severity.rb', line 57

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

#codeObject

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.



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

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

#hashObject

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.



66
67
68
# File 'lib/rubocop/cop/severity.rb', line 66

def hash
  @name.hash
end

#levelObject

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.



52
53
54
# File 'lib/rubocop/cop/severity.rb', line 52

def level
  NAMES.index(name) + 1
end

#to_sObject

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.



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

def to_s
  @name.to_s
end