How does one go about using dry-system with or without Zeitwerk to collapse directories?
Directory collapsing is a standard feature of zeitwerk, so you need to define your own loader.
require "bundler/setup"
require "dry/system/container"
require "zeitwerk"
module Bookshelf
# Adapt zeitwerk inflector interface to dry-inflector
class Inflector
def camelize(basename, _abspath)
Application.inflector.camelize(basename)
end
end
def self.autoloader
@autoloader ||= Zeitwerk::Loader.new.tap do |loader|
loader.inflector = Inflector.new
loader.push_dir Application.config.root.join("app").realpath, namespace: Bookshelf
# configuration for collapsed directories goes here
loader.enable_reloading
loader.setup
end
end
class Application < Dry::System::Container
configure do |config|
require "dry/inflector"
config.root = Pathname(__dir__).parent.expand_path
config.inflector = Dry::Inflector.new
end
def self.inflector = config.inflector
use :zeitwerk, loader: Bookshelf.autoloader
end
end
The order of operations to make sure things get initialized in the right order is a little tricky. This is adapted from how I did it.
It is important that your inflector is initialized first before the autoloader. This allows you to configure acronyms for your constants.