Class: Philiprehberger::Mask::Configuration
- Inherits:
-
Object
- Object
- Philiprehberger::Mask::Configuration
- 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
- DEFAULT_FILTERED_PLACEHOLDER =
'[FILTERED]'
Instance Attribute Summary collapse
-
#detector_priority ⇒ Object
readonly
Returns the value of attribute detector_priority.
-
#filtered_placeholder ⇒ Object
Returns the value of attribute filtered_placeholder.
-
#locales ⇒ Object
readonly
Returns the value of attribute locales.
-
#sensitive_keys ⇒ Object
readonly
Returns the value of attribute sensitive_keys.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_locale(locale, patterns_hash) ⇒ Object
Register locale-specific patterns.
-
#add_pattern(name, pattern, replacement:) ⇒ Object
Add a custom pattern with a static replacement string.
-
#add_sensitive_key(key) ⇒ Object
Add a custom sensitive key name.
-
#detect(name, pattern) {|match| ... } ⇒ Object
Register a custom detector with a block-based replacer (DSL).
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#patterns(locale: nil) ⇒ Array<Hash>
All patterns (built-in + custom), optionally reordered by priority.
-
#set_priority(order) ⇒ Object
Set detector evaluation priority.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
16 17 18 19 20 21 22 23 |
# File 'lib/philiprehberger/mask/configuration.rb', line 16 def initialize @mutex = Mutex.new @custom_patterns = [] @sensitive_keys = DEFAULT_SENSITIVE_KEYS.dup @detector_priority = nil @locales = {} @filtered_placeholder = DEFAULT_FILTERED_PLACEHOLDER end |
Instance Attribute Details
#detector_priority ⇒ Object (readonly)
Returns the value of attribute detector_priority.
14 15 16 |
# File 'lib/philiprehberger/mask/configuration.rb', line 14 def detector_priority @detector_priority end |
#filtered_placeholder ⇒ Object
Returns the value of attribute filtered_placeholder.
14 15 16 |
# File 'lib/philiprehberger/mask/configuration.rb', line 14 def filtered_placeholder @filtered_placeholder end |
#locales ⇒ Object (readonly)
Returns the value of attribute locales.
14 15 16 |
# File 'lib/philiprehberger/mask/configuration.rb', line 14 def locales @locales end |
#sensitive_keys ⇒ Object (readonly)
Returns the value of attribute sensitive_keys.
14 15 16 |
# File 'lib/philiprehberger/mask/configuration.rb', line 14 def sensitive_keys @sensitive_keys end |
Class Method Details
.instance ⇒ Object
118 119 120 |
# File 'lib/philiprehberger/mask/configuration.rb', line 118 def instance @instance_mutex.synchronize { @instance ||= new } end |
.reset! ⇒ Object
122 123 124 |
# File 'lib/philiprehberger/mask/configuration.rb', line 122 def reset! @instance_mutex.synchronize { @instance = new } end |
Instance Method Details
#add_locale(locale, patterns_hash) ⇒ Object
Register locale-specific patterns
76 77 78 79 80 81 82 |
# File 'lib/philiprehberger/mask/configuration.rb', line 76 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
37 38 39 40 41 |
# File 'lib/philiprehberger/mask/configuration.rb', line 37 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
58 59 60 61 62 63 |
# File 'lib/philiprehberger/mask/configuration.rb', line 58 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)
49 50 51 52 53 |
# File 'lib/philiprehberger/mask/configuration.rb', line 49 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
88 89 90 91 92 93 94 |
# File 'lib/philiprehberger/mask/configuration.rb', line 88 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
68 69 70 |
# File 'lib/philiprehberger/mask/configuration.rb', line 68 def set_priority(order) @mutex.synchronize { @detector_priority = order.map(&:to_sym) } end |