module Auth::Sanitizer::FilteredAttributes
Mixin that redacts sensitive instance variables in โ#inspect` output.
Classes include this module and declare attribute names to filter via {.filtered_attributes}. Matching and replacement behavior is delegated to {ThingFilter}, 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
Matches redactable key/value pairs inside โ#inspect` output.
- INSPECTED_STRING_VALUE
Matches a quoted string literal inside โ#inspect` output.
Public Class Methods
Hook invoked when the module is included. Extends the including class with class-level helpers and prepends the initializer hook.
@param base [Class] The including class @return [void]
# File lib/auth/sanitizer/filtered_attributes.rb 23 def included(base) 24 base.extend(ClassMethods) 25 base.prepend(InitializerMethods) 26 end
Public Instance Methods
Custom inspect that redacts configured attributes.
@return [String]
# File lib/auth/sanitizer/filtered_attributes.rb 94 def inspect 95 inspected = super 96 return inspected if thing_filter.things.empty? 97 98 redact_inspected_values(inspected.dup) 99 end
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 87 def thing_filter 88 @thing_filter 89 end
Private Instance Methods
Redact filtered values from โ#inspect` output text.
@param inspected [String] The โ#inspect` output to redact @return [String] The redacted inspect output
# File lib/auth/sanitizer/filtered_attributes.rb 121 def redact_inspected_values(inspected) 122 inspected.gsub(INSPECTED_REDACTABLE_VALUE) do |match| 123 captures = Regexp.last_match.captures 124 prefix, = captures.each_slice(2).detect do |(_candidate_prefix, candidate_key)| 125 thing_filter.things.include?(candidate_key) 126 end 127 prefix ? "#{prefix}#{thing_filter.label}" : match 128 end 129 end