RequireDir
This gem provides an easy way to require all file from a folder – recursively, or not.
Unlike other gems, such as require_all, this gem does suffer from module clobbering or changing global search paths.
Author
This library is the work of Konstantin Gredeskoul, © 2016, distributed under the MIT license.
Installation
Add this line to your application's Gemfile:
gem 'require_dir'
And then execute:
$ bundle
Or install it with gem command:
$ gem install require_dir
Usage
Recommended usage is to include this gem's module into the top level module of your library, like so:
# file 'lib/mylib.rb' -- top level file for a gem 'mylib'
require 'require_dir'
module Mylib
extend RequireDir
init __FILE__
end
Mylib.dir('mylib/subfolder') # loads all files in the folder 'lib/mylib/subfolder/*.rb'
Mylib.dir_r('mylib/subfolder') # recursive load from 'lib/mylib/subfolder/**/*.rb'
Offset
You can optionally load the library using init_with_offset method, which allows you
to initialize the library from a file located not directly under lib, but further down below.
Say you want to initialize the library from lib/mylib/subfolder/mymodule.rb. But you want to
be able to later use RequireDir to load files relative to lib. This is how you would do that:
# file 'lib/mylib/subfolder/mymodule.rb' -- top level file for a gem 'mylib'
require 'require_dir'
module Mylib
module SubFolder
extend RequireDir
init_with_offset(__FILE__, 1)
end
end
Mylib.dir('mylib/subfolder') # loads all files in the folder 'lib/mylib/subfolder/*.rb'
Mylib.dir_r('mylib/subfolder') # recursive load from 'lib/mylib/subfolder/**/*.rb'
Load Errors
The library attempts to catch load errors and print out (to STDERR) meaningful and colorful messages.
But sometimes that's not enough – and you may want to see which file was RequireDir loading just before it exploded. You can enable debugging output using two methods:
- Set environment variable
REQUIRE_DIR_DEBUG - Initialize library with options hash, setting:
extend RequireDir
init __FILE__, debug: 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/kigster/require_dir.
License
The gem is available as open source under the terms of the MIT License.