Skip to content

Commit

Permalink
Allow Zeitwerk::Loader#setup to be run externally
Browse files Browse the repository at this point in the history
This is useful if external integrating systems (e.g. Hanami) want to make `#setup` call at a different time.
  • Loading branch information
timriley committed Feb 6, 2022
1 parent 13f8c87 commit 99767e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
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
2 changes: 1 addition & 1 deletion lib/dry/system/plugins/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Env < Module
attr_reader :options

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

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 if !eager_load.nil?

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

0 comments on commit 99767e4

Please sign in to comment.