Class: Tracebook::Redaction::Pattern

Inherits:
Data
  • Object
show all
Defined in:
lib/tracebook/redaction/pattern.rb

Overview

A single redaction pattern with regex, replacement, and optional validator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, regex:, replacement:, validator: nil) ⇒ Pattern

Returns a new instance of Pattern.



7
8
9
# File 'lib/tracebook/redaction/pattern.rb', line 7

def initialize(name:, regex:, replacement:, validator: nil)
  super
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



6
7
8
# File 'lib/tracebook/redaction/pattern.rb', line 6

def name
  @name
end

#regexObject (readonly)

Returns the value of attribute regex

Returns:

  • (Object)

    the current value of regex



6
7
8
# File 'lib/tracebook/redaction/pattern.rb', line 6

def regex
  @regex
end

#replacementObject (readonly)

Returns the value of attribute replacement

Returns:

  • (Object)

    the current value of replacement



6
7
8
# File 'lib/tracebook/redaction/pattern.rb', line 6

def replacement
  @replacement
end

#validatorObject (readonly)

Returns the value of attribute validator

Returns:

  • (Object)

    the current value of validator



6
7
8
# File 'lib/tracebook/redaction/pattern.rb', line 6

def validator
  @validator
end

Instance Method Details

#redact(text) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tracebook/redaction/pattern.rb', line 11

def redact(text)
  return text unless text.is_a?(String)

  text.gsub(regex) do |match|
    if validator && !validator.call(match)
      match
    else
      replacement
    end
  end
end