Class: Auth::Sanitizer::ThingFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/auth/sanitizer/thing_filter.rb

Overview

Small value object for matching and filtering named things.

Used by multiple filtering surfaces in the library, such as inspected object attributes and debug-log key filtering.

‘ThingFilter` snapshots and duplicates its inputs on initialization so later mutation of caller-owned arrays or strings does not affect existing filter instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(things, label:) ⇒ ThingFilter

Create a new filter.

The provided values are duplicated and frozen so the filter remains stable for the lifetime of the object.

Parameters:

  • things (Enumerable<#to_s>)

    Names that should be filtered

  • label (String)

    Replacement label to use for filtered values



21
22
23
24
25
# File 'lib/auth/sanitizer/thing_filter.rb', line 21

def initialize(things, label:)
  @things = Array(things).map { |thing| thing.to_s.dup.freeze }.freeze
  @label = label.to_s.dup.freeze
  @pattern_source = Regexp.union(@things).source.freeze
end

Instance Attribute Details

#labelString (readonly)

The configured replacement label.

Returns:

  • (String)


35
36
37
# File 'lib/auth/sanitizer/thing_filter.rb', line 35

def label
  @label
end

#pattern_sourceString (readonly)

Build a regular-expression source for the configured thing names.

Useful when a filtering surface needs regex-based replacement rather than direct name checks.

Returns:

  • (String)


55
56
57
# File 'lib/auth/sanitizer/thing_filter.rb', line 55

def pattern_source
  @pattern_source
end

#thingsArray<String> (readonly)

The configured names that should be filtered.

Returns:

  • (Array<String>)


30
31
32
# File 'lib/auth/sanitizer/thing_filter.rb', line 30

def things
  @things
end

Instance Method Details

#filtered?(thing_name) ⇒ Boolean

True when the provided name matches any configured filter entry.

Matching is substring-based so it works naturally with instance-variable names used by ‘#inspect`, such as `@secret` matching `secret`.

Parameters:

  • thing_name (#to_s)

    Candidate thing name

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/auth/sanitizer/thing_filter.rb', line 44

def filtered?(thing_name)
  thing_name_str = thing_name.to_s
  things.any? { |thing| thing_name_str.include?(thing) }
end