• Stars
    star
    413
  • Rank 104,801 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 14 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A wonderfully simple way to load Ruby code

require_all

Gem Version Build Status Coverage

A wonderfully simple way to load your code.

Tired of futzing around with require statements everywhere, littering your code with require File.dirname(__FILE__) crap? What if you could just point something at a big directory full of code and have everything just automagically load?

Wouldn't that be nice? Well, now you can!

Installation

Add this line to your application's Gemfile:

gem 'require_all'

And then execute:

$ bundle

Or install it yourself as:

$ gem install require_all

Usage

require 'require_all'

# load all ruby files in the directory "lib" and its subdirectories
require_all 'lib'

# or load all files by using glob
require_all 'lib/**/*.rb'

# or load files in an Array
require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file? f }

# or load manually specified files
require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'

You can also load files relative to the current file by using require_rel:

# Instead of
require File.dirname(__FILE__) + '/foobar'

# you can do simply like this
require_rel 'foobar'

You can give all the same argument types to the require_rel as for require_all.

It is recommended to use require_rel instead of require_all since it will require files relatively to the current file (__FILE__) as opposed to loading files relative from the working directory.

load_all and load_rel methods also exist to use Kernel#load instead of Kernel#require!

Files are required in alphabetical order and if there are files in nested directories, they are required depth-first. If a NameError caused by a reference to an uninitialised constant is encountered during the requiring process, then a RequireAll::LoadError will be thrown, indicating the file that needs the dependency adding to.

autoload_all

This library also includes methods for performing autoload - what a bargain!

Similar syntax is used as for require_(all|rel) and load_(all|rel) methods with some caveats:

  • Directory and file names have to reflect namespaces and/or constant names:
# lib/dir1/dir2/my_file.rb
module Dir1
  module Dir2
    class MyFile
    end
  end
end

# lib/loader.rb
autoload_all File.dirname(__FILE__) + "/dir1"
  • A base_dir option has to be specified if loading directories or files from some other location than top-level directory:
# lib/dir1/other_file.rb
autoload_all File.dirname(__FILE__) + "/dir2/my_file.rb",
             base_dir: File.dirname(__FILE__) + "/../dir1"
  • All namespaces will be created dynamically by autoload_all - this means that defined?(Dir1) will return "constant" even if my_file.rb is not yet loaded!

Of course there's also an autoload_rel method:

autoload_rel "dir2/my_file.rb", base_dir: File.dirname(__FILE__) + "/../dir1"

If having some problems with autoload_all or autoload_rel then set $DEBUG=true to see how files are mapped to their respective modules and classes.

Version compatibility and upgrading

As of version 2, RequireAll will raise a RequireAll::LoadError if it encounters a NameError caused by a reference to an uninitialised constant during the requiring process. As such, it is not backwards compatible with version 1.x, but simple to upgrade by adding any requires to load dependencies in files that need them. See CHANGES for more details.

Questions? Comments? Concerns?

You can reach the author on github or by email [email protected]

License

MIT (see the LICENSE file for details)

More Repositories

1

RAutomation

RAutomation
Ruby
100
star
2

test-page

Test::Page helps you to write easily maintainable integration tests using Watir, Selenium or any other testing library by implementing Page Objects pattern.
Ruby
81
star
3

win32screenshot

Capture Screenshots on Windows with Ruby
Ruby
70
star
4

hackersays

SCSS
52
star
5

secrets-web

Not Yet Another Password Manager self-hosted Web app written in Go using libsodium
Go
38
star
6

secrets

Not Yet Another Password Manager written in Go using libsodium
Go
28
star
7

WatirSplash

Combining best features of Watir and RSpec for browser-based functional testing.
Ruby
25
star
8

backpocket

Backpocket is a command line utility for storing a reading list of articles from the Internet to your local disk for the future. It's an alternative to Pocket offering all the required features without handing over all your private reading materials to any 3rd party.
Go
10
star
9

secrets-cli

Not Yet Another Password Manager CLI interface written in Go using libsodium
Go
10
star
10

expand-aliases-oh-my-zsh

expand-aliases plugin for oh-my-zsh
Shell
5
star
11

autotest-doom

autotest plugin for showing bloody Doom grunt when specs are failing
Ruby
3
star
12

extreme_startup

Ruby
1
star
13

authorize_action

Really secure and simple authorization library for your Rails, Sinatra or whatever web framework, which just works.
Ruby
1
star
14

PMDb

Personal Movie Database
JavaScript
1
star
15

spork-local_process

Ruby
1
star
16

IT-Really-Matters-blog

Project files for blog entries
Ruby
1
star
17

eventify

Eventify will notify you about upcoming events from different providers/organizers.
HTML
1
star
18

watir-page-helper

This is a page helper for Watir/Watir-WebDriver that allows use easy access to elements.
Ruby
1
star
19

candalf

Candalf is a simple tool that helps to orchestrate Linux and Unix-like system configuration/setup/management using SSH
Shell
1
star
20

dotfiles

Shell
1
star