module Auth::Sanitizer::FilteredAttributes

Mixin that redacts sensitive instance variables in ‘#inspect` output.

Classes include this module and declare which attribute names should be filtered via {.filtered_attributes}. Matching and replacement behavior is delegated to {ThingFilter}, which is initialized once per object.

This means existing objects keep the filter configuration that was present when they were initialized, even if global config or class-level filter declarations change later.

The label used for redaction is resolved from {Auth::Sanitizer.filtered_label} at object initialization time. Host gems may customize this by installing a provider via {Auth::Sanitizer.filtered_label_provider=}.

Constants

INSPECTED_REDACTABLE_VALUE
INSPECTED_STRING_VALUE

Public Class Methods

included(base) click to toggle source

Hook invoked when the module is included. Extends the including class with class-level helpers and prepends the initializer hook.

@param [Class] base The including class @return [void]

   # File lib/auth/sanitizer/filtered_attributes.rb
25 def included(base)
26   base.extend(ClassMethods)
27   base.prepend(InitializerMethods)
28 end

Public Instance Methods

inspect() click to toggle source

Custom inspect that redacts configured attributes.

@return [String]

Calls superclass method
    # File lib/auth/sanitizer/filtered_attributes.rb
 95 def inspect
 96   inspected = super
 97   return inspected if thing_filter.things.empty?
 98 
 99   redact_inspected_values(inspected.dup)
100 end
thing_filter() click to toggle source

The initialized thing filter used by this object.

This is a per-instance snapshot created during initialization.

@return [ThingFilter]

   # File lib/auth/sanitizer/filtered_attributes.rb
88 def thing_filter
89   @thing_filter
90 end

Private Instance Methods

redact_inspected_values(inspected) click to toggle source
    # File lib/auth/sanitizer/filtered_attributes.rb
116 def redact_inspected_values(inspected)
117   inspected.gsub(INSPECTED_REDACTABLE_VALUE) do |match|
118     captures = Regexp.last_match.captures
119     prefix, = captures.each_slice(2).detect do |(_candidate_prefix, candidate_key)|
120       thing_filter.things.include?(candidate_key)
121     end
122     prefix ? "#{prefix}#{thing_filter.label}" : match
123   end
124 end