0.0
No commit activity in last 3 years
No release in over 3 years
Easily and non-intrusively require files from sub-folders. Without polluting global namespace, or having modules clobber each other, include RequireDir and initialize it to get access to #dir and #dir_r
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 10.0
~> 3.0

Runtime

 Project Readme

Build Status Maintainability Test Coverage
Gem Version Downloads


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-2018, 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
  # You must pass __FILE__ so that the library knows the "root" of your sources.
  RequireDir.enable_require_dir!(self, __FILE__)

  dir_r 'mylib/cli'
  dir   'mylib/commands'
end

You literally have access to two methods (with aliases):

  • dir_r (or require_dir_r) loads all ruby files recursively
  • dir (or require_dir) loads files non-recursively

Offset

If your library's top required file is not at the very top level of the directory structure, which might happen, if, say you have a gem named app-foo, and your primary required file is located in lib/app/foo.rb, in this case you will need to pass an offset of 1 to the enable_require_dir! method to properly initialize recursive loading:

# 'lib/app/foo.rb' 
# i.e. this file is one level deep in the source tree of this library:

require 'require_dir'

module App
  module Foo
    # offset is 1, because the current file is 1 level deep into the source tree.
    RequireDir.enable_require_dir!(self, __FILE__, 1) 

    dir_r 'app/foo/bar'   # loads all files in the folder 'app/foo/bar/**.rb'
  end
end

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:
module Foo
  RequireDir.enable_require_dir!(self, __FILE__, 0, debug: true)
end

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.