From 3159275e9fdd7451e12692b6313961f2af889b6e Mon Sep 17 00:00:00 2001 From: Victor Andrey Date: Wed, 13 Nov 2024 12:16:38 -0800 Subject: [PATCH] Expose access to set_namespace_for_pack, so that callers can use it in other situations (#23) Applications might want to enforce automatic namespaces based on certain criteria specific to their package.yml, let's say, version: v2, or any other application-specific property. By exposing a public method to do the namespacing, applications can call this method rather than duplicating the code from this gem. This change allows applications to hook into the set_namespace_for_pack 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)