Class: RightSupport::Notifier::Blacklister::Wildcard
- Inherits:
-
RegularExpression
- Object
- Base
- RegularExpression
- RightSupport::Notifier::Blacklister::Wildcard
- Defined in:
- lib/right_support/notifiers/blacklisters/wildcard.rb
Overview
implements a blacklister that matches key paths by wildcard pattern interpreted as a key/value hierarchy represented as slash (/) delimited parts of the pattern string. supports the usual [*,?] wildcard characters as well as the more advanced multiple-indirection “**/” pattern.
example:
patterns = ['a/*/c']
data = {a: {b: c: 42}}
result == {a: {b: c: 'HIDDEN'}}
Instance Attribute Summary
Attributes inherited from RegularExpression
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(patterns, options = {}) ⇒ Wildcard
constructor
A new instance of Wildcard.
Methods inherited from RegularExpression
#filter, normalize_regular_expressions, to_regexp
Methods inherited from Base
Constructor Details
#initialize(patterns, options = {}) ⇒ Wildcard
Returns a new instance of Wildcard.
39 40 41 42 43 44 45 46 |
# File 'lib/right_support/notifiers/blacklisters/wildcard.rb', line 39 def initialize(patterns, = {}) if [:delimiter] raise ::ArgumentError, 'options[:delimiter] is not allowed for wildcards.' end super( ::RightSupport::Notifier::Blacklister::Wildcard.to_regular_expressions(Array(patterns)), ) end |
Class Method Details
.to_regular_expressions(patterns) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/right_support/notifiers/blacklisters/wildcard.rb', line 48 def self.to_regular_expressions(patterns) patterns.map do |pattern| # remove leading/trailing slashes here = pattern == 'a/b/*' pattern = pattern.to_s.gsub(/^[\/]+(.*)/, '\2').gsub(/(.*)[\/]+$/, '\1') # remove trailing "/*" as matching the parent will obfuscate all children. while pattern.end_with?('/*') pattern = pattern[0...-2] end pattern = ::Regexp.escape(pattern) # escape all pattern = pattern.gsub('\?', '[^/]') # question mark == match any single non-slash character pattern = pattern.gsub('\*\*/', '.*/') # '**/' == match at any depth pattern = pattern.gsub('\*', '[^/]*') # asterisk == match zero or more non-slash characters ::Regexp.compile(pattern) end end |