Skip to content

Commit

Permalink
Expose access to set_namespace_for_pack, so that callers can use it i…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
vaot authored Nov 13, 2024
1 parent 40f4c1d commit 3159275
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/automatic_namespaces/autoloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3159275

Please sign in to comment.