Class: Philiprehberger::Mask::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/mask/configuration.rb

Overview

Thread-safe configuration for custom patterns

Constant Summary collapse

DEFAULT_SENSITIVE_KEYS =
%w[
  password secret token authorization api_key apikey
  access_token refresh_token private_key secret_key
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



14
15
16
17
18
19
20
# File 'lib/philiprehberger/mask/configuration.rb', line 14

def initialize
  @mutex = Mutex.new
  @custom_patterns = []
  @sensitive_keys = DEFAULT_SENSITIVE_KEYS.dup
  @detector_priority = nil
  @locales = {}
end

Instance Attribute Details

#detector_priorityObject (readonly)

Returns the value of attribute detector_priority.



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

def detector_priority
  @detector_priority
end

#localesObject (readonly)

Returns the value of attribute locales.



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

def locales
  @locales
end

#sensitive_keysObject (readonly)

Returns the value of attribute sensitive_keys.



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

def sensitive_keys
  @sensitive_keys
end

Class Method Details

.instanceObject



108
109
110
# File 'lib/philiprehberger/mask/configuration.rb', line 108

def instance
  @instance_mutex.synchronize { @instance ||= new }
end

.reset!Object



112
113
114
# File 'lib/philiprehberger/mask/configuration.rb', line 112

def reset!
  @instance_mutex.synchronize { @instance = new }
end

Instance Method Details

#add_locale(locale, patterns_hash) ⇒ Object

Register locale-specific patterns

Parameters:

  • locale (Symbol)

    locale identifier

  • patterns_hash (Hash<Symbol, Regexp>)

    detector name to regex mapping



66
67
68
69
70
71
72
# File 'lib/philiprehberger/mask/configuration.rb', line 66

def add_locale(locale, patterns_hash)
  @mutex.synchronize do
    @locales[locale.to_sym] = patterns_hash.each_with_object({}) do |(name, regex), hash|
      hash[name.to_sym] = regex
    end
  end
end

#add_pattern(name, pattern, replacement:) ⇒ Object

Add a custom pattern with a static replacement string

Parameters:

  • name (Symbol)

    pattern name

  • pattern (Regexp)

    the regex pattern

  • replacement (String)

    the replacement string



27
28
29
30
31
# File 'lib/philiprehberger/mask/configuration.rb', line 27

def add_pattern(name, pattern, replacement:)
  @mutex.synchronize do
    @custom_patterns << { name: name, pattern: pattern, replacer: ->(_) { replacement } }
  end
end

#add_sensitive_key(key) ⇒ Object

Add a custom sensitive key name

Parameters:

  • key (Symbol, String)

    key name to treat as sensitive



48
49
50
51
52
53
# File 'lib/philiprehberger/mask/configuration.rb', line 48

def add_sensitive_key(key)
  @mutex.synchronize do
    normalized = key.to_s.downcase
    @sensitive_keys << normalized unless @sensitive_keys.include?(normalized)
  end
end

#detect(name, pattern) {|match| ... } ⇒ Object

Register a custom detector with a block-based replacer (DSL)

Parameters:

  • name (Symbol)

    pattern name

  • pattern (Regexp)

    the regex pattern

Yields:

  • (match)

    the matched string

Yield Returns:

  • (String)

    the replacement



39
40
41
42
43
# File 'lib/philiprehberger/mask/configuration.rb', line 39

def detect(name, pattern, &block)
  @mutex.synchronize do
    @custom_patterns << { name: name, pattern: pattern, replacer: block }
  end
end

#patterns(locale: nil) ⇒ Array<Hash>

All patterns (built-in + custom), optionally reordered by priority

Parameters:

  • locale (Symbol, nil) (defaults to: nil)

    optional locale for locale-specific patterns

Returns:

  • (Array<Hash>)


78
79
80
81
82
83
84
# File 'lib/philiprehberger/mask/configuration.rb', line 78

def patterns(locale: nil)
  @mutex.synchronize do
    base = Detector.builtin_patterns + @custom_patterns
    base = apply_locale(base, locale) if locale && @locales.key?(locale)
    @detector_priority ? reorder(base, @detector_priority) : base
  end
end

#set_priority(order) ⇒ Object

Set detector evaluation priority

Parameters:

  • order (Array<Symbol>)

    detector names in desired evaluation order



58
59
60
# File 'lib/philiprehberger/mask/configuration.rb', line 58

def set_priority(order)
  @mutex.synchronize { @detector_priority = order.map(&:to_sym) }
end