Class: HamlLint::Severity

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Comparable
Defined in:
lib/haml_lint/severity.rb

Overview

Models the severity of a lint

Constant Summary collapse

SEVERITY_ERROR =
:error
SEVERITY_WARNING =
:warning
COLORS =
{ error: :red, warning: :yellow }.freeze
MARKS =
{ error: 'E', warning: 'W' }.freeze
NAMES =
[SEVERITY_WARNING, SEVERITY_ERROR].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Severity

Creates a new severity for a lint

Examples:

HamlLint::Severity.new(:warning)

Parameters:

  • name (Symbol)

    the name of the severity level



24
25
26
27
28
29
# File 'lib/haml_lint/severity.rb', line 24

def initialize(name)
  name = name.name if name.is_a?(Severity)
  name ||= :warning
  fail Exceptions::UnknownSeverity, "Unknown severity: #{name}" unless NAMES.include?(name)
  super
end

Instance Method Details

#<=>(other) ⇒ Integer

Compares the severity to another severity or a symbol

Returns:

  • (Integer)


92
93
94
95
# File 'lib/haml_lint/severity.rb', line 92

def <=>(other)
  other = Severity.new(other) unless other.respond_to?(:level)
  level <=> other.level
end

#colorSymbol

The color of the mark in reporters.

Returns:

  • (Symbol)


34
35
36
# File 'lib/haml_lint/severity.rb', line 34

def color
  COLORS[__getobj__]
end

#error?Boolean

Checks whether the severity is an error

Examples:

HamlLint::Severity.new(:error).error? #=> true

Returns:

  • (Boolean)


45
46
47
# File 'lib/haml_lint/severity.rb', line 45

def error?
  __getobj__ == :error
end

#levelInteger

The level of severity for the lint

Returns:

  • (Integer)


53
54
55
# File 'lib/haml_lint/severity.rb', line 53

def level
  NAMES.index(__getobj__) + 1
end

#markObject

The symbol to use in a Reporter::ProgressReporter.



60
61
62
# File 'lib/haml_lint/severity.rb', line 60

def mark
  MARKS[__getobj__]
end

#mark_with_colorObject

The colorized symbol to use in a reporter.



67
68
69
# File 'lib/haml_lint/severity.rb', line 67

def mark_with_color
  Rainbow.global.wrap(mark).public_send(color)
end

#nameObject

The name of the severity.



74
75
76
# File 'lib/haml_lint/severity.rb', line 74

def name
  __getobj__
end

#warning?Boolean

Checks whether the severity is a warning

Examples:

HamlLint::Severity.new(:warning).warning? #=> true

Returns:

  • (Boolean)


85
86
87
# File 'lib/haml_lint/severity.rb', line 85

def warning?
  __getobj__ == :warning
end