Class: Violation

Inherits:
RuleDefinition show all
Defined in:
lib/cfn-nag/violation.rb

Overview

Rule definition for violations

Constant Summary

Constants inherited from RuleDefinition

RuleDefinition::FAILING_VIOLATION, RuleDefinition::WARNING

Instance Attribute Summary collapse

Attributes inherited from RuleDefinition

#id, #message, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RuleDefinition

#==

Constructor Details

#initialize(id:, type:, message:, logical_resource_ids: [], line_numbers: []) ⇒ Violation

Returns a new instance of Violation.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cfn-nag/violation.rb', line 9

def initialize(id:,
               type:,
               message:,
               logical_resource_ids: [],
               line_numbers: [])
  super id: id,
        type: type,
        message: message

  @logical_resource_ids = logical_resource_ids
  @line_numbers = line_numbers
end

Instance Attribute Details

#line_numbersObject (readonly)

Returns the value of attribute line_numbers.



7
8
9
# File 'lib/cfn-nag/violation.rb', line 7

def line_numbers
  @line_numbers
end

#logical_resource_idsObject (readonly)

Returns the value of attribute logical_resource_ids.



7
8
9
# File 'lib/cfn-nag/violation.rb', line 7

def logical_resource_ids
  @logical_resource_ids
end

Class Method Details

.count_failures(violations) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cfn-nag/violation.rb', line 47

def count_failures(violations)
  violations.inject(0) do |count, violation|
    if violation.type == Violation::FAILING_VIOLATION
      count += if empty?(violation.logical_resource_ids)
                 1
               else
                 violation.logical_resource_ids.size
               end
    end
    count
  end
end

.count_warnings(violations) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cfn-nag/violation.rb', line 34

def count_warnings(violations)
  violations.inject(0) do |count, violation|
    if violation.type == Violation::WARNING
      count += if empty?(violation.logical_resource_ids)
                 1
               else
                 violation.logical_resource_ids.size
               end
    end
    count
  end
end

Instance Method Details

#to_hObject



26
27
28
29
30
31
# File 'lib/cfn-nag/violation.rb', line 26

def to_h
  super.to_h.merge(
    logical_resource_ids: @logical_resource_ids,
    line_numbers: @line_numbers
  )
end

#to_sObject



22
23
24
# File 'lib/cfn-nag/violation.rb', line 22

def to_s
  "#{super} #{@logical_resource_ids}"
end