Dirload is an autoloader where folders don't have to follow namespaces and you can mix and match autoloading with manual requires. Dirload is like a bull in a china shop, smashing through fragile dependencies to autoload your code without manual load, require or require_relative calls.
✨ Features:
- Use any namespace or folder structure you want (they don't have to match)
- Manually require files from autoloaded files
- Supports Ruby, RBX, Markdown and can be extended
Load an entire directory with:
# Absolute path.
dirload(File.expand_path('app', __FILE__))
# Path relative to `Dir.pwd`.
dirload('app')Dirload supports loading individual files too:
dirload('spec/fixtures/html_node.rb')Dirload supports.rb as well as the following file types:
RBX (.rbx) files are Ruby files containing HTML:
class MyClass
def render
<p>Hello</p>
end
endℹ️ For more information see LowNode.
Antlers syntax can be embedded inside the render method of your RBX file:
class ParentNode
def render
<p><{ ChildNode }></p>
end
endℹ️ See Antlers.
- First Dirload goes through all your files and notes their constant definitions
- Then it goes through the same files again and creates autoloads for those dependencies
- Then it goes through every file again and loads it into Ruby
This approach results in a very flexible autoloader with no conventions to follow, no internal dependency graph and very little configuration needed... just point it at a directory. If there are dependencies outside the directory then you can just require those manually from files within the directory, or use a boot file.
Like other autoloading libraries, Dirload doesn't support circular dependencies.
❌ Don't do:
class A
include B
end
class B
include A
end✅ Instead do:
class A
include C
end
class B
include C
end
class C
# Code that both A and B share.
endDirload will support an adapter interface where any file type can be automatically loaded by dirload()... once you define how to load it.
- Folders are often organised by the kind of file it is, for example; a bunch of views. But namespaces should be organised by your domain, which can be different to your file structure
- You should be able to mix autoloading with manual
requireandrequire_relativecalls. You often need files outside the autoloaded directory
Add gem 'dirload' to your Gemfile then:
bundle install