rails-rfc6570 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/lib/rails/rfc6570/version.rb +1 -1
- data/rails-rfc6570.gemspec +1 -1
- data/spec/dummy/log/test.log +432 -0
- metadata +10 -262
- data/vendor/bundle/ruby/3.3.0/bundler/gems/rubocop-config-943facea9d8f/LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/bundler/gems/rubocop-config-943facea9d8f/README.md +0 -23
- data/vendor/bundle/ruby/3.3.0/bundler/gems/rubocop-config-943facea9d8f/rubocop-config.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/CHANGELOG.md +0 -154
- data/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/MIT-LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/actionpack-7.2.1/README.rdoc +0 -57
- data/vendor/bundle/ruby/3.3.0/gems/actionview-7.2.1/CHANGELOG.md +0 -113
- data/vendor/bundle/ruby/3.3.0/gems/actionview-7.2.1/MIT-LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/actionview-7.2.1/README.rdoc +0 -40
- data/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/CHANGELOG.md +0 -258
- data/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/MIT-LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.1/README.rdoc +0 -40
- data/vendor/bundle/ruby/3.3.0/gems/addressable-2.8.7/CHANGELOG.md +0 -301
- data/vendor/bundle/ruby/3.3.0/gems/addressable-2.8.7/LICENSE.txt +0 -202
- data/vendor/bundle/ruby/3.3.0/gems/addressable-2.8.7/README.md +0 -121
- data/vendor/bundle/ruby/3.3.0/gems/addressable-2.8.7/addressable.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/gems/appraisal-2.5.0/MIT-LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/appraisal-2.5.0/README.md +0 -243
- data/vendor/bundle/ruby/3.3.0/gems/appraisal-2.5.0/appraisal.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/gems/ast-2.4.2/LICENSE.MIT +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/ast-2.4.2/README.YARD.md +0 -12
- data/vendor/bundle/ruby/3.3.0/gems/benchmark-ips-2.13.0/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/benchmark-ips-2.13.0/README.md +0 -279
- data/vendor/bundle/ruby/3.3.0/gems/bigdecimal-3.1.8/LICENSE +0 -56
- data/vendor/bundle/ruby/3.3.0/gems/bigdecimal-3.1.8/bigdecimal.gemspec +0 -57
- data/vendor/bundle/ruby/3.3.0/gems/builder-3.3.0/MIT-LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/builder-3.3.0/README.md +0 -258
- data/vendor/bundle/ruby/3.3.0/gems/builder-3.3.0/builder.gemspec +0 -49
- data/vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/CHANGELOG.md +0 -934
- data/vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/LICENSE +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/README.md +0 -199
- data/vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/MIT-LICENSE +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/README_INDEX.rdoc +0 -123
- data/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/CHANGELOG.md +0 -593
- data/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/LICENSE.txt +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/README.md +0 -407
- data/vendor/bundle/ruby/3.3.0/gems/connection_pool-2.4.1/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/connection_pool-2.4.1/README.md +0 -135
- data/vendor/bundle/ruby/3.3.0/gems/connection_pool-2.4.1/connection_pool.gemspec +0 -24
- data/vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/LICENSE +0 -18
- data/vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/README.md +0 -192
- data/vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/crass.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/gems/diff-lcs-1.5.1/README.rdoc +0 -84
- data/vendor/bundle/ruby/3.3.0/gems/docile-1.4.1/LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/docile-1.4.1/README.md +0 -409
- data/vendor/bundle/ruby/3.3.0/gems/docile-1.4.1/docile.gemspec +0 -36
- data/vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/drb-2.2.1/drb.gemspec +0 -41
- data/vendor/bundle/ruby/3.3.0/gems/erubi-1.13.0/CHANGELOG +0 -107
- data/vendor/bundle/ruby/3.3.0/gems/erubi-1.13.0/MIT-LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/erubi-1.13.0/README.rdoc +0 -151
- data/vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/MIT-LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/README.md +0 -127
- data/vendor/bundle/ruby/3.3.0/gems/io-console-0.7.2/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/io-console-0.7.2/README.md +0 -46
- data/vendor/bundle/ruby/3.3.0/gems/irb-1.14.0/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/irb-1.14.0/README.md +0 -405
- data/vendor/bundle/ruby/3.3.0/gems/irb-1.14.0/irb.gemspec +0 -46
- data/vendor/bundle/ruby/3.3.0/gems/json-2.7.2/LICENSE +0 -56
- data/vendor/bundle/ruby/3.3.0/gems/json-2.7.2/README.md +0 -416
- data/vendor/bundle/ruby/3.3.0/gems/json-2.7.2/json.gemspec +0 -68
- data/vendor/bundle/ruby/3.3.0/gems/language_server-protocol-3.17.0.3/LICENSE.txt +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/language_server-protocol-3.17.0.3/README.md +0 -88
- data/vendor/bundle/ruby/3.3.0/gems/loofah-2.22.0/CHANGELOG.md +0 -566
- data/vendor/bundle/ruby/3.3.0/gems/loofah-2.22.0/MIT-LICENSE.txt +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/loofah-2.22.0/README.md +0 -363
- data/vendor/bundle/ruby/3.3.0/gems/method_source-1.1.0/CHANGELOG.md +0 -15
- data/vendor/bundle/ruby/3.3.0/gems/method_source-1.1.0/LICENSE +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/method_source-1.1.0/README.markdown +0 -101
- data/vendor/bundle/ruby/3.3.0/gems/method_source-1.1.0/method_source.gemspec +0 -19
- data/vendor/bundle/ruby/3.3.0/gems/minitest-5.25.1/README.rdoc +0 -835
- data/vendor/bundle/ruby/3.3.0/gems/nokogiri-1.16.7-x86_64-linux/LICENSE-DEPENDENCIES.md +0 -2224
- data/vendor/bundle/ruby/3.3.0/gems/nokogiri-1.16.7-x86_64-linux/LICENSE.md +0 -9
- data/vendor/bundle/ruby/3.3.0/gems/nokogiri-1.16.7-x86_64-linux/README.md +0 -290
- data/vendor/bundle/ruby/3.3.0/gems/parallel-1.26.3/MIT-LICENSE.txt +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/parser-3.3.4.2/LICENSE.txt +0 -25
- data/vendor/bundle/ruby/3.3.0/gems/parser-3.3.4.2/parser.gemspec +0 -43
- data/vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/CHANGELOG.md +0 -1141
- data/vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/LICENSE +0 -25
- data/vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/README.md +0 -466
- data/vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/slop/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/pry-byebug-3.10.1/CHANGELOG.md +0 -230
- data/vendor/bundle/ruby/3.3.0/gems/pry-byebug-3.10.1/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/pry-byebug-3.10.1/README.md +0 -189
- data/vendor/bundle/ruby/3.3.0/gems/public_suffix-6.0.1/CHANGELOG.md +0 -491
- data/vendor/bundle/ruby/3.3.0/gems/public_suffix-6.0.1/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/public_suffix-6.0.1/README.md +0 -222
- data/vendor/bundle/ruby/3.3.0/gems/racc-1.8.1/README.ja.rdoc +0 -58
- data/vendor/bundle/ruby/3.3.0/gems/racc-1.8.1/README.rdoc +0 -60
- data/vendor/bundle/ruby/3.3.0/gems/rack-3.1.7/CHANGELOG.md +0 -994
- data/vendor/bundle/ruby/3.3.0/gems/rack-3.1.7/MIT-LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/rack-3.1.7/README.md +0 -328
- data/vendor/bundle/ruby/3.3.0/gems/rack-test-2.1.0/MIT-LICENSE.txt +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/rack-test-2.1.0/README.md +0 -139
- data/vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/MIT-LICENSE +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/README.md +0 -91
- data/vendor/bundle/ruby/3.3.0/gems/rails-html-sanitizer-1.6.0/CHANGELOG.md +0 -201
- data/vendor/bundle/ruby/3.3.0/gems/rails-html-sanitizer-1.6.0/MIT-LICENSE +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/rails-html-sanitizer-1.6.0/README.md +0 -271
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/CHANGELOG.md +0 -361
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/MIT-LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/README.rdoc +0 -39
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/generators/rails/app/templates/README.md.tt +0 -24
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt +0 -28
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/generators/rails/plugin/templates/README.md.tt +0 -28
- data/vendor/bundle/ruby/3.3.0/gems/rainbow-3.1.1/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/rainbow-3.1.1/README.markdown +0 -227
- data/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/MIT-LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/README.rdoc +0 -155
- data/vendor/bundle/ruby/3.3.0/gems/rake-13.2.1/rake.gemspec +0 -101
- data/vendor/bundle/ruby/3.3.0/gems/rake-release-1.3.0/LICENSE +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/rake-release-1.3.0/README.md +0 -107
- data/vendor/bundle/ruby/3.3.0/gems/rake-release-1.3.0/rake-release.gemspec +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/rdoc-6.7.0/LICENSE.rdoc +0 -57
- data/vendor/bundle/ruby/3.3.0/gems/rdoc-6.7.0/README.rdoc +0 -129
- data/vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.9.2/LICENSE +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.9.2/regexp_parser.gemspec +0 -34
- data/vendor/bundle/ruby/3.3.0/gems/reline-0.5.9/README.md +0 -94
- data/vendor/bundle/ruby/3.3.0/gems/rexml-3.3.6/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/rexml-3.3.6/README.md +0 -57
- data/vendor/bundle/ruby/3.3.0/gems/rspec-3.13.0/LICENSE.md +0 -27
- data/vendor/bundle/ruby/3.3.0/gems/rspec-3.13.0/README.md +0 -47
- data/vendor/bundle/ruby/3.3.0/gems/rspec-core-3.13.0/LICENSE.md +0 -26
- data/vendor/bundle/ruby/3.3.0/gems/rspec-core-3.13.0/README.md +0 -389
- data/vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.2/LICENSE.md +0 -25
- data/vendor/bundle/ruby/3.3.0/gems/rspec-expectations-3.13.2/README.md +0 -326
- data/vendor/bundle/ruby/3.3.0/gems/rspec-mocks-3.13.1/LICENSE.md +0 -25
- data/vendor/bundle/ruby/3.3.0/gems/rspec-mocks-3.13.1/README.md +0 -465
- data/vendor/bundle/ruby/3.3.0/gems/rspec-rails-6.1.4/LICENSE.md +0 -25
- data/vendor/bundle/ruby/3.3.0/gems/rspec-rails-6.1.4/README.md +0 -381
- data/vendor/bundle/ruby/3.3.0/gems/rspec-support-3.13.1/LICENSE.md +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/rspec-support-3.13.1/README.md +0 -40
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-1.65.1/LICENSE.txt +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-1.65.1/README.md +0 -251
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.32.1/LICENSE.txt +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-ast-1.32.1/README.md +0 -56
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-capybara-2.21.0/CHANGELOG.md +0 -85
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-capybara-2.21.0/MIT-LICENSE.md +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-capybara-2.21.0/README.md +0 -88
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-factory_bot-2.26.1/CHANGELOG.md +0 -116
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-factory_bot-2.26.1/MIT-LICENSE.md +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-factory_bot-2.26.1/README.md +0 -88
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.1/LICENSE.txt +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.1/README.md +0 -97
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.4/CHANGELOG.md +0 -1028
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.4/MIT-LICENSE.md +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.4/README.md +0 -109
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-rspec_rails-2.30.0/CHANGELOG.md +0 -84
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-rspec_rails-2.30.0/MIT-LICENSE.md +0 -21
- data/vendor/bundle/ruby/3.3.0/gems/rubocop-rspec_rails-2.30.0/README.md +0 -90
- data/vendor/bundle/ruby/3.3.0/gems/ruby-progressbar-1.13.0/LICENSE.txt +0 -19
- data/vendor/bundle/ruby/3.3.0/gems/ruby-progressbar-1.13.0/README.md +0 -131
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-0.22.0/CHANGELOG.md +0 -191
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-0.22.0/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-0.22.0/README.md +0 -974
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-cobertura-2.1.0/LICENSE +0 -202
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-cobertura-2.1.0/README.md +0 -62
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-cobertura-2.1.0/simplecov-cobertura.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-html-0.12.3/CHANGELOG.md +0 -81
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-html-0.12.3/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-html-0.12.3/README.md +0 -30
- data/vendor/bundle/ruby/3.3.0/gems/simplecov-html-0.12.3/simplecov-html.gemspec +0 -23
- data/vendor/bundle/ruby/3.3.0/gems/simplecov_json_formatter-0.1.4/CHANGELOG.md +0 -13
- data/vendor/bundle/ruby/3.3.0/gems/simplecov_json_formatter-0.1.4/README.md +0 -29
- data/vendor/bundle/ruby/3.3.0/gems/strscan-3.1.0/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/LICENSE.md +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/README.md +0 -45
- data/vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/thor.gemspec +0 -33
- data/vendor/bundle/ruby/3.3.0/gems/tzinfo-2.0.6/LICENSE +0 -19
- data/vendor/bundle/ruby/3.3.0/gems/tzinfo-2.0.6/README.md +0 -406
- data/vendor/bundle/ruby/3.3.0/gems/unicode-display_width-2.5.0/CHANGELOG.md +0 -196
- data/vendor/bundle/ruby/3.3.0/gems/unicode-display_width-2.5.0/MIT-LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/unicode-display_width-2.5.0/README.md +0 -171
- data/vendor/bundle/ruby/3.3.0/gems/useragent-0.16.10/LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/useragent-0.16.10/README.md +0 -42
- data/vendor/bundle/ruby/3.3.0/gems/webrick-1.8.1/LICENSE.txt +0 -22
- data/vendor/bundle/ruby/3.3.0/gems/webrick-1.8.1/README.md +0 -61
- data/vendor/bundle/ruby/3.3.0/gems/webrick-1.8.1/webrick.gemspec +0 -70
- data/vendor/bundle/ruby/3.3.0/gems/zeitwerk-2.6.17/MIT-LICENSE +0 -20
- data/vendor/bundle/ruby/3.3.0/gems/zeitwerk-2.6.17/README.md +0 -1494
- data/vendor/bundle/ruby/3.3.0/specifications/actionpack-7.2.1.gemspec +0 -37
- data/vendor/bundle/ruby/3.3.0/specifications/actionview-7.2.1.gemspec +0 -33
- data/vendor/bundle/ruby/3.3.0/specifications/activesupport-7.2.1.gemspec +0 -36
- data/vendor/bundle/ruby/3.3.0/specifications/addressable-2.8.7.gemspec +0 -30
- data/vendor/bundle/ruby/3.3.0/specifications/appraisal-2.5.0.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/ast-2.4.2.gemspec +0 -30
- data/vendor/bundle/ruby/3.3.0/specifications/benchmark-ips-2.13.0.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/bigdecimal-3.1.8.gemspec +0 -25
- data/vendor/bundle/ruby/3.3.0/specifications/builder-3.3.0.gemspec +0 -22
- data/vendor/bundle/ruby/3.3.0/specifications/byebug-11.1.3.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/coderay-1.1.3.gemspec +0 -25
- data/vendor/bundle/ruby/3.3.0/specifications/concurrent-ruby-1.3.4.gemspec +0 -24
- data/vendor/bundle/ruby/3.3.0/specifications/connection_pool-2.4.1.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/crass-1.0.6.gemspec +0 -27
- data/vendor/bundle/ruby/3.3.0/specifications/diff-lcs-1.5.1.gemspec +0 -37
- data/vendor/bundle/ruby/3.3.0/specifications/docile-1.4.1.gemspec +0 -22
- data/vendor/bundle/ruby/3.3.0/specifications/drb-2.2.1.gemspec +0 -22
- data/vendor/bundle/ruby/3.3.0/specifications/erubi-1.13.0.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/i18n-1.14.5.gemspec +0 -26
- data/vendor/bundle/ruby/3.3.0/specifications/io-console-0.7.2.gemspec +0 -25
- data/vendor/bundle/ruby/3.3.0/specifications/irb-1.14.0.gemspec +0 -30
- data/vendor/bundle/ruby/3.3.0/specifications/json-2.7.2.gemspec +0 -0
- data/vendor/bundle/ruby/3.3.0/specifications/language_server-protocol-3.17.0.3.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/loofah-2.22.0.gemspec +0 -27
- data/vendor/bundle/ruby/3.3.0/specifications/method_source-1.1.0.gemspec +0 -21
- data/vendor/bundle/ruby/3.3.0/specifications/minitest-5.25.1.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/nokogiri-1.16.7-x86_64-linux.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/parallel-1.26.3.gemspec +0 -21
- data/vendor/bundle/ruby/3.3.0/specifications/parser-3.3.4.2.gemspec +0 -37
- data/vendor/bundle/ruby/3.3.0/specifications/pry-0.14.2.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/pry-byebug-3.10.1.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/public_suffix-6.0.1.gemspec +0 -24
- data/vendor/bundle/ruby/3.3.0/specifications/racc-1.8.1.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/rack-3.1.7.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/rack-session-2.0.0.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/rack-test-2.1.0.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/rackup-2.1.0.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/rails-dom-testing-2.2.0.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/rails-html-sanitizer-1.6.0.gemspec +0 -27
- data/vendor/bundle/ruby/3.3.0/specifications/railties-7.2.1.gemspec +0 -37
- data/vendor/bundle/ruby/3.3.0/specifications/rainbow-3.1.1.gemspec +0 -25
- data/vendor/bundle/ruby/3.3.0/specifications/rake-13.2.1.gemspec +0 -26
- data/vendor/bundle/ruby/3.3.0/specifications/rake-release-1.3.0.gemspec +0 -26
- data/vendor/bundle/ruby/3.3.0/specifications/rdoc-6.7.0.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/regexp_parser-2.9.2.gemspec +0 -22
- data/vendor/bundle/ruby/3.3.0/specifications/reline-0.5.9.gemspec +0 -26
- data/vendor/bundle/ruby/3.3.0/specifications/rexml-3.3.6.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/rspec-3.13.0.gemspec +0 -31
- data/vendor/bundle/ruby/3.3.0/specifications/rspec-core-3.13.0.gemspec +0 -39
- data/vendor/bundle/ruby/3.3.0/specifications/rspec-expectations-3.13.2.gemspec +0 -33
- data/vendor/bundle/ruby/3.3.0/specifications/rspec-mocks-3.13.1.gemspec +0 -33
- data/vendor/bundle/ruby/3.3.0/specifications/rspec-rails-6.1.4.gemspec +0 -37
- data/vendor/bundle/ruby/3.3.0/specifications/rspec-support-3.13.1.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-1.65.1.gemspec +0 -39
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-ast-1.32.1.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-capybara-2.21.0.gemspec +0 -27
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-factory_bot-2.26.1.gemspec +0 -27
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-performance-1.21.1.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-rspec-3.0.4.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/rubocop-rspec_rails-2.30.0.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/ruby-progressbar-1.13.0.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/simplecov-0.22.0.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/simplecov-cobertura-2.1.0.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/simplecov-html-0.12.3.gemspec +0 -21
- data/vendor/bundle/ruby/3.3.0/specifications/simplecov_json_formatter-0.1.4.gemspec +0 -21
- data/vendor/bundle/ruby/3.3.0/specifications/strscan-3.1.0.gemspec +0 -24
- data/vendor/bundle/ruby/3.3.0/specifications/thor-1.3.1.gemspec +0 -28
- data/vendor/bundle/ruby/3.3.0/specifications/tzinfo-2.0.6.gemspec +0 -30
- data/vendor/bundle/ruby/3.3.0/specifications/unicode-display_width-2.5.0.gemspec +0 -29
- data/vendor/bundle/ruby/3.3.0/specifications/useragent-0.16.10.gemspec +0 -25
- data/vendor/bundle/ruby/3.3.0/specifications/webrick-1.8.1.gemspec +0 -22
- data/vendor/bundle/ruby/3.3.0/specifications/zeitwerk-2.6.17.gemspec +0 -22
@@ -1,1494 +0,0 @@
|
|
1
|
-
# Zeitwerk
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
[![Gem Version](https://img.shields.io/gem/v/zeitwerk.svg?style=for-the-badge)](https://rubygems.org/gems/zeitwerk)
|
6
|
-
[![Build Status](https://img.shields.io/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3Amain)
|
7
|
-
|
8
|
-
|
9
|
-
<!-- TOC -->
|
10
|
-
|
11
|
-
- [Introduction](#introduction)
|
12
|
-
- [Synopsis](#synopsis)
|
13
|
-
- [File structure](#file-structure)
|
14
|
-
- [The idea: File paths match constant paths](#the-idea-file-paths-match-constant-paths)
|
15
|
-
- [Inner simple constants](#inner-simple-constants)
|
16
|
-
- [Root directories and root namespaces](#root-directories-and-root-namespaces)
|
17
|
-
- [The default root namespace is `Object`](#the-default-root-namespace-is-object)
|
18
|
-
- [Custom root namespaces](#custom-root-namespaces)
|
19
|
-
- [Nested root directories](#nested-root-directories)
|
20
|
-
- [Implicit namespaces](#implicit-namespaces)
|
21
|
-
- [Explicit namespaces](#explicit-namespaces)
|
22
|
-
- [Collapsing directories](#collapsing-directories)
|
23
|
-
- [Testing compliance](#testing-compliance)
|
24
|
-
- [Usage](#usage)
|
25
|
-
- [Setup](#setup)
|
26
|
-
- [Generic](#generic)
|
27
|
-
- [for_gem](#for_gem)
|
28
|
-
- [for_gem_extension](#for_gem_extension)
|
29
|
-
- [Autoloading](#autoloading)
|
30
|
-
- [Eager loading](#eager-loading)
|
31
|
-
- [Eager load exclusions](#eager-load-exclusions)
|
32
|
-
- [Eager load directories](#eager-load-directories)
|
33
|
-
- [Eager load namespaces](#eager-load-namespaces)
|
34
|
-
- [Eager load namespaces shared by several loaders](#eager-load-namespaces-shared-by-several-loaders)
|
35
|
-
- [Global eager load](#global-eager-load)
|
36
|
-
- [Loading individual files](#loading-individual-files)
|
37
|
-
- [Reloading](#reloading)
|
38
|
-
- [Configuration and usage](#configuration-and-usage)
|
39
|
-
- [Thread-safety](#thread-safety)
|
40
|
-
- [Inflection](#inflection)
|
41
|
-
- [Zeitwerk::Inflector](#zeitwerkinflector)
|
42
|
-
- [Zeitwerk::GemInflector](#zeitwerkgeminflector)
|
43
|
-
- [Zeitwerk::NullInflector](#zeitwerknullinflector)
|
44
|
-
- [Custom inflector](#custom-inflector)
|
45
|
-
- [Callbacks](#callbacks)
|
46
|
-
- [The on_setup callback](#the-on_setup-callback)
|
47
|
-
- [The on_load callback](#the-on_load-callback)
|
48
|
-
- [The on_unload callback](#the-on_unload-callback)
|
49
|
-
- [Technical details](#technical-details)
|
50
|
-
- [Logging](#logging)
|
51
|
-
- [Loader tag](#loader-tag)
|
52
|
-
- [Ignoring parts of the project](#ignoring-parts-of-the-project)
|
53
|
-
- [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
|
54
|
-
- [Use case: The adapter pattern](#use-case-the-adapter-pattern)
|
55
|
-
- [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
|
56
|
-
- [Shadowed files](#shadowed-files)
|
57
|
-
- [Edge cases](#edge-cases)
|
58
|
-
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
59
|
-
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
60
|
-
- [Introspection](#introspection)
|
61
|
-
- [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
|
62
|
-
- [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
|
63
|
-
- [`Zeitwerk::Loader#all_expected_cpaths`](#zeitwerkloaderall_expected_cpaths)
|
64
|
-
- [Encodings](#encodings)
|
65
|
-
- [Rules of thumb](#rules-of-thumb)
|
66
|
-
- [Debuggers](#debuggers)
|
67
|
-
- [Pronunciation](#pronunciation)
|
68
|
-
- [Supported Ruby versions](#supported-ruby-versions)
|
69
|
-
- [Testing](#testing)
|
70
|
-
- [Motivation](#motivation)
|
71
|
-
- [Kernel#require is brittle](#kernelrequire-is-brittle)
|
72
|
-
- [Rails autoloading was brittle](#rails-autoloading-was-brittle)
|
73
|
-
- [Awards](#awards)
|
74
|
-
- [Thanks](#thanks)
|
75
|
-
- [License](#license)
|
76
|
-
|
77
|
-
<!-- /TOC -->
|
78
|
-
|
79
|
-
<a id="markdown-introduction" name="introduction"></a>
|
80
|
-
## Introduction
|
81
|
-
|
82
|
-
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
83
|
-
|
84
|
-
Given a [conventional file structure](#file-structure), Zeitwerk is capable of loading your project's classes and modules on demand (autoloading) or upfront (eager loading). You don't need to write `require` calls for your own files; instead, you can streamline your programming by knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and aligns with Ruby's semantics for constants.
|
85
|
-
|
86
|
-
Zeitwerk also supports code reloading, which can be useful during web application development. However, coordination is required to reload in a thread-safe manner. The documentation below explains how to achieve this.
|
87
|
-
|
88
|
-
The gem is designed to allow any project, gem dependency, or application to have its own independent loader. Multiple loaders can coexist in the same process, each managing its own project tree and operating independently of each other. Each loader has its own configuration, inflector, and optional logger.
|
89
|
-
|
90
|
-
Internally, Zeitwerk exclusively uses absolute file names when issuing `require` calls, eliminating the need for costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk don't even need to be in `$LOAD_PATH`.
|
91
|
-
|
92
|
-
Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
|
93
|
-
|
94
|
-
<a id="markdown-synopsis" name="synopsis"></a>
|
95
|
-
## Synopsis
|
96
|
-
|
97
|
-
Main interface for gems:
|
98
|
-
|
99
|
-
```ruby
|
100
|
-
# lib/my_gem.rb (main file)
|
101
|
-
|
102
|
-
require "zeitwerk"
|
103
|
-
loader = Zeitwerk::Loader.for_gem
|
104
|
-
loader.setup # ready!
|
105
|
-
|
106
|
-
module MyGem
|
107
|
-
# ...
|
108
|
-
end
|
109
|
-
|
110
|
-
loader.eager_load # optionally
|
111
|
-
```
|
112
|
-
|
113
|
-
Main generic interface:
|
114
|
-
|
115
|
-
```ruby
|
116
|
-
loader = Zeitwerk::Loader.new
|
117
|
-
loader.push_dir(...)
|
118
|
-
loader.setup # ready!
|
119
|
-
```
|
120
|
-
|
121
|
-
The `loader` variable can go out of scope. Zeitwerk keeps a registry with all of them, and so the object won't be garbage collected.
|
122
|
-
|
123
|
-
You can reload if you want to:
|
124
|
-
|
125
|
-
```ruby
|
126
|
-
loader = Zeitwerk::Loader.new
|
127
|
-
loader.push_dir(...)
|
128
|
-
loader.enable_reloading # you need to opt-in before setup
|
129
|
-
loader.setup
|
130
|
-
...
|
131
|
-
loader.reload
|
132
|
-
```
|
133
|
-
|
134
|
-
and you can eager load all the code:
|
135
|
-
|
136
|
-
```ruby
|
137
|
-
loader.eager_load
|
138
|
-
```
|
139
|
-
|
140
|
-
It is also possible to broadcast `eager_load` to all instances:
|
141
|
-
|
142
|
-
```ruby
|
143
|
-
Zeitwerk::Loader.eager_load_all
|
144
|
-
```
|
145
|
-
|
146
|
-
<a id="markdown-file-structure" name="file-structure"></a>
|
147
|
-
## File structure
|
148
|
-
|
149
|
-
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
150
|
-
### The idea: File paths match constant paths
|
151
|
-
|
152
|
-
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
153
|
-
|
154
|
-
```
|
155
|
-
lib/my_gem.rb -> MyGem
|
156
|
-
lib/my_gem/foo.rb -> MyGem::Foo
|
157
|
-
lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
158
|
-
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
159
|
-
```
|
160
|
-
|
161
|
-
You can fine-tune this behavior by [collapsing directories](#collapsing-directories) or [ignoring specific parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
|
162
|
-
|
163
|
-
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
164
|
-
### Inner simple constants
|
165
|
-
|
166
|
-
While a simple constant like `HttpCrawler::MAX_RETRIES` can be defined in its own file:
|
167
|
-
|
168
|
-
```ruby
|
169
|
-
# http_crawler/max_retries.rb
|
170
|
-
HttpCrawler::MAX_RETRIES = 10
|
171
|
-
```
|
172
|
-
|
173
|
-
that is not required, you can also define it the regular way:
|
174
|
-
|
175
|
-
```ruby
|
176
|
-
# http_crawler.rb
|
177
|
-
class HttpCrawler
|
178
|
-
MAX_RETRIES = 10
|
179
|
-
end
|
180
|
-
```
|
181
|
-
|
182
|
-
The first example needs a custom [inflection](#inflection) rule:
|
183
|
-
|
184
|
-
```ruby
|
185
|
-
loader.inflector.inflect("max_retries" => "MAX_RETRIES")
|
186
|
-
```
|
187
|
-
|
188
|
-
Otherwise, Zeitwerk would expect the file to define `MaxRetries`.
|
189
|
-
|
190
|
-
In the second example, no custom rule is needed.
|
191
|
-
|
192
|
-
<a id="markdown-root-directories-and-root-namespaces" name="root-directories-and-root-namespaces"></a>
|
193
|
-
### Root directories and root namespaces
|
194
|
-
|
195
|
-
Every directory configured with `push_dir` is called a _root directory_, and they represent _root namespaces_.
|
196
|
-
|
197
|
-
<a id="markdown-the-default-root-namespace-is-object" name="the-default-root-namespace-is-object"></a>
|
198
|
-
#### The default root namespace is `Object`
|
199
|
-
|
200
|
-
By default, the namespace associated to a root directory is the top-level one: `Object`.
|
201
|
-
|
202
|
-
For example, given
|
203
|
-
|
204
|
-
```ruby
|
205
|
-
loader.push_dir("#{__dir__}/models")
|
206
|
-
loader.push_dir("#{__dir__}/serializers"))
|
207
|
-
```
|
208
|
-
|
209
|
-
these are the expected classes and modules being defined by these files:
|
210
|
-
|
211
|
-
```
|
212
|
-
models/user.rb -> User
|
213
|
-
serializers/user_serializer.rb -> UserSerializer
|
214
|
-
```
|
215
|
-
|
216
|
-
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
217
|
-
#### Custom root namespaces
|
218
|
-
|
219
|
-
Although `Object` is the most common root namespace, you have the flexibility to associate a different one with a specific root directory. The `push_dir` method accepts a non-anonymous class or module object as the optional `namespace` keyword argument.
|
220
|
-
|
221
|
-
For example, given:
|
222
|
-
|
223
|
-
```ruby
|
224
|
-
require "active_job"
|
225
|
-
require "active_job/queue_adapters"
|
226
|
-
loader.push_dir("#{__dir__}/adapters", namespace: ActiveJob::QueueAdapters)
|
227
|
-
```
|
228
|
-
|
229
|
-
a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the conventional parent directories, you can (and have to) store the file directly below `adapters`:
|
230
|
-
|
231
|
-
```
|
232
|
-
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
233
|
-
```
|
234
|
-
|
235
|
-
Please note that the provided root namespace must be non-reloadable, while allowing autoloaded constants within that namespace to be reloadable. This means that if you associate the `app/api` directory with an existing `Api` module, the module itself should not be reloadable. However, if the project defines and autoloads the `Api::Deliveries` class, that class can be reloaded.
|
236
|
-
|
237
|
-
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
238
|
-
#### Nested root directories
|
239
|
-
|
240
|
-
Root directories are recommended not to be nested; however, Zeitwerk provides support for nested root directories since in frameworks like Rails, both `app/models` and `app/models/concerns` belong to the autoload paths.
|
241
|
-
|
242
|
-
Zeitwerk identifies nested root directories and treats them as independent roots. In the given example, `concerns` is not considered a namespace within `app/models`. For instance, consider the following file:
|
243
|
-
|
244
|
-
```
|
245
|
-
app/models/concerns/geolocatable.rb
|
246
|
-
```
|
247
|
-
|
248
|
-
should define `Geolocatable`, not `Concerns::Geolocatable`.
|
249
|
-
|
250
|
-
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
251
|
-
### Implicit namespaces
|
252
|
-
|
253
|
-
If a namespace consists only of a simple module without any code, there is no need to explicitly define it in a separate file. Zeitwerk automatically creates modules on your behalf for directories without a corresponding Ruby file.
|
254
|
-
|
255
|
-
For instance, suppose a project includes an `admin` directory:
|
256
|
-
|
257
|
-
```
|
258
|
-
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
259
|
-
```
|
260
|
-
|
261
|
-
and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
|
262
|
-
|
263
|
-
To trigger this behavior, the directory must contain non-ignored Ruby files with the ".rb" extension, either directly or recursively. Otherwise, the directory is ignored. This condition is reevaluated during reloads.
|
264
|
-
|
265
|
-
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
266
|
-
### Explicit namespaces
|
267
|
-
|
268
|
-
Classes and modules that act as namespaces can also be explicitly defined, though. For instance, consider
|
269
|
-
|
270
|
-
```
|
271
|
-
app/models/hotel.rb -> Hotel
|
272
|
-
app/models/hotel/pricing.rb -> Hotel::Pricing
|
273
|
-
```
|
274
|
-
|
275
|
-
There, `app/models/hotel.rb` defines `Hotel`, and thus Zeitwerk does not autovivify a module.
|
276
|
-
|
277
|
-
The classes and modules from the namespace are already available in the body of the class or module defining it:
|
278
|
-
|
279
|
-
```ruby
|
280
|
-
class Hotel < ApplicationRecord
|
281
|
-
include Pricing # works
|
282
|
-
...
|
283
|
-
end
|
284
|
-
```
|
285
|
-
|
286
|
-
An explicit namespace must be managed by one single loader. Loaders that reopen namespaces owned by other projects are responsible for loading their constants before setup.
|
287
|
-
|
288
|
-
<a id="markdown-collapsing-directories" name="collapsing-directories"></a>
|
289
|
-
### Collapsing directories
|
290
|
-
|
291
|
-
Say some directories in a project exist for organizational purposes only, and you prefer not to have them as namespaces. For example, the `actions` subdirectory in the next example is not meant to represent a namespace, it is there only to group all actions related to bookings:
|
292
|
-
|
293
|
-
```
|
294
|
-
booking.rb -> Booking
|
295
|
-
booking/actions/create.rb -> Booking::Create
|
296
|
-
```
|
297
|
-
|
298
|
-
To make it work that way, configure Zeitwerk to collapse said directory:
|
299
|
-
|
300
|
-
```ruby
|
301
|
-
loader.collapse("#{__dir__}/booking/actions")
|
302
|
-
```
|
303
|
-
|
304
|
-
This method accepts an arbitrary number of strings or `Pathname` objects, and also an array of them.
|
305
|
-
|
306
|
-
You can pass directories and glob patterns. Glob patterns are expanded when they are added, and again on each reload.
|
307
|
-
|
308
|
-
To illustrate usage of glob patterns, if `actions` in the example above is part of a standardized structure, you could use a wildcard:
|
309
|
-
|
310
|
-
```ruby
|
311
|
-
loader.collapse("#{__dir__}/*/actions")
|
312
|
-
```
|
313
|
-
|
314
|
-
<a id="markdown-testing-compliance" name="testing-compliance"></a>
|
315
|
-
### Testing compliance
|
316
|
-
|
317
|
-
When a managed file is loaded, Zeitwerk verifies the expected constant is defined. If it is not, `Zeitwerk::NameError` is raised.
|
318
|
-
|
319
|
-
So, an easy way to ensure compliance in the test suite is to eager load the project:
|
320
|
-
|
321
|
-
```ruby
|
322
|
-
begin
|
323
|
-
loader.eager_load(force: true)
|
324
|
-
rescue Zeitwerk::NameError => e
|
325
|
-
flunk e.message
|
326
|
-
else
|
327
|
-
assert true
|
328
|
-
end
|
329
|
-
```
|
330
|
-
|
331
|
-
<a id="markdown-usage" name="usage"></a>
|
332
|
-
## Usage
|
333
|
-
|
334
|
-
<a id="markdown-setup" name="setup"></a>
|
335
|
-
### Setup
|
336
|
-
|
337
|
-
<a id="markdown-generic" name="generic"></a>
|
338
|
-
#### Generic
|
339
|
-
|
340
|
-
Loaders are ready to load code right after calling `setup` on them:
|
341
|
-
|
342
|
-
```ruby
|
343
|
-
loader.setup
|
344
|
-
```
|
345
|
-
|
346
|
-
This method is synchronized and idempotent.
|
347
|
-
|
348
|
-
Customization should generally be done before that call. In particular, in the generic interface you may set the root directories from which you want to load files:
|
349
|
-
|
350
|
-
```ruby
|
351
|
-
loader.push_dir(...)
|
352
|
-
loader.push_dir(...)
|
353
|
-
loader.setup
|
354
|
-
```
|
355
|
-
|
356
|
-
<a id="markdown-for_gem" name="for_gem"></a>
|
357
|
-
#### for_gem
|
358
|
-
|
359
|
-
`Zeitwerk::Loader.for_gem` is a convenience shortcut for the common case in which a gem has its entry point directly under the `lib` directory:
|
360
|
-
|
361
|
-
```
|
362
|
-
lib/my_gem.rb # MyGem
|
363
|
-
lib/my_gem/version.rb # MyGem::VERSION
|
364
|
-
lib/my_gem/foo.rb # MyGem::Foo
|
365
|
-
```
|
366
|
-
|
367
|
-
Neither a gemspec nor a version file are technically required, this helper works as long as the code is organized using that standard structure.
|
368
|
-
|
369
|
-
Conceptually, `for_gem` translates to:
|
370
|
-
|
371
|
-
```ruby
|
372
|
-
# lib/my_gem.rb
|
373
|
-
|
374
|
-
require "zeitwerk"
|
375
|
-
loader = Zeitwerk::Loader.new
|
376
|
-
loader.tag = File.basename(__FILE__, ".rb")
|
377
|
-
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
378
|
-
loader.push_dir(File.dirname(__FILE__))
|
379
|
-
```
|
380
|
-
|
381
|
-
If the main module references project constants at the top-level, Zeitwerk has to be ready to load them. Their definitions, in turn, may reference other project constants. And this is recursive. Therefore, it is important that the `setup` call happens above the main module definition:
|
382
|
-
|
383
|
-
```ruby
|
384
|
-
# lib/my_gem.rb (main file)
|
385
|
-
|
386
|
-
require "zeitwerk"
|
387
|
-
loader = Zeitwerk::Loader.for_gem
|
388
|
-
loader.setup
|
389
|
-
|
390
|
-
module MyGem
|
391
|
-
# Since the setup has been performed, at this point we are already able
|
392
|
-
# to reference project constants, in this case MyGem::MyLogger.
|
393
|
-
include MyLogger
|
394
|
-
end
|
395
|
-
```
|
396
|
-
|
397
|
-
Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`, which is the standard way to load a gem. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work.
|
398
|
-
|
399
|
-
`Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
400
|
-
|
401
|
-
If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](#reopening-third-party-namespaces) down below.
|
402
|
-
|
403
|
-
Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
|
404
|
-
|
405
|
-
For example, if the gem has Rails generators under `lib/generators`, by convention that directory defines a `Generators` Ruby module. If `generators` is just a container for non-autoloadable code and templates, not acting as a project namespace, you need to setup things accordingly.
|
406
|
-
|
407
|
-
If the warning is legit, just tell the loader to ignore the offending file or directory:
|
408
|
-
|
409
|
-
```ruby
|
410
|
-
loader.ignore("#{__dir__}/generators")
|
411
|
-
```
|
412
|
-
|
413
|
-
Otherwise, there's a flag to say the extra stuff is OK:
|
414
|
-
|
415
|
-
```ruby
|
416
|
-
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
417
|
-
```
|
418
|
-
|
419
|
-
<a id="markdown-for_gem_extension" name="for_gem_extension"></a>
|
420
|
-
#### for_gem_extension
|
421
|
-
|
422
|
-
Let's suppose you are writing a gem to extend `Net::HTTP` with some niche feature. By [convention](https://guides.rubygems.org/name-your-gem/):
|
423
|
-
|
424
|
-
* The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
|
425
|
-
* The namespace should be `Net::HTTP::NicheFeature`.
|
426
|
-
* The entry point should be `lib/net/http/niche_feature.rb`.
|
427
|
-
* Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point.
|
428
|
-
|
429
|
-
The top-level file mentioned in the last point is optional. In particular, from
|
430
|
-
|
431
|
-
```ruby
|
432
|
-
gem "net-http-niche_feature"
|
433
|
-
```
|
434
|
-
|
435
|
-
if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
|
436
|
-
|
437
|
-
Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
|
438
|
-
|
439
|
-
The structure of the gem would be like this:
|
440
|
-
|
441
|
-
```ruby
|
442
|
-
# lib/net-http-niche_feature.rb (optional)
|
443
|
-
|
444
|
-
# For technical reasons, this cannot be require_relative.
|
445
|
-
require "net/http/niche_feature"
|
446
|
-
|
447
|
-
|
448
|
-
# lib/net/http/niche_feature.rb
|
449
|
-
|
450
|
-
require "net/http"
|
451
|
-
require "zeitwerk"
|
452
|
-
|
453
|
-
loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
|
454
|
-
loader.setup
|
455
|
-
|
456
|
-
module Net::HTTP::NicheFeature
|
457
|
-
# Since the setup has been performed, at this point we are already able
|
458
|
-
# to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
|
459
|
-
include MyMixin
|
460
|
-
end
|
461
|
-
|
462
|
-
|
463
|
-
# lib/net/http/niche_feature/version.rb
|
464
|
-
|
465
|
-
module Net::HTTP::NicheFeature
|
466
|
-
VERSION = "1.0.0"
|
467
|
-
end
|
468
|
-
```
|
469
|
-
|
470
|
-
`Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
|
471
|
-
|
472
|
-
If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
|
473
|
-
|
474
|
-
Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. This is important if you load the entry point from the optional hyphenated top-level file.
|
475
|
-
|
476
|
-
`Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
477
|
-
|
478
|
-
<a id="markdown-autoloading" name="autoloading"></a>
|
479
|
-
### Autoloading
|
480
|
-
|
481
|
-
After `setup`, you are able to reference classes and modules from the project without issuing `require` calls for them. They are all available everywhere, autoloading loads them on demand. This works even if the reference to the class or module is first hit in client code, outside your project.
|
482
|
-
|
483
|
-
Let's revisit the example above:
|
484
|
-
|
485
|
-
```ruby
|
486
|
-
# lib/my_gem.rb (main file)
|
487
|
-
|
488
|
-
require "zeitwerk"
|
489
|
-
loader = Zeitwerk::Loader.for_gem
|
490
|
-
loader.setup
|
491
|
-
|
492
|
-
module MyGem
|
493
|
-
include MyLogger # (*)
|
494
|
-
end
|
495
|
-
```
|
496
|
-
|
497
|
-
That works, and there is no `require "my_gem/my_logger"`. When `(*)` is reached, Zeitwerk seamlessly autoloads `MyGem::MyLogger`.
|
498
|
-
|
499
|
-
If autoloading a file does not define the expected class or module, Zeitwerk raises `Zeitwerk::NameError`, which is a subclass of `NameError`.
|
500
|
-
|
501
|
-
<a id="markdown-eager-loading" name="eager-loading"></a>
|
502
|
-
### Eager loading
|
503
|
-
|
504
|
-
Zeitwerk instances are able to eager load their managed files:
|
505
|
-
|
506
|
-
```ruby
|
507
|
-
loader.eager_load
|
508
|
-
```
|
509
|
-
|
510
|
-
That skips [ignored files and directories](#ignoring-parts-of-the-project).
|
511
|
-
|
512
|
-
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
|
513
|
-
|
514
|
-
Eager loading is synchronized and idempotent.
|
515
|
-
|
516
|
-
Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
517
|
-
|
518
|
-
<a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
|
519
|
-
#### Eager load exclusions
|
520
|
-
|
521
|
-
You can tell Zeitwerk that certain files or directories are autoloadable, but should not be eager loaded:
|
522
|
-
|
523
|
-
```ruby
|
524
|
-
db_adapters = "#{__dir__}/my_gem/db_adapters"
|
525
|
-
loader.do_not_eager_load(db_adapters)
|
526
|
-
loader.setup
|
527
|
-
loader.eager_load # won't eager load the database adapters
|
528
|
-
```
|
529
|
-
|
530
|
-
However, that can be overridden with `force`:
|
531
|
-
|
532
|
-
```ruby
|
533
|
-
loader.eager_load(force: true) # database adapters are eager loaded
|
534
|
-
```
|
535
|
-
|
536
|
-
Which may be handy if the project eager loads in the test suite to [ensure project layout compliance](#testing-compliance).
|
537
|
-
|
538
|
-
The `force` flag does not affect ignored files and directories, those are still ignored.
|
539
|
-
|
540
|
-
<a id="markdown-eager-load-directories" name="eager-load-directories"></a>
|
541
|
-
#### Eager load directories
|
542
|
-
|
543
|
-
The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
|
544
|
-
|
545
|
-
```ruby
|
546
|
-
loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
|
547
|
-
```
|
548
|
-
|
549
|
-
This is useful when the loader is not eager loading the entire project, but you still need some subtree to be loaded for things to function properly.
|
550
|
-
|
551
|
-
Both strings and `Pathname` objects are supported as arguments. If the argument is not a directory managed by the receiver, the method raises `Zeitwerk::Error`.
|
552
|
-
|
553
|
-
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
554
|
-
|
555
|
-
`Zeitwerk::Loader#eager_load_dir` is idempotent, but compatible with reloading. If you eager load a directory and then reload, eager loading that directory will load its (current) contents again.
|
556
|
-
|
557
|
-
The method checks if a regular eager load was already executed, in which case it returns fast.
|
558
|
-
|
559
|
-
Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
|
560
|
-
|
561
|
-
Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
562
|
-
|
563
|
-
<a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
|
564
|
-
#### Eager load namespaces
|
565
|
-
|
566
|
-
The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
|
567
|
-
|
568
|
-
```ruby
|
569
|
-
loader.eager_load_namespace(MyApp::Routes)
|
570
|
-
```
|
571
|
-
|
572
|
-
This is useful when the loader is not eager loading the entire project, but you still need some namespace to be loaded for things to function properly.
|
573
|
-
|
574
|
-
The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
|
575
|
-
|
576
|
-
If the namespace is spread over multiple directories in the receiver's source tree, they are all eager loaded. For example, if you have a structure like
|
577
|
-
|
578
|
-
```
|
579
|
-
root_dir1/my_app/routes
|
580
|
-
root_dir2/my_app/routes
|
581
|
-
root_dir3/my_app/routes
|
582
|
-
```
|
583
|
-
|
584
|
-
where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
585
|
-
|
586
|
-
There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](#reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
|
587
|
-
|
588
|
-
This method is flexible about what it accepts. Its semantics have to be interpreted as: "_If_ you manage this namespace, or part of this namespace, please eager load what you got". In particular, if the receiver does not manage the namespace, it will simply do nothing, this is not an error condition.
|
589
|
-
|
590
|
-
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
591
|
-
|
592
|
-
`Zeitwerk::Loader#eager_load_namespace` is idempotent, but compatible with reloading. If you eager load a namespace and then reload, eager loading that namespace will load its (current) descendants again.
|
593
|
-
|
594
|
-
The method checks if a regular eager load was already executed, in which case it returns fast.
|
595
|
-
|
596
|
-
If root directories are assigned to custom namespaces, the method behaves as you'd expect, according to the namespacing relationship between the custom namespace and the argument.
|
597
|
-
|
598
|
-
Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
599
|
-
|
600
|
-
<a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
|
601
|
-
#### Eager load namespaces shared by several loaders
|
602
|
-
|
603
|
-
The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
|
604
|
-
|
605
|
-
```ruby
|
606
|
-
Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
|
607
|
-
```
|
608
|
-
|
609
|
-
This may be handy, for example, if a framework supports plugins and a shared namespace needs to be eager loaded for the framework to function properly.
|
610
|
-
|
611
|
-
Please, note that loaders only eager load namespaces they manage, as documented above. Therefore, this method does not allow you to eager load namespaces not managed by Zeitwerk loaders.
|
612
|
-
|
613
|
-
This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped.
|
614
|
-
|
615
|
-
<a id="markdown-global-eager-load" name="global-eager-load"></a>
|
616
|
-
#### Global eager load
|
617
|
-
|
618
|
-
If you want to eager load yourself and all dependencies that use Zeitwerk, you can broadcast the `eager_load` call to all instances:
|
619
|
-
|
620
|
-
```ruby
|
621
|
-
Zeitwerk::Loader.eager_load_all
|
622
|
-
```
|
623
|
-
|
624
|
-
This may be handy in top-level services, like web applications.
|
625
|
-
|
626
|
-
Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager load twice if any of the instances already eager loaded.
|
627
|
-
|
628
|
-
This method does not accept the `force` flag, since in general it wouldn't be a good idea to force eager loading in 3rd party code.
|
629
|
-
|
630
|
-
This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped.
|
631
|
-
|
632
|
-
<a id="markdown-loading-individual-files" name="loading-individual-files"></a>
|
633
|
-
### Loading individual files
|
634
|
-
|
635
|
-
The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
|
636
|
-
|
637
|
-
```ruby
|
638
|
-
loader.load_file("#{__dir__}/custom_web_app/routes.rb")
|
639
|
-
```
|
640
|
-
|
641
|
-
This is useful when the loader is not eager loading the entire project, but you still need an individual file to be loaded for things to function properly.
|
642
|
-
|
643
|
-
Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](#shadowed-files), or is not managed by the receiver.
|
644
|
-
|
645
|
-
`Zeitwerk::Loader#load_file` is idempotent, but compatible with reloading. If you load a file and then reload, a new call will load its (current) contents again.
|
646
|
-
|
647
|
-
If you want to eager load a directory, `Zeitwerk::Loader#eager_load_dir` is more efficient than invoking `Zeitwerk::Loader#load_file` on its files.
|
648
|
-
|
649
|
-
<a id="markdown-reloading" name="reloading"></a>
|
650
|
-
### Reloading
|
651
|
-
|
652
|
-
<a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
|
653
|
-
#### Configuration and usage
|
654
|
-
|
655
|
-
Zeitwerk is able to reload code, but you need to enable this feature:
|
656
|
-
|
657
|
-
```ruby
|
658
|
-
loader = Zeitwerk::Loader.new
|
659
|
-
loader.push_dir(...)
|
660
|
-
loader.enable_reloading # you need to opt-in before setup
|
661
|
-
loader.setup
|
662
|
-
...
|
663
|
-
loader.reload
|
664
|
-
```
|
665
|
-
|
666
|
-
There is no way to undo this, either you want to reload or you don't.
|
667
|
-
|
668
|
-
Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`. Attempting to reload without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
669
|
-
|
670
|
-
Generally speaking, reloading is useful while developing running services like web applications. Gems that implement regular libraries, so to speak, or services running in testing or production environments, won't normally have a use case for reloading. If reloading is not enabled, Zeitwerk is able to use less memory.
|
671
|
-
|
672
|
-
Reloading removes the currently loaded classes and modules and resets the loader so that it will pick whatever is in the file system now.
|
673
|
-
|
674
|
-
It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent.
|
675
|
-
|
676
|
-
<a id="markdown-thread-safety" name="thread-safety"></a>
|
677
|
-
#### Thread-safety
|
678
|
-
|
679
|
-
In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
|
680
|
-
|
681
|
-
For example, a web framework that serves each request in its own thread and has reloading enabled could create a read-write lock on boot like this:
|
682
|
-
|
683
|
-
```ruby
|
684
|
-
require "concurrent/atomic/read_write_lock"
|
685
|
-
|
686
|
-
MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
|
687
|
-
```
|
688
|
-
|
689
|
-
You acquire the lock for reading for serving each individual request:
|
690
|
-
|
691
|
-
```ruby
|
692
|
-
MyFramework::RELOAD_RW_LOCK.with_read_lock do
|
693
|
-
serve(request)
|
694
|
-
end
|
695
|
-
```
|
696
|
-
|
697
|
-
Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
|
698
|
-
|
699
|
-
```ruby
|
700
|
-
MyFramework::RELOAD_RW_LOCK.with_write_lock do
|
701
|
-
loader.reload
|
702
|
-
end
|
703
|
-
```
|
704
|
-
|
705
|
-
On reloading, client code has to update anything that would otherwise be storing a stale object. For example, if the routing layer of a web framework stores reloadable controller class objects or instances in internal structures, on reload it has to refresh them somehow, possibly reevaluating routes.
|
706
|
-
|
707
|
-
<a id="markdown-inflection" name="inflection"></a>
|
708
|
-
### Inflection
|
709
|
-
|
710
|
-
Each individual loader needs an inflector to figure out which constant path would a given file or directory map to. Zeitwerk ships with two basic inflectors, and you can define your own.
|
711
|
-
|
712
|
-
<a id="markdown-zeitwerkinflector" name="zeitwerkinflector"></a>
|
713
|
-
#### Zeitwerk::Inflector
|
714
|
-
|
715
|
-
Each loader instantiated with `Zeitwerk::Loader.new` has an inflector of this type by default.
|
716
|
-
|
717
|
-
This is a very basic inflector that converts snake case to camel case:
|
718
|
-
|
719
|
-
```
|
720
|
-
user -> User
|
721
|
-
users_controller -> UsersController
|
722
|
-
html_parser -> HtmlParser
|
723
|
-
```
|
724
|
-
|
725
|
-
The camelize logic can be overridden easily for individual basenames:
|
726
|
-
|
727
|
-
```ruby
|
728
|
-
loader.inflector.inflect(
|
729
|
-
"html_parser" => "HTMLParser",
|
730
|
-
"mysql_adapter" => "MySQLAdapter"
|
731
|
-
)
|
732
|
-
```
|
733
|
-
|
734
|
-
The `inflect` method can be invoked several times if you prefer this other style:
|
735
|
-
|
736
|
-
```ruby
|
737
|
-
loader.inflector.inflect "html_parser" => "HTMLParser"
|
738
|
-
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
739
|
-
```
|
740
|
-
|
741
|
-
Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
|
742
|
-
|
743
|
-
```ruby
|
744
|
-
loader.inflector.inflect("xml" => "XML")
|
745
|
-
```
|
746
|
-
|
747
|
-
then the following constants are expected:
|
748
|
-
|
749
|
-
```
|
750
|
-
xml.rb -> XML
|
751
|
-
foo/xml -> Foo::XML
|
752
|
-
foo/bar/xml.rb -> Foo::Bar::XML
|
753
|
-
```
|
754
|
-
|
755
|
-
As you see, any directory whose basename is exactly `xml`, and any file whose basename is exactly `xml.rb` are expected to define the constant `XML` in the corresponding namespace. On the other hand, partial matches are ignored. For example, `xml_parser.rb` would be inflected as `XmlParser` because `xml_parser` is not equal to `xml`. You'd need an additional override:
|
756
|
-
|
757
|
-
```ruby
|
758
|
-
loader.inflector.inflect(
|
759
|
-
"xml" => "XML",
|
760
|
-
"xml_parser" => "XMLParser"
|
761
|
-
)
|
762
|
-
```
|
763
|
-
|
764
|
-
If you need more flexibility, you can define a custom inflector, as explained down below.
|
765
|
-
|
766
|
-
Overrides need to be configured before calling `setup`.
|
767
|
-
|
768
|
-
The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
|
769
|
-
|
770
|
-
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
771
|
-
#### Zeitwerk::GemInflector
|
772
|
-
|
773
|
-
Each loader instantiated with `Zeitwerk::Loader.for_gem` has an inflector of this type by default.
|
774
|
-
|
775
|
-
This inflector is like the basic one, except it expects `lib/my_gem/version.rb` to define `MyGem::VERSION`.
|
776
|
-
|
777
|
-
The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
|
778
|
-
|
779
|
-
<a id="markdown-zeitwerknullinflector" name="zeitwerknullinflector"></a>
|
780
|
-
#### Zeitwerk::NullInflector
|
781
|
-
|
782
|
-
This is an experimental inflector that simply returns its input unchanged.
|
783
|
-
|
784
|
-
```ruby
|
785
|
-
loader.inflector = Zeitwerk::NullInflector.new
|
786
|
-
```
|
787
|
-
|
788
|
-
In a project using this inflector, the names of files and directories are equal to the constants they define:
|
789
|
-
|
790
|
-
```
|
791
|
-
User.rb -> User
|
792
|
-
HTMLParser.rb -> HTMLParser
|
793
|
-
Admin/Role.rb -> Admin::Role
|
794
|
-
```
|
795
|
-
|
796
|
-
Point is, you think less. Names that typically need custom configuration like acronyms no longer require your attention. What you see is what you get, simple.
|
797
|
-
|
798
|
-
This inflector is experimental since Ruby usually goes for snake case in files and directories. But hey, if you fancy giving it a whirl, go for it!
|
799
|
-
|
800
|
-
The null inflector cannot be used in Rails applications because the `main` autoloader also manages engines. However, you could subclass the default inflector and override `camelize` to return the basename untouched if it starts with an uppercase letter. Generators would not create the expected file names, but you could still experiment to see how far this approach takes you.
|
801
|
-
|
802
|
-
In case-insensitive file systems, this inflector works as long as directory listings return the expected strings. Zeitwerk lists directories using Ruby APIs like `Dir.children` or `Dir.entries`.
|
803
|
-
|
804
|
-
<a id="markdown-custom-inflector" name="custom-inflector"></a>
|
805
|
-
#### Custom inflector
|
806
|
-
|
807
|
-
The inflectors that ship with Zeitwerk are deterministic and simple. But you can configure your own:
|
808
|
-
|
809
|
-
```ruby
|
810
|
-
# frozen_string_literal: true
|
811
|
-
|
812
|
-
class MyInflector < Zeitwerk::Inflector
|
813
|
-
def camelize(basename, abspath)
|
814
|
-
if basename =~ /\Ahtml_(.*)/
|
815
|
-
"HTML" + super($1, abspath)
|
816
|
-
else
|
817
|
-
super
|
818
|
-
end
|
819
|
-
end
|
820
|
-
end
|
821
|
-
```
|
822
|
-
|
823
|
-
The first argument, `basename`, is a string with the basename of the file or directory to be inflected. In the case of a file, without extension. In the case of a directory, without trailing slash. The inflector needs to return this basename inflected. Therefore, a simple constant name without colons.
|
824
|
-
|
825
|
-
The second argument, `abspath`, is a string with the absolute path to the file or directory in case you need it to decide how to inflect the basename. Paths to directories don't have trailing slashes.
|
826
|
-
|
827
|
-
Then, assign the inflector:
|
828
|
-
|
829
|
-
```ruby
|
830
|
-
loader.inflector = MyInflector.new
|
831
|
-
```
|
832
|
-
|
833
|
-
This needs to be done before calling `setup`.
|
834
|
-
|
835
|
-
If a custom inflector definition in a gem takes too much space in the main file, you can extract it. For example, this is a simple pattern:
|
836
|
-
|
837
|
-
```ruby
|
838
|
-
# lib/my_gem/inflector.rb
|
839
|
-
module MyGem
|
840
|
-
class Inflector < Zeitwerk::GemInflector
|
841
|
-
...
|
842
|
-
end
|
843
|
-
end
|
844
|
-
|
845
|
-
# lib/my_gem.rb
|
846
|
-
require "zeitwerk"
|
847
|
-
require_relative "my_gem/inflector"
|
848
|
-
|
849
|
-
loader = Zeitwerk::Loader.for_gem
|
850
|
-
loader.inflector = MyGem::Inflector.new(__FILE__)
|
851
|
-
loader.setup
|
852
|
-
|
853
|
-
module MyGem
|
854
|
-
# ...
|
855
|
-
end
|
856
|
-
```
|
857
|
-
|
858
|
-
Since `MyGem` is referenced before the namespace is defined in the main file, it is important to use this style:
|
859
|
-
|
860
|
-
```ruby
|
861
|
-
# Correct, effectively defines MyGem.
|
862
|
-
module MyGem
|
863
|
-
class Inflector < Zeitwerk::GemInflector
|
864
|
-
# ...
|
865
|
-
end
|
866
|
-
end
|
867
|
-
```
|
868
|
-
|
869
|
-
instead of:
|
870
|
-
|
871
|
-
```ruby
|
872
|
-
# Raises uninitialized constant MyGem (NameError).
|
873
|
-
class MyGem::Inflector < Zeitwerk::GemInflector
|
874
|
-
# ...
|
875
|
-
end
|
876
|
-
```
|
877
|
-
|
878
|
-
<a id="markdown-callbacks" name="callbacks"></a>
|
879
|
-
### Callbacks
|
880
|
-
|
881
|
-
<a id="markdown-the-on_setup-callback" name="the-on_setup-callback"></a>
|
882
|
-
#### The on_setup callback
|
883
|
-
|
884
|
-
The `on_setup` callback is fired on setup and on each reload:
|
885
|
-
|
886
|
-
```ruby
|
887
|
-
loader.on_setup do
|
888
|
-
# Ready to autoload here.
|
889
|
-
end
|
890
|
-
```
|
891
|
-
|
892
|
-
Multiple `on_setup` callbacks are supported, and they run in order of definition.
|
893
|
-
|
894
|
-
If `setup` was already executed, the callback is fired immediately.
|
895
|
-
|
896
|
-
<a id="markdown-the-on_load-callback" name="the-on_load-callback"></a>
|
897
|
-
#### The on_load callback
|
898
|
-
|
899
|
-
The usual place to run something when a file is loaded is the file itself. However, sometimes you'd like to be called, and this is possible with the `on_load` callback.
|
900
|
-
|
901
|
-
For example, let's imagine this class belongs to a Rails application:
|
902
|
-
|
903
|
-
```ruby
|
904
|
-
class SomeApiClient
|
905
|
-
class << self
|
906
|
-
attr_accessor :endpoint
|
907
|
-
end
|
908
|
-
end
|
909
|
-
```
|
910
|
-
|
911
|
-
With `on_load`, it is easy to schedule code at boot time that initializes `endpoint` according to the configuration:
|
912
|
-
|
913
|
-
```ruby
|
914
|
-
# config/environments/development.rb
|
915
|
-
loader.on_load("SomeApiClient") do |klass, _abspath|
|
916
|
-
klass.endpoint = "https://api.dev"
|
917
|
-
end
|
918
|
-
|
919
|
-
# config/environments/production.rb
|
920
|
-
loader.on_load("SomeApiClient") do |klass, _abspath|
|
921
|
-
klass.endpoint = "https://api.prod"
|
922
|
-
end
|
923
|
-
```
|
924
|
-
|
925
|
-
Some uses cases:
|
926
|
-
|
927
|
-
* Doing something with a reloadable class or module in a Rails application during initialization, in a way that plays well with reloading. As in the previous example.
|
928
|
-
* Delaying the execution of the block until the class is loaded for performance.
|
929
|
-
* Delaying the execution of the block until the class is loaded because it follows the adapter pattern and better not to load the class if the user does not need it.
|
930
|
-
|
931
|
-
`on_load` gets a target constant path as a string (e.g., "User", or "Service::NotificationsGateway"). When fired, its block receives the stored value, and the absolute path to the corresponding file or directory as a string. The callback is executed every time the target is loaded. That includes reloads.
|
932
|
-
|
933
|
-
Multiple callbacks on the same target are supported, and they run in order of definition.
|
934
|
-
|
935
|
-
The block is executed once the loader has loaded the target. In particular, if the target was already loaded when the callback is defined, the block won't run. But if you reload and load the target again, then it will. Normally, you'll want to define `on_load` callbacks before `setup`.
|
936
|
-
|
937
|
-
Defining a callback for a target not managed by the receiver is not an error, the block simply won't ever be executed.
|
938
|
-
|
939
|
-
It is also possible to be called when any constant managed by the loader is loaded:
|
940
|
-
|
941
|
-
```ruby
|
942
|
-
loader.on_load do |cpath, value, abspath|
|
943
|
-
# ...
|
944
|
-
end
|
945
|
-
```
|
946
|
-
|
947
|
-
The block gets the constant path as a string (e.g., "User", or "Foo::VERSION"), the value it stores (e.g., the class object stored in `User`, or "2.5.0"), and the absolute path to the corresponding file or directory as a string.
|
948
|
-
|
949
|
-
Multiple callbacks like these are supported, and they run in order of definition.
|
950
|
-
|
951
|
-
There are use cases for this last catch-all callback, but they are rare. If you just need to understand how things are being loaded for debugging purposes, please remember that `Zeitwerk::Loader#log!` logs plenty of information.
|
952
|
-
|
953
|
-
If both types of callbacks are defined, the specific ones run first.
|
954
|
-
|
955
|
-
Since `on_load` callbacks are executed right after files are loaded, even if the loading context seems to be far away, in practice **the block is subject to [circular dependencies](#beware-of-circular-dependencies)**. As a rule of thumb, as far as loading order and its interdependencies is concerned, you have to program as if the block was executed at the bottom of the file just loaded.
|
956
|
-
|
957
|
-
<a id="markdown-the-on_unload-callback" name="the-on_unload-callback"></a>
|
958
|
-
#### The on_unload callback
|
959
|
-
|
960
|
-
When reloading is enabled, you may occasionally need to execute something before a certain autoloaded class or module is unloaded. The `on_unload` callback allows you to do that.
|
961
|
-
|
962
|
-
For example, let's imagine that a `Country` class fetches a list of countries and caches them when it is loaded. You might want to clear that cache if unloaded:
|
963
|
-
|
964
|
-
```ruby
|
965
|
-
loader.on_unload("Country") do |klass, _abspath|
|
966
|
-
klass.clear_cache
|
967
|
-
end
|
968
|
-
```
|
969
|
-
|
970
|
-
`on_unload` gets a target constant path as a string (e.g., "User", or "Service::NotificationsGateway"). When fired, its block receives the stored value, and the absolute path to the corresponding file or directory as a string. The callback is executed every time the target is unloaded.
|
971
|
-
|
972
|
-
`on_unload` blocks are executed before the class is unloaded, but in the middle of unloading, which happens in an unspecified order. Therefore, **that callback should not refer to any reloadable constant because there is no guarantee the constant works there**. Those blocks should rely on objects only, as in the example above, or regular constants not managed by the loader. This remark is transitive, applies to any methods invoked within the block.
|
973
|
-
|
974
|
-
Multiple callbacks on the same target are supported, and they run in order of definition.
|
975
|
-
|
976
|
-
Defining a callback for a target not managed by the receiver is not an error, the block simply won't ever be executed.
|
977
|
-
|
978
|
-
It is also possible to be called when any constant managed by the loader is unloaded:
|
979
|
-
|
980
|
-
```ruby
|
981
|
-
loader.on_unload do |cpath, value, abspath|
|
982
|
-
# ...
|
983
|
-
end
|
984
|
-
```
|
985
|
-
|
986
|
-
The block gets the constant path as a string (e.g., "User", or "Foo::VERSION"), the value it stores (e.g., the class object stored in `User`, or "2.5.0"), and the absolute path to the corresponding file or directory as a string.
|
987
|
-
|
988
|
-
Multiple callbacks like these are supported, and they run in order of definition.
|
989
|
-
|
990
|
-
If both types of callbacks are defined, the specific ones run first.
|
991
|
-
|
992
|
-
<a id="markdown-technical-details" name="technical-details"></a>
|
993
|
-
#### Technical details
|
994
|
-
|
995
|
-
Zeitwerk uses the word "unload" to ease communication and for symmetry with `on_load`. However, in Ruby you cannot unload things for real. So, when does `on_unload` technically happen?
|
996
|
-
|
997
|
-
When unloading, Zeitwerk issues `Module#remove_const` calls. Classes and modules are no longer reachable through their constants, and `on_unload` callbacks are executed right before those calls.
|
998
|
-
|
999
|
-
Technically, though, the objects themselves are still alive, but if everything is used as expected and they are not stored in any non-reloadable place (don't do that), they are ready for garbage collection, which is when the real unloading happens.
|
1000
|
-
|
1001
|
-
<a id="markdown-logging" name="logging"></a>
|
1002
|
-
### Logging
|
1003
|
-
|
1004
|
-
Zeitwerk is silent by default, but you can ask loaders to trace their activity. Logging is meant just for troubleshooting, shouldn't normally be enabled.
|
1005
|
-
|
1006
|
-
The `log!` method is a quick shortcut to let the loader log to `$stdout`:
|
1007
|
-
|
1008
|
-
```
|
1009
|
-
loader.log!
|
1010
|
-
```
|
1011
|
-
|
1012
|
-
If you want more control, a logger can be configured as a callable
|
1013
|
-
|
1014
|
-
```ruby
|
1015
|
-
loader.logger = method(:puts)
|
1016
|
-
loader.logger = ->(msg) { ... }
|
1017
|
-
```
|
1018
|
-
|
1019
|
-
as well as anything that responds to `debug`:
|
1020
|
-
|
1021
|
-
```ruby
|
1022
|
-
loader.logger = Logger.new($stderr)
|
1023
|
-
loader.logger = Rails.logger
|
1024
|
-
```
|
1025
|
-
|
1026
|
-
In both cases, the corresponding methods are going to be passed exactly one argument with the message to be logged.
|
1027
|
-
|
1028
|
-
It is also possible to set a global default this way:
|
1029
|
-
|
1030
|
-
```ruby
|
1031
|
-
Zeitwerk::Loader.default_logger = method(:puts)
|
1032
|
-
```
|
1033
|
-
|
1034
|
-
If there is a logger configured, you'll see traces when autoloads are set, files loaded, and modules autovivified. While reloading, removed autoloads and unloaded objects are also traced.
|
1035
|
-
|
1036
|
-
As a curiosity, if your project has namespaces you'll notice in the traces Zeitwerk sets autoloads for _directories_. This allows descending into subdirectories on demand, thus avoiding unnecessary tree walks.
|
1037
|
-
|
1038
|
-
<a id="markdown-loader-tag" name="loader-tag"></a>
|
1039
|
-
#### Loader tag
|
1040
|
-
|
1041
|
-
Loaders have a tag that is printed in traces in order to be able to distinguish them in globally logged activity:
|
1042
|
-
|
1043
|
-
```
|
1044
|
-
Zeitwerk@9fa54b: autoload set for User, to be loaded from ...
|
1045
|
-
```
|
1046
|
-
|
1047
|
-
By default, a random tag like the one above is assigned, but you can change it:
|
1048
|
-
|
1049
|
-
```
|
1050
|
-
loader.tag = "grep_me"
|
1051
|
-
```
|
1052
|
-
|
1053
|
-
The tag of a loader returned by `for_gem` is the basename of the root file without extension:
|
1054
|
-
|
1055
|
-
```
|
1056
|
-
Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
|
1057
|
-
```
|
1058
|
-
|
1059
|
-
<a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
|
1060
|
-
### Ignoring parts of the project
|
1061
|
-
|
1062
|
-
Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have the extension ".rb".
|
1063
|
-
|
1064
|
-
However, sometimes it might still be convenient to tell Zeitwerk to completely ignore some particular Ruby file or directory. That is possible with `ignore`, which accepts an arbitrary number of strings or `Pathname` objects, and also an array of them.
|
1065
|
-
|
1066
|
-
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
1067
|
-
|
1068
|
-
There is an edge case related to nested root directories. Conceptually, root directories are independent source trees. If you ignore a parent of a nested root directory, the nested root directory is not affected. You need to ignore it explicitly if you want it ignored too.
|
1069
|
-
|
1070
|
-
Let's see some use cases.
|
1071
|
-
|
1072
|
-
<a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
|
1073
|
-
#### Use case: Files that do not follow the conventions
|
1074
|
-
|
1075
|
-
Let's suppose that your gem decorates something in `Kernel`:
|
1076
|
-
|
1077
|
-
```ruby
|
1078
|
-
# lib/my_gem/core_ext/kernel.rb
|
1079
|
-
|
1080
|
-
Kernel.module_eval do
|
1081
|
-
# ...
|
1082
|
-
end
|
1083
|
-
```
|
1084
|
-
|
1085
|
-
`Kernel` is already defined by Ruby so the module cannot be autoloaded. Also, that file does not define a constant path after the path name. Therefore, Zeitwerk should not process it at all.
|
1086
|
-
|
1087
|
-
The extension can still coexist with the rest of the project, you only need to tell Zeitwerk to ignore it:
|
1088
|
-
|
1089
|
-
```ruby
|
1090
|
-
kernel_ext = "#{__dir__}/my_gem/core_ext/kernel.rb"
|
1091
|
-
loader.ignore(kernel_ext)
|
1092
|
-
loader.setup
|
1093
|
-
```
|
1094
|
-
|
1095
|
-
You can also ignore the whole directory:
|
1096
|
-
|
1097
|
-
```ruby
|
1098
|
-
core_ext = "#{__dir__}/my_gem/core_ext"
|
1099
|
-
loader.ignore(core_ext)
|
1100
|
-
loader.setup
|
1101
|
-
```
|
1102
|
-
|
1103
|
-
Now, that file has to be loaded manually with `require` or `require_relative`:
|
1104
|
-
|
1105
|
-
```ruby
|
1106
|
-
require_relative "my_gem/core_ext/kernel"
|
1107
|
-
```
|
1108
|
-
|
1109
|
-
and you can do that anytime, before configuring the loader, or after configuring the loader, does not matter.
|
1110
|
-
|
1111
|
-
<a id="markdown-use-case-the-adapter-pattern" name="use-case-the-adapter-pattern"></a>
|
1112
|
-
#### Use case: The adapter pattern
|
1113
|
-
|
1114
|
-
Another use case for ignoring files is the adapter pattern.
|
1115
|
-
|
1116
|
-
Let's imagine your project talks to databases, supports several, and has adapters for each one of them. Those adapters may have top-level `require` calls that load their respective drivers:
|
1117
|
-
|
1118
|
-
```ruby
|
1119
|
-
# my_gem/db_adapters/postgresql.rb
|
1120
|
-
require "pg"
|
1121
|
-
```
|
1122
|
-
|
1123
|
-
but you don't want your users to install them all, only the one they are going to use.
|
1124
|
-
|
1125
|
-
On the other hand, if your code is eager loaded by you or a parent project (with `Zeitwerk::Loader.eager_load_all`), those `require` calls are going to be executed. Ignoring the adapters prevents that:
|
1126
|
-
|
1127
|
-
```ruby
|
1128
|
-
db_adapters = "#{__dir__}/my_gem/db_adapters"
|
1129
|
-
loader.ignore(db_adapters)
|
1130
|
-
loader.setup
|
1131
|
-
```
|
1132
|
-
|
1133
|
-
The chosen adapter, then, has to be loaded by hand somehow:
|
1134
|
-
|
1135
|
-
```ruby
|
1136
|
-
require "my_gem/db_adapters/#{config[:db_adapter]}"
|
1137
|
-
```
|
1138
|
-
|
1139
|
-
Note that since the directory is ignored, the required adapter can instantiate another loader to manage its subtree, if desired. Such loader would coexist with the main one just fine.
|
1140
|
-
|
1141
|
-
<a id="markdown-use-case-test-files-mixed-with-implementation-files" name="use-case-test-files-mixed-with-implementation-files"></a>
|
1142
|
-
#### Use case: Test files mixed with implementation files
|
1143
|
-
|
1144
|
-
There are project layouts that put implementation files and test files together. To ignore the test files, you can use a glob pattern like this:
|
1145
|
-
|
1146
|
-
```ruby
|
1147
|
-
tests = "#{__dir__}/**/*_test.rb"
|
1148
|
-
loader.ignore(tests)
|
1149
|
-
loader.setup
|
1150
|
-
```
|
1151
|
-
|
1152
|
-
<a id="markdown-shadowed-files" name="shadowed-files"></a>
|
1153
|
-
### Shadowed files
|
1154
|
-
|
1155
|
-
In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
|
1156
|
-
|
1157
|
-
```ruby
|
1158
|
-
require "foo"
|
1159
|
-
```
|
1160
|
-
|
1161
|
-
the first one found gets loaded, and the rest are ignored.
|
1162
|
-
|
1163
|
-
Zeitwerk behaves in a similar way. If `foo.rb` is present in several root directories (at the same namespace level), the constant `Foo` is autoloaded from the first one, and the rest of the files are not evaluated. If logging is enabled, you'll see something like
|
1164
|
-
|
1165
|
-
```
|
1166
|
-
file #{file} is ignored because #{previous_occurrence} has precedence
|
1167
|
-
```
|
1168
|
-
|
1169
|
-
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1170
|
-
|
1171
|
-
Even if there's only one `foo.rb`, if the constant `Foo` is already defined when Zeitwerk finds `foo.rb`, then the file is ignored too. This could happen if `Foo` was defined by a dependency, for example. If logging is enabled, you'll see something like
|
1172
|
-
|
1173
|
-
```
|
1174
|
-
file #{file} is ignored because #{constant_path} is already defined
|
1175
|
-
```
|
1176
|
-
|
1177
|
-
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1178
|
-
|
1179
|
-
Shadowing only applies to Ruby files, namespace definition can be spread over multiple directories. And you can also reopen third-party namespaces if done [orderly](#reopening-third-party-namespaces).
|
1180
|
-
|
1181
|
-
<a id="markdown-edge-cases" name="edge-cases"></a>
|
1182
|
-
### Edge cases
|
1183
|
-
|
1184
|
-
[Explicit namespaces](#explicit-namespaces) like `Trip` here:
|
1185
|
-
|
1186
|
-
```ruby
|
1187
|
-
# trip.rb
|
1188
|
-
class Trip
|
1189
|
-
include Geolocation
|
1190
|
-
end
|
1191
|
-
|
1192
|
-
# trip/geolocation.rb
|
1193
|
-
module Trip::Geolocation
|
1194
|
-
...
|
1195
|
-
end
|
1196
|
-
```
|
1197
|
-
|
1198
|
-
have to be defined with the `class`/`module` keywords, as in the example above.
|
1199
|
-
|
1200
|
-
For technical reasons, raw constant assignment is not supported:
|
1201
|
-
|
1202
|
-
```ruby
|
1203
|
-
# trip.rb
|
1204
|
-
Trip = Class { ...} # NOT SUPPORTED
|
1205
|
-
Trip = Struct.new { ... } # NOT SUPPORTED
|
1206
|
-
Trip = Data.define { ... } # NOT SUPPORTED
|
1207
|
-
```
|
1208
|
-
|
1209
|
-
This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
|
1210
|
-
|
1211
|
-
<a id="markdown-beware-of-circular-dependencies" name="beware-of-circular-dependencies"></a>
|
1212
|
-
### Beware of circular dependencies
|
1213
|
-
|
1214
|
-
In Ruby, you can't have certain top-level circular dependencies. Take for example:
|
1215
|
-
|
1216
|
-
```ruby
|
1217
|
-
# c.rb
|
1218
|
-
class C < D
|
1219
|
-
end
|
1220
|
-
|
1221
|
-
# d.rb
|
1222
|
-
class D
|
1223
|
-
C
|
1224
|
-
end
|
1225
|
-
```
|
1226
|
-
|
1227
|
-
In order to define `C`, you need to load `D`. However, the body of `D` refers to `C`.
|
1228
|
-
|
1229
|
-
Circular dependencies like those do not work in plain Ruby, and therefore do not work in projects managed by Zeitwerk either.
|
1230
|
-
|
1231
|
-
<a id="markdown-reopening-third-party-namespaces" name="reopening-third-party-namespaces"></a>
|
1232
|
-
### Reopening third-party namespaces
|
1233
|
-
|
1234
|
-
Projects managed by Zeitwerk can work with namespaces defined by third-party libraries. However, they have to be loaded in memory before calling `setup`.
|
1235
|
-
|
1236
|
-
For example, let's imagine you're writing a gem that implements an adapter for [Active Job](https://guides.rubyonrails.org/active_job_basics.html) that uses AwesomeQueue as backend. By convention, your gem has to define a class called `ActiveJob::QueueAdapters::AwesomeQueue`, and it has to do so in a file with a matching path:
|
1237
|
-
|
1238
|
-
```ruby
|
1239
|
-
# lib/active_job/queue_adapters/awesome_queue.rb
|
1240
|
-
module ActiveJob
|
1241
|
-
module QueueAdapters
|
1242
|
-
class AwesomeQueue
|
1243
|
-
# ...
|
1244
|
-
end
|
1245
|
-
end
|
1246
|
-
end
|
1247
|
-
```
|
1248
|
-
|
1249
|
-
It is very important that your gem _reopens_ the modules `ActiveJob` and `ActiveJob::QueueAdapters` instead of _defining_ them. Because their proper definition lives in Active Job. Furthermore, if the project reloads, you do not want any of `ActiveJob` or `ActiveJob::QueueAdapters` to be reloaded.
|
1250
|
-
|
1251
|
-
Bottom line, Zeitwerk should not be managing those namespaces. Active Job owns them and defines them. Your gem needs to _reopen_ them.
|
1252
|
-
|
1253
|
-
In order to do so, you need to make sure those modules are loaded before calling `setup`. For instance, in the entry file for the gem:
|
1254
|
-
|
1255
|
-
```ruby
|
1256
|
-
# Ensure these namespaces are reopened, not defined.
|
1257
|
-
require "active_job"
|
1258
|
-
require "active_job/queue_adapters"
|
1259
|
-
|
1260
|
-
require "zeitwerk"
|
1261
|
-
# By passing the flag, we acknowledge the extra directory lib/active_job
|
1262
|
-
# has to be managed by the loader and no warning has to be issued for it.
|
1263
|
-
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
1264
|
-
loader.setup
|
1265
|
-
```
|
1266
|
-
|
1267
|
-
With that, when Zeitwerk scans the file system and reaches the gem directories `lib/active_job` and `lib/active_job/queue_adapters`, it detects the corresponding modules already exist and therefore understands it does not have to manage them. The loader just descends into those directories. Eventually will reach `lib/active_job/queue_adapters/awesome_queue.rb`, and since `ActiveJob::QueueAdapters::AwesomeQueue` is unknown, Zeitwerk will manage it. Which is what happens regularly with the files in your gem. On reload, the namespaces are safe, won't be reloaded. The loader only reloads what it manages, which in this case is the adapter itself.
|
1268
|
-
|
1269
|
-
<a id="markdown-introspection" name="introspection"></a>
|
1270
|
-
### Introspection
|
1271
|
-
|
1272
|
-
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
1273
|
-
#### `Zeitwerk::Loader#dirs`
|
1274
|
-
|
1275
|
-
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
1276
|
-
|
1277
|
-
```ruby
|
1278
|
-
loader = Zeitwerk::Loader.new
|
1279
|
-
loader.push_dir(Pathname.new("/foo"))
|
1280
|
-
loader.dirs # => ["/foo"]
|
1281
|
-
```
|
1282
|
-
|
1283
|
-
This method accepts an optional `namespaces` keyword argument. If truthy, the method returns a hash table instead. Keys are the absolute paths of the root directories as strings. Values are their corresponding namespaces, class or module objects:
|
1284
|
-
|
1285
|
-
```ruby
|
1286
|
-
loader = Zeitwerk::Loader.new
|
1287
|
-
loader.push_dir(Pathname.new("/foo"))
|
1288
|
-
loader.push_dir(Pathname.new("/bar"), namespace: Bar)
|
1289
|
-
loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
|
1290
|
-
```
|
1291
|
-
|
1292
|
-
By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
|
1293
|
-
|
1294
|
-
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1295
|
-
|
1296
|
-
<a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
|
1297
|
-
#### `Zeitwerk::Loader#cpath_expected_at`
|
1298
|
-
|
1299
|
-
Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
|
1300
|
-
|
1301
|
-
Some examples, assuming that `app/models` is a root directory:
|
1302
|
-
|
1303
|
-
```ruby
|
1304
|
-
loader.cpath_expected_at("app/models") # => "Object"
|
1305
|
-
loader.cpath_expected_at("app/models/user.rb") # => "User"
|
1306
|
-
loader.cpath_expected_at("app/models/hotel") # => "Hotel"
|
1307
|
-
loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
|
1308
|
-
```
|
1309
|
-
|
1310
|
-
If `collapsed` is a collapsed directory:
|
1311
|
-
|
1312
|
-
```ruby
|
1313
|
-
loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
|
1314
|
-
loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
|
1315
|
-
loader.cpath_expected_at("a/b") # => "A::B"
|
1316
|
-
```
|
1317
|
-
|
1318
|
-
If the argument corresponds to an [ignored file or directory](#ignoring-parts-of-the-project), the method returns `nil`. Same if the argument is not managed by the loader.
|
1319
|
-
|
1320
|
-
`Zeitwerk::Error` is raised if the given path does not exist:
|
1321
|
-
|
1322
|
-
```ruby
|
1323
|
-
loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
|
1324
|
-
```
|
1325
|
-
|
1326
|
-
`Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
|
1327
|
-
|
1328
|
-
```ruby
|
1329
|
-
loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
|
1330
|
-
```
|
1331
|
-
|
1332
|
-
This method does not parse file contents and does not guarantee files define the returned constant path. It just says which is the _expected_ one.
|
1333
|
-
|
1334
|
-
`Zeitwerk::Loader#cpath_expected_at` is designed to be used with individual paths. If you want to know all the expected constant paths in the project, please use `Zeitwerk::Loader#all_expected_cpaths`, documented next.
|
1335
|
-
|
1336
|
-
<a id="markdown-zeitwerkloaderall_expected_cpaths" name="zeitwerkloaderall_expected_cpaths"></a>
|
1337
|
-
#### `Zeitwerk::Loader#all_expected_cpaths`
|
1338
|
-
|
1339
|
-
The method `Zeitwerk::Loader#all_expected_cpaths` returns a hash that maps the absolute paths of the files and directories managed by the receiver to their expected constant paths.
|
1340
|
-
|
1341
|
-
Ignored files, hidden files, and files whose extension is not ".rb" are not included in the result. Same for directories, hidden or ignored directories are not included in the result. Additionally, directories that contain no files with extension ".rb" (recursively) are also excluded, since those are not considered to represent Ruby namespaces.
|
1342
|
-
|
1343
|
-
For example, if `lib` is the root directory of a gem with the following contents:
|
1344
|
-
|
1345
|
-
```
|
1346
|
-
lib/.DS_Store
|
1347
|
-
lib/my_gem.rb
|
1348
|
-
lib/my_gem/version.rb
|
1349
|
-
lib/my_gem/ignored.rb
|
1350
|
-
lib/my_gem/drivers/unix.rb
|
1351
|
-
lib/my_gem/drivers/windows.rb
|
1352
|
-
lib/my_gem/collapsed/foo.rb
|
1353
|
-
lib/tasks/my_gem.rake
|
1354
|
-
```
|
1355
|
-
|
1356
|
-
`Zeitwerk::Loader#all_expected_cpaths` would return (maybe in a different order):
|
1357
|
-
|
1358
|
-
```ruby
|
1359
|
-
{
|
1360
|
-
"/.../lib" => "Object",
|
1361
|
-
"/.../lib/my_gem.rb" => "MyGem",
|
1362
|
-
"/.../lib/my_gem" => "MyGem",
|
1363
|
-
"/.../lib/my_gem/version.rb" => "MyGem::VERSION",
|
1364
|
-
"/.../lib/my_gem/drivers" => "MyGem::Drivers",
|
1365
|
-
"/.../lib/my_gem/drivers/unix.rb" => "MyGem::Drivers::Unix",
|
1366
|
-
"/.../lib/my_gem/drivers/windows.rb" => "MyGem::Drivers::Windows",
|
1367
|
-
"/.../lib/my_gem/collapsed" => "MyGem"
|
1368
|
-
"/.../lib/my_gem/collapsed/foo.rb" => "MyGem::Foo"
|
1369
|
-
}
|
1370
|
-
```
|
1371
|
-
|
1372
|
-
In the previous example we assume `lib/my_gem/ignored.rb` is ignored, and therefore it is not present in the returned hash. Also, `lib/my_gem/collapsed` is a collapsed directory, so the expected namespace at that level is still `MyGem` (this is an edge case).
|
1373
|
-
|
1374
|
-
The file `lib/.DS_Store` is hidden, hence excluded. The directory `lib/tasks` is also not present because it contains no files with extension ".rb".
|
1375
|
-
|
1376
|
-
Directory paths do not have trailing slashes.
|
1377
|
-
|
1378
|
-
The order of the hash entries is undefined.
|
1379
|
-
|
1380
|
-
This method does not parse or execute file contents and does not guarantee files define the corresponding constant paths. It just says which are the _expected_ ones.
|
1381
|
-
|
1382
|
-
<a id="markdown-encodings" name="encodings"></a>
|
1383
|
-
### Encodings
|
1384
|
-
|
1385
|
-
Zeitwerk supports projects whose files and file system are in UTF-8. The encoding of the file system can be checked this way:
|
1386
|
-
|
1387
|
-
```
|
1388
|
-
% ruby -e "puts Encoding.find('filesystem')"
|
1389
|
-
UTF-8
|
1390
|
-
```
|
1391
|
-
|
1392
|
-
The test suite passes on Windows with codepage `Windows-1252` if all the involved absolute paths are ASCII. Other supersets of ASCII may work too, but you have to try.
|
1393
|
-
|
1394
|
-
<a id="markdown-rules-of-thumb" name="rules-of-thumb"></a>
|
1395
|
-
### Rules of thumb
|
1396
|
-
|
1397
|
-
1. Different loaders should manage different directory trees. It is an error condition to configure overlapping root directories in different loaders.
|
1398
|
-
|
1399
|
-
2. Think the mere existence of a file is effectively like writing a `require` call for them, which is executed on demand (autoload) or upfront (eager load).
|
1400
|
-
|
1401
|
-
3. In that line, if two loaders manage files that translate to the same constant in the same namespace, the first one wins, the rest are ignored. Similar to what happens with `require` and `$LOAD_PATH`, only the first occurrence matters.
|
1402
|
-
|
1403
|
-
4. Projects that reopen a namespace defined by some dependency have to ensure said namespace is loaded before setup. That is, the project has to make sure it reopens, rather than defines, the namespace. This is often accomplished by loading (e.g., `require`-ing) the dependency.
|
1404
|
-
|
1405
|
-
5. Objects stored in reloadable constants should not be cached in places that are not reloaded. For example, non-reloadable classes should not subclass a reloadable class, or mixin a reloadable module. Otherwise, after reloading, those classes or module objects would become stale. Referring to constants in dynamic places like method calls or lambdas is fine.
|
1406
|
-
|
1407
|
-
6. In a given process, ideally, there should be at most one loader with reloading enabled. Technically, you can have more, but it may get tricky if one refers to constants managed by the other one. Do that only if you know what you are doing.
|
1408
|
-
|
1409
|
-
<a id="markdown-debuggers" name="debuggers"></a>
|
1410
|
-
### Debuggers
|
1411
|
-
|
1412
|
-
Zeitwerk and [debug.rb](https://github.com/ruby/debug) are fully compatible if CRuby is ≥ 3.1 (see [ruby/debug#558](https://github.com/ruby/debug/pull/558)).
|
1413
|
-
|
1414
|
-
[Byebug](https://github.com/deivid-rodriguez/byebug) is compatible except for an edge case explained in [deivid-rodriguez/byebug#564](https://github.com/deivid-rodriguez/byebug/issues/564). Prior to CRuby 3.1, `debug.rb` has a similar edge incompatibility.
|
1415
|
-
|
1416
|
-
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
1417
|
-
|
1418
|
-
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1419
|
-
## Pronunciation
|
1420
|
-
|
1421
|
-
"Zeitwerk" is pronounced [this way](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3).
|
1422
|
-
|
1423
|
-
<a id="markdown-supported-ruby-versions" name="supported-ruby-versions"></a>
|
1424
|
-
## Supported Ruby versions
|
1425
|
-
|
1426
|
-
Zeitwerk works with CRuby 2.5 and above.
|
1427
|
-
|
1428
|
-
On TruffleRuby all is good except for thread-safety. Right now, in TruffleRuby `Module#autoload` does not block threads accessing a constant that is being autoloaded. CRuby prevents such access to avoid concurrent threads from seeing partial evaluations of the corresponding file. Zeitwerk inherits autoloading thread-safety from this property. This is not an issue if your project gets eager loaded, or if you lazy load in single-threaded environments. (See https://github.com/oracle/truffleruby/issues/2431.)
|
1429
|
-
|
1430
|
-
JRuby 9.3.0.0 is almost there. As of this writing, the test suite of Zeitwerk passes on JRuby except for three tests. (See https://github.com/jruby/jruby/issues/6781.)
|
1431
|
-
|
1432
|
-
<a id="markdown-testing" name="testing"></a>
|
1433
|
-
## Testing
|
1434
|
-
|
1435
|
-
In order to run the test suite of Zeitwerk, `cd` into the project root and execute
|
1436
|
-
|
1437
|
-
```
|
1438
|
-
bin/test
|
1439
|
-
```
|
1440
|
-
|
1441
|
-
To run one particular suite, pass its file name as an argument:
|
1442
|
-
|
1443
|
-
```
|
1444
|
-
bin/test test/lib/zeitwerk/test_eager_load.rb
|
1445
|
-
```
|
1446
|
-
|
1447
|
-
Furthermore, the project has a development dependency on [`minitest-focus`](https://github.com/seattlerb/minitest-focus). To run an individual test mark it with `focus`:
|
1448
|
-
|
1449
|
-
```ruby
|
1450
|
-
focus
|
1451
|
-
test "capitalizes the first letter" do
|
1452
|
-
assert_equal "User", camelize("user")
|
1453
|
-
end
|
1454
|
-
```
|
1455
|
-
|
1456
|
-
and run `bin/test`.
|
1457
|
-
|
1458
|
-
<a id="markdown-motivation" name="motivation"></a>
|
1459
|
-
## Motivation
|
1460
|
-
|
1461
|
-
<a id="markdown-kernelrequire-is-brittle" name="kernelrequire-is-brittle"></a>
|
1462
|
-
### Kernel#require is brittle
|
1463
|
-
|
1464
|
-
Since `require` has global side-effects, and there is no static way to verify that you have issued the `require` calls for code that your file depends on, in practice it is very easy to forget some. That introduces bugs that depend on the load order.
|
1465
|
-
|
1466
|
-
Also, if the project has namespaces, setting things up and getting client code to load things in a consistent way needs discipline. For example, `require "foo/bar"` may define `Foo`, instead of reopen it. That may be a broken window, giving place to superclass mismatches or partially-defined namespaces.
|
1467
|
-
|
1468
|
-
With Zeitwerk, you just name things following conventions and done. Things are available everywhere, and descend is always orderly. Without effort and without broken windows.
|
1469
|
-
|
1470
|
-
<a id="markdown-rails-autoloading-was-brittle" name="rails-autoloading-was-brittle"></a>
|
1471
|
-
### Rails autoloading was brittle
|
1472
|
-
|
1473
|
-
Autoloading in Rails was based on `const_missing` up to Rails 5. That callback lacks fundamental information like the nesting or the resolution algorithm being used. Because of that, Rails autoloading was not able to match Ruby's semantics, and that introduced a [series of issues](https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#common-gotchas). Zeitwerk is based on a different technique and fixed Rails autoloading starting with Rails 6.
|
1474
|
-
|
1475
|
-
<a id="markdown-awards" name="awards"></a>
|
1476
|
-
## Awards
|
1477
|
-
|
1478
|
-
Zeitwerk has been awarded an "Outstanding Performance Award" Fukuoka Ruby Award 2022.
|
1479
|
-
|
1480
|
-
<a id="markdown-thanks" name="thanks"></a>
|
1481
|
-
## Thanks
|
1482
|
-
|
1483
|
-
I'd like to thank [@matthewd](https://github.com/matthewd) for the discussions we've had about this topic in the past years, I learned a couple of tricks used in Zeitwerk from him.
|
1484
|
-
|
1485
|
-
Also, would like to thank [@Shopify](https://github.com/Shopify), [@rafaelfranca](https://github.com/rafaelfranca), and [@dylanahsmith](https://github.com/dylanahsmith), for sharing [this PoC](https://github.com/Shopify/autoload_reloader). The technique Zeitwerk uses to support explicit namespaces was copied from that project.
|
1486
|
-
|
1487
|
-
Jean Boussier ([@casperisfine](https://github.com/casperisfine), [@byroot](https://github.com/byroot)) deserves special mention. Jean migrated autoloading in Shopify when Zeitwerk integration in Rails was yet unreleased. His work and positive attitude have been outstanding, and thanks to his feedback the interface and performance of Zeitwerk are way, way better. Kudos man ❤️.
|
1488
|
-
|
1489
|
-
Finally, many thanks to [@schurig](https://github.com/schurig) for recording an [audio file](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3) with the pronunciation of "Zeitwerk" in perfect German. 💯
|
1490
|
-
|
1491
|
-
<a id="markdown-license" name="license"></a>
|
1492
|
-
## License
|
1493
|
-
|
1494
|
-
Released under the MIT License, Copyright (c) 2019–<i>ω</i> Xavier Noria.
|