LazyRequire
Isn't it annoying when you try to load one file that is dependent on another file that hasn't been loaded yet? LazyRequire lets you require all your project files without worrying about the load order.
It has a similar function to the Rails autoloader, but is much simpler.
Installation
Add this line to your application's Gemfile:
gem 'lazy_require'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lazy_require
Usage
Given this folder structure:
app_root
|- lib/
|- first.rb
|- second.rb
|- app.rb
If you have files like these inside lib:
# first.rb
# The First class depends on the Second class
class First < Second
end
# second.rb
class Second
# Does something special
end
You might want to load all files inside your lib directory from the app.rb root file. Doing something like this could easily fail if load order matters:
# App.rb
# This might cause issues
Dir['./lib/**/*.rb'].each { |file| require file }
class App
# Some stuff here
end
In the above files, its required to load the "second.rb" file prior to the "first.rb" file. You could add a line like this, explicitly adding the require line:
# first.rb
require_relative './second.rb'
# The First class depends on the Second class
class First < Second
end
But that can get cumbersome, especially when you start moving or renaming files in your project.
With LazyRequire, you can ask it to load all the files in your project/folder, without having to think about the load order. If LazyRequire tries to load a file, which has an unloaded dependency, it will simply skip that file and try again later. So your app.rb file can look something like this:
# App.rb
LazyRequire.require_all('./lib/**/*.rb')
class App
# Some stuff here
end
LazyRequire.load_all() accepts any glob pattern and will try to require all files that it finds with that glob pattern. If it it successfully loads all files it will return true:
2.3.0 :010 > LazyRequire.require_all('./spec/support/load_all/**/*.rb')
#=> true
If it cannot load any of the files, it will raise an exception:
2.3.0 :005 > LazyRequire.require(@files)
NameError: uninitialized constant Top
from /code/lazy_require/spec/support/errors/top_two.rb:2:in `<top (required)>'
from /code/lazy_require/lib/lazy_require.rb:13:in `require'
from /code/lazy_require/lib/lazy_require.rb:13:in `require'
from (irb):5
If you want to load a specific collection of files and avoid using the glob pattern, you can do that to using the LazyRequire#require() method.
files = [
'./spec/support/errors/top_two.rb',
'./spec/support/errors/top.rb',
]
LazyRequire.require(files)
#=> true
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/thomas07vt/lazy_require.
License
The gem is available as open source under the terms of the MIT License.