Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
Last active December 10, 2015 10:38
Show Gist options
  • Save seanlinsley/4422450 to your computer and use it in GitHub Desktop.
Save seanlinsley/4422450 to your computer and use it in GitHub Desktop.
Why can't nested /lib loading be easy?
module YourApplication
class Application < Rails::Application
# Loads ruby files in a given directory.
# NOTE: `require_dependency` auto-reloads on file change when in development.
# POSTERITY: this was needed to prevent auto-namespacing of lib/a/b.rb files.
def loader(*dirs)
dirs.each do |dir|
Dir["#{Rails.root}/#{dir}/*.rb"].each do |path|
d = dir.gsub /lib\/?/, '' # removes lib and lib/
require_dependency (d.blank? ? '' : d + '/') + File.basename(path, '.rb')
end
end
end
# Load lib, lib/core_ext and lib/model_ext before everything else
# Load gem_ext after the Gems themselves have been loaded (to override behavior)
config.autoload_paths += %W(#{Rails.root}/lib)
config.before_initialize { loader 'lib/core_ext', 'lib/model_ext' }
config.after_initialize { loader 'lib/gem_ext' }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment