Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Zeitwerk::Loader#setup to be run externally #223

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/dry/system/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def initialize(name, mod, &block)
end

# @api private
def apply_to(system, options)
system.extend(stateful? ? mod.new(options) : mod)
def apply_to(system, **options)
system.extend(stateful? ? mod.new(**options) : mod)
system.instance_eval(&block) if block
system
end
Expand Down Expand Up @@ -90,13 +90,13 @@ def self.loaded_dependencies
# @return [self]
#
# @api public
def use(name, options = {})
def use(name, **options)
return self if enabled_plugins.include?(name)

raise PluginNotFoundError, name unless (plugin = Plugins.registry[name])

plugin.load_dependencies
plugin.apply_to(self, options)
plugin.apply_to(self, **options)

enabled_plugins << name

Expand Down
3 changes: 2 additions & 1 deletion lib/dry/system/plugins/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class Env < Module
attr_reader :options

# @api private
def initialize(options)
def initialize(**options)
@options = options
super()
end

def inferrer
Expand Down
28 changes: 17 additions & 11 deletions lib/dry/system/plugins/zeitwerk.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "dry/system/constants"
require "dry/system/plugins/zeitwerk/compat_inflector"

module Dry
module System
Expand All @@ -10,23 +9,30 @@ module Plugins
class Zeitwerk < Module
# @api private
def self.dependencies
["dry/system/loader/autoloading", {"zeitwerk" => "zeitwerk"}]
[
"dry/system/loader/autoloading",
"dry/system/plugins/zeitwerk/compat_inflector",
{"zeitwerk" => "zeitwerk"}
]
end

# @api private
attr_reader :options
attr_reader :loader, :setup, :eager_load, :debug

# @api private
def initialize(options)
@options = options
def initialize(loader: nil, setup: true, eager_load: nil, debug: false)
@loader = loader || ::Zeitwerk::Loader.new
@setup = setup
@eager_load = eager_load
@debug = debug
super()
end

# @api private
def extended(system)
system.setting :autoloader, reader: true

system.config.autoloader = options.fetch(:loader) { ::Zeitwerk::Loader.new }
system.config.autoloader = loader
system.config.component_dirs.loader = Dry::System::Loader::Autoloading
system.config.component_dirs.add_to_load_path = false

Expand All @@ -42,7 +48,7 @@ def setup_autoloader(system)

push_component_dirs_to_loader(system, system.autoloader)

system.autoloader.setup
system.autoloader.setup if setup

system.after(:finalize) { system.autoloader.eager_load } if eager_load?(system)

Expand All @@ -55,7 +61,7 @@ def setup_autoloader(system)
def configure_loader(loader, system)
loader.tag = system.config.name || system.name unless loader.tag
loader.inflector = CompatInflector.new(system.config)
loader.logger = method(:puts) if options[:debug]
loader.logger = method(:puts) if debug
end

# Add component dirs to the zeitwerk loader
Expand Down Expand Up @@ -93,9 +99,9 @@ def get_or_define_module(parent_mod, name)
end

def eager_load?(system)
options.fetch(:eager_load) {
system.config.respond_to?(:env) && system.config.env == :production
}
return eager_load unless eager_load.nil?

system.config.respond_to?(:env) && system.config.env == :production
end
end
end
Expand Down