Project

dirload

0.0
The project is in a healthy, maintained state
An autoloader that lets you use any module namespace with any folder structure and mix in manual requires.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 0
 Project Readme

Gem version GitHub repo Codeberg repo

Dirload

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

Usage

dirload()

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')

File Support

Dirload supports.rb as well as the following file types:

RBX

RBX (.rbx) files are Ruby files containing HTML:

class MyClass
  def render
    <p>Hello</p>
  end
end

ℹ️ For more information see LowNode.

Antlers

Antlers syntax can be embedded inside the render method of your RBX file:

class ParentNode
  def render
    <p><{ ChildNode }></p>
  end
end

ℹ️ See Antlers.

How it works

  1. First Dirload goes through all your files and notes their constant definitions
  2. Then it goes through the same files again and creates autoloads for those dependencies
  3. 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.

Caveats

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.
end

Adapter Interface [UNRELEASED]

Dirload will support an adapter interface where any file type can be automatically loaded by dirload()... once you define how to load it.

Philosophy

  • 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 require and require_relative calls. You often need files outside the autoloaded directory

Installation

Add gem 'dirload' to your Gemfile then:

bundle install