module AuthSanitizer::Loader

Loader for consumers that need Auth::Sanitizer without defining a top-level Auth constant in the host application.

Constants

FILES

Gem source files evaluated into the anonymous namespace, in load order.

Public Class Methods

load_isolated() click to toggle source

Load Auth::Sanitizer into an anonymous namespace and return the nested Auth::Sanitizer module from that namespace.

This uses Module#module_eval with explicit file and line metadata so it works on Ruby 2.2+, where Kernel.load(path, module) is unavailable.

@return [Module] isolated Auth::Sanitizer module

   # File lib/auth_sanitizer/loader.rb
26 def load_isolated
27   namespace = Module.new
28   auth_namespace = Module.new
29   namespace.const_set(:Auth, auth_namespace)
30 
31   FILES.each do |relative_path|
32     path = File.expand_path("../#{relative_path}", __dir__)
33     auth_namespace.module_eval(isolated_source(path), path, 1)
34   end
35 
36   namespace.const_get(:Auth).const_get(:Sanitizer)
37 end

Private Class Methods

isolated_source(path) click to toggle source

Remove the public top-level Auth wrapper before evaluating a file inside the anonymous Auth namespace. This keeps the normal files unchanged while avoiding Object::Auth leakage on runtimes where Module#module_eval still resolves nested module declarations through Object.

@param path [String] Absolute path to the gem source file @return [String] The source with the top-level wrapper removed

   # File lib/auth_sanitizer/loader.rb
48 def isolated_source(path)
49   lines = File.readlines(path)
50   wrapper_index = lines.index("module Auth\n")
51   return lines.join.split("Auth::Sanitizer").join("Sanitizer") unless wrapper_index
52 
53   lines.delete_at(wrapper_index)
54   closing_index = lines.rindex("end\n")
55   lines.delete_at(closing_index) if closing_index
56 
57   wrapper_index.upto(lines.length - 1) do |index|
58     line = lines[index]
59     lines[index] = line.start_with?("  ") ? line[2..-1] : line
60   end
61   lines.join.split("Auth::Sanitizer").join("Sanitizer")
62 end