module AuthSanitizer::Loader
Loader for consumers that need Auth::Sanitizer without defining a top-level Auth constant in the host application.
Constants
- FILES
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 23 def load_isolated 24 namespace = Module.new 25 auth_namespace = Module.new 26 namespace.const_set(:Auth, auth_namespace) 27 28 FILES.each do |relative_path| 29 path = File.expand_path("../#{relative_path}", __dir__) 30 auth_namespace.module_eval(isolated_source(path), path, 1) 31 end 32 33 namespace.const_get(:Auth).const_get(:Sanitizer) 34 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.
# File lib/auth_sanitizer/loader.rb 42 def isolated_source(path) 43 lines = File.readlines(path) 44 wrapper_index = lines.index("module Auth\n") 45 return lines.join.split("Auth::Sanitizer").join("Sanitizer") unless wrapper_index 46 47 lines.delete_at(wrapper_index) 48 closing_index = lines.rindex("end\n") 49 lines.delete_at(closing_index) if closing_index 50 51 wrapper_index.upto(lines.length - 1) do |index| 52 line = lines[index] 53 lines[index] = line.start_with?(" ") ? line[2..-1] : line 54 end 55 lines.join.split("Auth::Sanitizer").join("Sanitizer") 56 end