Class: GuardrailsRuby::Violation

Inherits:
Object
  • Object
show all
Defined in:
lib/guardrails_ruby/violation.rb

Constant Summary collapse

ACTIONS =
i[block warn redact log].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, detail:, action: :block, severity: nil, matches: nil, sanitized: nil) ⇒ Violation

Returns a new instance of Violation.



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

def initialize(type:, detail:, action: :block, severity: nil, matches: nil, sanitized: nil)
  unless ACTIONS.include?(action)
    raise ArgumentError, "Invalid action #{action.inspect}. Must be one of: #{ACTIONS.join(', ')}"
  end

  @type = type
  @detail = detail
  @action = action
  @severity = severity || default_severity(action)
  @matches = matches
  @sanitized = sanitized
end

Instance Attribute Details

#action ⇒ Object (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/guardrails_ruby/violation.rb', line 7

def action
  @action
end

#detail ⇒ Object (readonly)

Returns the value of attribute detail.



7
8
9
# File 'lib/guardrails_ruby/violation.rb', line 7

def detail
  @detail
end

#matches ⇒ Object (readonly)

Returns the value of attribute matches.



7
8
9
# File 'lib/guardrails_ruby/violation.rb', line 7

def matches
  @matches
end

#sanitized ⇒ Object (readonly)

Returns the value of attribute sanitized.



7
8
9
# File 'lib/guardrails_ruby/violation.rb', line 7

def sanitized
  @sanitized
end

#severity ⇒ Object (readonly)

Returns the value of attribute severity.



7
8
9
# File 'lib/guardrails_ruby/violation.rb', line 7

def severity
  @severity
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/guardrails_ruby/violation.rb', line 7

def type
  @type
end

Instance Method Details

#inspect ⇒ Object



26
27
28
# File 'lib/guardrails_ruby/violation.rb', line 26

def inspect
  "#<#{self.class} type=#{type.inspect}, detail=#{detail.inspect}, action=#{action.inspect}>"
end

#to_s ⇒ Object



22
23
24
# File 'lib/guardrails_ruby/violation.rb', line 22

def to_s
  "#{type}: #{detail} (action=#{action})"
end