diff --git a/Gemfile.lock b/Gemfile.lock index 6a87b2b..427f9a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - automatic_namespaces (0.4.0) + automatic_namespaces (0.5.0) activesupport packs-rails diff --git a/README.md b/README.md index 81d164c..80e5704 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ If bundler is not being used to manage dependencies, install the gem by executin Given the `package.yml` of a strongly namespaced pack: -``` +```yml enforce_dependencies: true enforce_privacy: true public_path: app/public/ @@ -72,7 +72,7 @@ metadata: modify the metadata to opt into automatic namespacing: -``` +```yml metadata: automatic_pack_namespace: true ``` @@ -99,7 +99,7 @@ metadata: If your package / namespace name requires ActiveSupport inflections, you will probably need to tell `automatic_namespaces` what the correct namespace name should be in that package: -``` +```yml # packs/shoes_ui/package.yml metadata: automatic_pack_namespace: true @@ -109,6 +109,16 @@ metadata: This is necessary because `automatic_namespaces` works by modifying the autoloader paths, which has to happen during Rails application initialization; but the inflector is not available for use then. +If you would like to use your own file layout conventions for packs (i.e. not `app/*`) you can specify +your own glob by using `autoload_glob` to append the glob to the folder containing package.yml. This defaults +to `'/**/app/*'` + +```yml +metadata: + # Put the folder containing package.yml as the root for the autoloader. + autoload_glob: '' +``` + ## Development After checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests. diff --git a/lib/automatic_namespaces/autoloader.rb b/lib/automatic_namespaces/autoloader.rb index 297e201..d4029e4 100644 --- a/lib/automatic_namespaces/autoloader.rb +++ b/lib/automatic_namespaces/autoloader.rb @@ -27,7 +27,8 @@ def set_namespace_for_dir(pack_dir, package_namespace) end def pack_directories(pack_root_dir, metadata) - Dir.glob("#{pack_root_dir}/**/app/*").select { |dir| namespaced_directory?(dir, metadata) } + glob = metadata['autoload_glob'] || "/**/app/*" + Dir.glob("#{pack_root_dir}#{glob}").select { |dir| namespaced_directory?(dir, metadata) } end def namespaced_directory?(dir, metadata) diff --git a/lib/automatic_namespaces/version.rb b/lib/automatic_namespaces/version.rb index ce1986e..352708d 100644 --- a/lib/automatic_namespaces/version.rb +++ b/lib/automatic_namespaces/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module AutomaticNamespaces - VERSION = "0.4.0" + VERSION = "0.5.0" end diff --git a/spec/automatic_namespaces_spec.rb b/spec/automatic_namespaces_spec.rb index 7afc1ef..bf0a59e 100644 --- a/spec/automatic_namespaces_spec.rb +++ b/spec/automatic_namespaces_spec.rb @@ -49,4 +49,10 @@ expect(defined?(Sneaker)).to eq("constant") end end + + it "can find classes that are located at the root of the package.yml" do + expect(defined?(Summer::Fun)).to eq("constant") + expect(defined?(Summer::Swim)).to eq("constant") + expect(defined?(Summer::Swim::Trunks)).to eq("constant") + end end diff --git a/spec/fixtures/rails-7.0/packs/summer/app/domain/fun.rb b/spec/fixtures/rails-7.0/packs/summer/app/domain/fun.rb new file mode 100644 index 0000000..81c5c4f --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/summer/app/domain/fun.rb @@ -0,0 +1 @@ +module Summer::Fun; end diff --git a/spec/fixtures/rails-7.0/packs/summer/package.yml b/spec/fixtures/rails-7.0/packs/summer/package.yml new file mode 100644 index 0000000..692bf00 --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/summer/package.yml @@ -0,0 +1,2 @@ +metadata: + automatic_pack_namespace: true diff --git a/spec/fixtures/rails-7.0/packs/summer/swim/package.yml b/spec/fixtures/rails-7.0/packs/summer/swim/package.yml new file mode 100644 index 0000000..1d0205a --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/summer/swim/package.yml @@ -0,0 +1,4 @@ +metadata: + automatic_pack_namespace: true + namespace_override: "Summer::Swim" + autoload_glob: '' diff --git a/spec/fixtures/rails-7.0/packs/summer/swim/trunks.rb b/spec/fixtures/rails-7.0/packs/summer/swim/trunks.rb new file mode 100644 index 0000000..ee5c077 --- /dev/null +++ b/spec/fixtures/rails-7.0/packs/summer/swim/trunks.rb @@ -0,0 +1 @@ +class Summer::Swim::Trunks; end