diff options
author | Xavier Noria <fxn@hashref.com> | 2019-02-11 12:44:25 -0800 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2019-02-12 02:28:04 -0800 |
commit | 821d6c694cd305b7792b96d6ebc1c15ca235cf3e (patch) | |
tree | cb72d427f1744ad5a5c7dc901cb53db63c94a591 /activesupport/lib/active_support/dependencies/zeitwerk_integration.rb | |
parent | ad3cbc2452e8150542ecb539925396361f12534c (diff) | |
download | rails-821d6c694cd305b7792b96d6ebc1c15ca235cf3e.tar.gz rails-821d6c694cd305b7792b96d6ebc1c15ca235cf3e.tar.bz2 rails-821d6c694cd305b7792b96d6ebc1c15ca235cf3e.zip |
Zeitwerk integration
Diffstat (limited to 'activesupport/lib/active_support/dependencies/zeitwerk_integration.rb')
-rw-r--r-- | activesupport/lib/active_support/dependencies/zeitwerk_integration.rb | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb new file mode 100644 index 0000000000..8b2e1b5391 --- /dev/null +++ b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb @@ -0,0 +1,71 @@ +module ActiveSupport + module Dependencies + module ZeitwerkIntegration # :nodoc: all + module Decorations + def clear + Dependencies.unload_interlock do + Rails.autoloader.reload + end + end + + def constantize(cpath) + Inflector.constantize(cpath) + end + + def safe_constantize(cpath) + Inflector.safe_constantize(cpath) + end + + def autoloaded_constants + Rails.autoloaders.flat_map do |autoloader| + autoloader.loaded.to_a + end + end + + def autoloaded?(object) + cpath = object.is_a?(Module) ? object.name : object.to_s + Rails.autoloaders.any? { |autoloader| autoloader.loaded?(cpath) } + end + end + + class << self + def take_over + setup_autoloaders + freeze_autoload_paths + decorate_dependencies + end + + private + + def setup_autoloaders + Dependencies.autoload_paths.each do |autoload_path| + if File.directory?(autoload_path) + if autoload_once?(autoload_path) + Rails.once_autoloader.push_dir(autoload_path) + else + Rails.autoloader.push_dir(autoload_path) + end + end + end + + Rails.autoloaders.each(&:setup) + end + + def autoload_once?(autoload_path) + Dependencies.autoload_once_paths.include?(autoload_path) || + Gem.path.any? { |gem_path| autoload_path.to_s.start_with?(gem_path) } + end + + def freeze_autoload_paths + Dependencies.autoload_paths.freeze + Dependencies.autoload_once_paths.freeze + end + + def decorate_dependencies + Dependencies.singleton_class.prepend(Decorations) + Object.class_eval { alias_method :require_dependency, :require } + end + end + end + end +end |