Guest User

Untitled

a guest
Jun 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. begin
  2. require 'term/ansicolor'
  3. rescue LoadError
  4. end
  5.  
  6. module Kernel
  7.  
  8. def autoloads(lib_dir)
  9.  
  10. base_dir = File.expand_path File.join(Rails.root, lib_dir)
  11. dir_glob = File.join(base_dir, '**/')
  12.  
  13. Dir[dir_glob].sort_by(&:length).reverse.each do |dir|
  14.  
  15. constant_names = dir.gsub(File.dirname(base_dir), '').
  16. gsub(/(^\/|\/$)/, '').
  17. titlecase.
  18. gsub(' ','').
  19. split('/')
  20.  
  21. mod = constant_names.inject(Object) do |const, name|
  22. if const.const_defined?(name)
  23. const.const_get(name)
  24. else
  25. const.const_set(name, Module.new)
  26. end
  27. end # inject
  28.  
  29. Dir[File.join dir, "*.rb"].each do |fn|
  30. const_name = File.basename(fn).gsub(/\.rb$/,'').titlecase.gsub(' ','').to_sym
  31. # UNCOMMENT FOR DEBUG INFO:
  32. # puts Term::ANSIColor.green("#{mod} -> autoload :#{const_name}, '#{fn}'") if defined?(Term::ANSIColor)
  33. mod.autoload const_name, fn
  34. end
  35.  
  36. end
  37. end # self.load
  38.  
  39. end
Add Comment
Please, Sign In to add comment