From 5a8305dc72d441898b3334f9471198dabc8f8f0c Mon Sep 17 00:00:00 2001 From: vaot Date: Tue, 12 Nov 2024 15:38:03 -0800 Subject: [PATCH] Move logic to set a specific pack namespace to its own method. This allows applications to hook into this public method to do any on-demand custom namespacing without duplicating the logic defined in this gem. --- lib/automatic_namespaces/autoloader.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/automatic_namespaces/autoloader.rb b/lib/automatic_namespaces/autoloader.rb index eb51714..297e201 100644 --- a/lib/automatic_namespaces/autoloader.rb +++ b/lib/automatic_namespaces/autoloader.rb @@ -6,16 +6,20 @@ class AutomaticNamespaces::Autoloader def enable_automatic_namespaces namespaced_packages.each do |pack, metadata| - package_namespace = define_namespace(pack, metadata) - pack_directories(pack.path, metadata).each do |pack_dir| - set_namespace_for(pack_dir, package_namespace) - end + set_namespace_for_pack(pack, metadata) + end + end + + def set_namespace_for_pack(pack, metadata) + package_namespace = define_namespace(pack, metadata) + pack_directories(pack.path, metadata).each do |pack_dir| + set_namespace_for_dir(pack_dir, package_namespace) end end private - def set_namespace_for(pack_dir, package_namespace) + def set_namespace_for_dir(pack_dir, package_namespace) Rails.logger.debug { "Associating #{pack_dir} with namespace #{package_namespace}" } ActiveSupport::Dependencies.autoload_paths.delete(pack_dir) Rails.autoloaders.main.push_dir(pack_dir, namespace: package_namespace)