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
Instance Attribute Summary collapse
-
#detector_priority ⇒ Object
readonly
Returns the value of attribute detector_priority.
-
#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.
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_priority ⇒ Object (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 |
#locales ⇒ Object (readonly)
Returns the value of attribute locales.
12 13 14 |
# File 'lib/philiprehberger/mask/configuration.rb', line 12 def locales @locales end |
#sensitive_keys ⇒ Object (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
.instance ⇒ Object
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
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
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
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)
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
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
58 59 60 |
# File 'lib/philiprehberger/mask/configuration.rb', line 58 def set_priority(order) @mutex.synchronize { @detector_priority = order.map(&:to_sym) } end |