Skip to content

Instantly share code, notes, and snippets.

@acook
Created August 17, 2012 17:16
Show Gist options
  • Save acook/3380730 to your computer and use it in GitHub Desktop.
Save acook/3380730 to your computer and use it in GitHub Desktop.
Recursive mass require files with only a relative path. Just require the `require_path_method.rb` file or copy/paste the contents into your project to use it. The `oneliner.rb` script performs identically to the full `requirepath.rb` script.
$ pwd
/Users/acook/Projects/personal/3380730
$ bash makepaths.sh
-- current_file: requirepath.rb
-- relative_path_of_current_file: .
-- path: /Users/acook/Projects/personal/3380730
-- filematch: /Users/acook/Projects/personal/3380730/features/support/**/*.rb
-- file: /Users/acook/Projects/personal/3380730/features/support/whatever/something.rb
-- required: true
Dir[File.expand_path('..', File.dirname(__FILE__)) + '/features/support/**/*.rb'].map{|file| require file}
def require_path relative_path
Dir[File.expand_path('..', File.dirname(__FILE__)) + File.join(relative_path, '**', '*.rb')].map{|file| require file}
end
def out *args
puts "-- #{args.shift}: #{args.join(' ')}"
end
def stuff
current_file = __FILE__
out :current_file, current_file
relative_path_of_current_file = File.dirname current_file
out :relative_path_of_current_file, relative_path_of_current_file
path = File.expand_path '..', relative_path_of_current_file
out :path, path
filematch = File.join path, "features/support/**/*.rb"
out :filematch, filematch
Dir[filematch].each do |file|
out :file, file
out :required, require(file)
end
end
stuff
#!bash
mkdir -p features/support/whatever
touch features/support/whatever/something.rb
mkdir scripts
cp requirepath.rb scripts/
cd scripts
ruby requirepath.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment