Last active
December 21, 2015 06:38
-
-
Save pboling/6265104 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DESCRIPTION: | |
# For auto lazy loading of namespaced objects nested in sub-directories | |
# This is a custom library loader - Written by Peter Boling | |
# INSTALLATION: | |
# 1. Add 'gist-dep' to your Gemfile | |
# 2. bundle install | |
# 3. gist-dep add --path lib/handy_gists/nested_lib_loader.rb 6265104/nested_lib_loader.rb | |
# 4. add the following lines (uncommented) to your config/application.rb inside the `class Application < Rails::Application` | |
# require "#{config.root}/lib/handy_gists/nested_lib_loader" | |
# include HandyGists::NestedLibLoader | |
# REMOVAL: | |
# 1. gist-dep remove 6265104/nested_lib_loader.rb | |
# USAGE: | |
# # Custom directories with classes and modules you want to be autoloadable. | |
# # Load Middleware | |
# nested_app_libs('middleware/stuff', true) | |
# # Load Extensions to Standard Ruby/Rails libs | |
# nested_libs('lib/ext', true) | |
# # Load the lib stuff as these are lower level than app code | |
# %w( lib ).each do |lib_dir| | |
# config.autoload_paths += nested_libs(lib_dir, false) | |
# end | |
# # For auto lazy loading of namespaced objects nested in sub-directories | |
# %w( railties classes models modules presenters workers ).each do |lib_dir| | |
# config.autoload_paths += nested_app_libs(lib_dir, false) | |
# end | |
# | |
module HandyGists | |
module NestedLibLoader | |
# Used to load nested libraries | |
def nested_libs(dir, do_require = false) | |
Dir["#{dir}/**/*"].select do |x| | |
!File.directory?(x) && x.to_s =~ /\.rb/ | |
end.map do |x| | |
library = "#{config.root}/#{x}" | |
if do_require | |
require library | |
else | |
library | |
end | |
end | |
end | |
# nested directory structures inside app/ | |
def nested_app_libs(dir, require = false) | |
nested_libs("app/#{dir}", require) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment