automatic_namespaces 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4591f54c57c3df9887c0321b7105e03873f7f922576b9cbc6385dae4c40a1c11
4
- data.tar.gz: 3f478d56630b968d7b22d4f3feac07eadd9ed22535667a34d824fa8e3067f929
3
+ metadata.gz: e7aa390b64ae64d8c78d9b6ac2af5bd2e98d0c63007220eccba244f90ed3c674
4
+ data.tar.gz: 9acd543871604473d1ac25ca14d64e48873c18c7265ea4c9641def935737ecb4
5
5
  SHA512:
6
- metadata.gz: 2c8c2b1df45d0a7e6273a7f2b01ad521d87bd5ede3ca5f2e47107981191f4949c9fb997fe93478f97daf256b3499271ddd9ab865940940d82196795a01e7a9ee
7
- data.tar.gz: 7f6c638a11fc1c3ec1b70591f3f18e8d60f49c18f07e051281bd8c002da15b645ef9e34b9869b8eecebb398a72b4987652b48d01252541d974f8bde358c3a5bc
6
+ metadata.gz: 0b2c495a8cd14320988c3ee06a760f913611f1e2f5cf82d14ec0ba01d276b85bd00a8d404b1d6bed6e37a1a0ba454a76a1bfda15327fb445798751d03fef425d
7
+ data.tar.gz: df329e6def590f4714ef782ab235d82a89daee0fbf2cf3291527e1bd3cf719f24e7bb75981c40d659db98dc31140e3f4732b867eccf888d3daf6a966e06a51e4
data/README.md CHANGED
@@ -11,7 +11,7 @@ app
11
11
  │ │ ├── class1.rb # contains Component1::Class1
12
12
  ```
13
13
 
14
- When building a modular monolith using packages ([packwerk](https://github.com/Shopify/packwerk) + [stimpack](https://github.com/rubyatscale/stimpack)),
14
+ When building a modular monolith using packages ([packwerk](https://github.com/Shopify/packwerk) + [packs-rails](https://github.com/rubyatscale/packs-rails)),
15
15
  this pattern creates a lot of extra noise in the directory structure:
16
16
 
17
17
  ```
@@ -40,7 +40,7 @@ And that's only for a single pack! As your modular monolith grows, you'll likely
40
40
  hundreds) of packs. That's a lot of "namespace" directories that aren't adding a lot of value. You already
41
41
  know the namespace of those classes in a strongly namespaced pack -- it's the pack name -- can Zeitwerk know it, too?
42
42
 
43
- This gem patches the Rails 7 autoloader so that most subdirectories under your strongly namespaced component's `app` directory are
43
+ This gem configures the Rails 7 autoloader so that most subdirectories under your strongly namespaced component's `app` directory are
44
44
  automatically associated with the namespace.
45
45
 
46
46
  ## Installation
@@ -57,7 +57,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
57
57
 
58
58
  Given the `package.yml` of a strongly namespaced pack:
59
59
 
60
- ```
60
+ ```yml
61
61
  enforce_dependencies: true
62
62
  enforce_privacy: true
63
63
  public_path: app/public/
@@ -72,7 +72,7 @@ metadata:
72
72
 
73
73
  modify the metadata to opt into automatic namespacing:
74
74
 
75
- ```
75
+ ```yml
76
76
  metadata:
77
77
  automatic_pack_namespace: true
78
78
  ```
@@ -85,10 +85,21 @@ generally contain namespaced classes. These are exempted from `automatic_namespa
85
85
  * javascript
86
86
  * views
87
87
 
88
+ Additional directories can be excluded by adding them to the `automatic_pack_namespace_exclusions` key in your
89
+ `package.yml` file. This is useful if you require files in your pack that sit outside of your packs namespace:
90
+
91
+ ```yml
92
+ metadata:
93
+ automatic_pack_namespace: true
94
+ automatic_pack_namespace_exclusions:
95
+ - app/policies # Exclude pundit policies
96
+ - app/admin # Exclude active admin definition files
97
+ ```
98
+
88
99
  If your package / namespace name requires ActiveSupport inflections, you will probably need to tell `automatic_namespaces`
89
100
  what the correct namespace name should be in that package:
90
101
 
91
- ```
102
+ ```yml
92
103
  # packs/shoes_ui/package.yml
93
104
  metadata:
94
105
  automatic_pack_namespace: true
@@ -98,9 +109,19 @@ metadata:
98
109
  This is necessary because `automatic_namespaces` works by modifying the autoloader paths, which has to
99
110
  happen during Rails application initialization; but the inflector is not available for use then.
100
111
 
112
+ If you would like to use your own file layout conventions for packs (i.e. not `app/*`) you can specify
113
+ your own glob by using `autoload_glob` to append the glob to the folder containing package.yml. This defaults
114
+ to `'/**/app/*'`
115
+
116
+ ```yml
117
+ metadata:
118
+ # Put the folder containing package.yml as the root for the autoloader.
119
+ autoload_glob: ''
120
+ ```
121
+
101
122
  ## Development
102
123
 
103
- After checking out the repo, run `bundle instal` to install dependencies. Then, run `rspec` to run the tests.
124
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests.
104
125
 
105
126
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
106
127
 
@@ -1,35 +1,42 @@
1
1
  require 'yaml'
2
2
 
3
3
  class AutomaticNamespaces::Autoloader
4
+ DEFAULT_EXCLUDED_DIRS = %w[/app/helpers /app/inputs /app/javascript /app/views].freeze
5
+ PACKAGE_EXCLUDED_DIRS_KEY = "automatic_pack_namespace_exclusions".freeze
4
6
 
5
7
  def enable_automatic_namespaces
6
8
  namespaced_packages.each do |pack, metadata|
7
- package_namespace = define_namespace(pack, metadata)
8
- pack_directories(pack.path).each do |pack_dir|
9
- set_namespace_for(pack_dir, package_namespace)
10
- end
9
+ set_namespace_for_pack(pack, metadata)
10
+ end
11
+ end
12
+
13
+ def set_namespace_for_pack(pack, metadata)
14
+ package_namespace = define_namespace(pack, metadata)
15
+ pack_directories(pack.path, metadata).each do |pack_dir|
16
+ set_namespace_for_dir(pack_dir, package_namespace)
11
17
  end
12
18
  end
13
19
 
14
20
  private
15
21
 
16
- def set_namespace_for(pack_dir, package_namespace)
22
+ def set_namespace_for_dir(pack_dir, package_namespace)
17
23
  Rails.logger.debug { "Associating #{pack_dir} with namespace #{package_namespace}" }
18
24
  ActiveSupport::Dependencies.autoload_paths.delete(pack_dir)
19
25
  Rails.autoloaders.main.push_dir(pack_dir, namespace: package_namespace)
20
26
  Rails.application.config.watchable_dirs[pack_dir] = [:rb]
21
27
  end
22
28
 
23
- def pack_directories(pack_root_dir)
24
- Dir.glob("#{pack_root_dir}/**/app/*").reject { |dir| non_namspaced_directory(dir) }
29
+ def pack_directories(pack_root_dir, metadata)
30
+ glob = metadata['autoload_glob'] || "/**/app/*"
31
+ Dir.glob("#{pack_root_dir}#{glob}").select { |dir| namespaced_directory?(dir, metadata) }
32
+ end
33
+
34
+ def namespaced_directory?(dir, metadata)
35
+ excluded_directories(metadata).none? { |excluded_dir| dir.include?(excluded_dir) }
25
36
  end
26
37
 
27
- def non_namspaced_directory(dir)
28
- dir.include?('/app/assets') ||
29
- dir.include?('/app/helpers') || # Rails assumes helpers are global, not namespaced
30
- dir.include?('/app/inputs') || # Not sure how to namespace form inputs
31
- dir.include?('/app/javascript') ||
32
- dir.include?('/app/views')
38
+ def excluded_directories(metadata)
39
+ DEFAULT_EXCLUDED_DIRS + metadata.fetch(PACKAGE_EXCLUDED_DIRS_KEY, [])
33
40
  end
34
41
 
35
42
  def define_namespace(pack, metadata)
@@ -59,7 +66,3 @@ class AutomaticNamespaces::Autoloader
59
66
  package_description["metadata"]
60
67
  end
61
68
  end
62
-
63
-
64
-
65
-
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutomaticNamespaces
4
- VERSION = "0.3.1"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: automatic_namespaces
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Passero
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-28 00:00:00.000000000 Z
11
+ date: 2025-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description:
97
+ description:
98
98
  email:
99
99
  - gpassero@gmail.com
100
100
  executables: []
@@ -114,7 +114,7 @@ metadata:
114
114
  source_code_uri: https://github.com/gap777/automatic_namespaces
115
115
  changelog_uri: https://github.com/gap777/automatic_namespaces/releases
116
116
  allowed_push_host: https://rubygems.org
117
- post_install_message:
117
+ post_install_message:
118
118
  rdoc_options: []
119
119
  require_paths:
120
120
  - lib
@@ -129,8 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  requirements: []
132
- rubygems_version: 3.1.6
133
- signing_key:
132
+ rubygems_version: 3.5.11
133
+ signing_key:
134
134
  specification_version: 4
135
135
  summary: Modify autoloading to assume all files within a directory belong in a namespace
136
136
  test_files: []