Module: Philiprehberger::Mask::Detector

Defined in:
lib/philiprehberger/mask/detector.rb

Overview

Built-in pattern detectors for common PII types

Constant Summary collapse

BUILTIN_PATTERNS =
[
  email_pattern, credit_card_pattern, ssn_pattern, phone_pattern,
  ip_pattern, jwt_pattern, passport_pattern, iban_pattern,
  drivers_license_pattern, mrn_pattern
].map(&:freeze).freeze

Class Method Summary collapse

Class Method Details

.builtin_patternsArray<Hash>

The memoized, frozen list of built-in detectors. Consumers must treat it as read-only — configuration (priority/locale/custom merges) copies this list rather than mutating it.

Returns:

  • (Array<Hash>)

    frozen detector definitions



12
13
14
# File 'lib/philiprehberger/mask/detector.rb', line 12

def self.builtin_patterns
  BUILTIN_PATTERNS
end

.luhn_valid?(number) ⇒ Boolean

Validate a candidate card number with the Luhn checksum

Used to gate the credit_card detector so that non-card digit runs (order IDs, tracking numbers) are not masked as cards.

Parameters:

  • number (String)

    the matched candidate (may include separators)

Returns:

  • (Boolean)

    true when the digits pass the Luhn check



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/philiprehberger/mask/detector.rb', line 114

def self.luhn_valid?(number)
  digits = number.to_s.gsub(/\D/, '')
  return false if digits.length < 13

  sum = 0
  digits.reverse.each_char.with_index do |char, index|
    value = char.to_i
    if index.odd?
      value *= 2
      value -= 9 if value > 9
    end
    sum += value
  end
  (sum % 10).zero?
end