# File lib/require_all.rb, line 34
 34:   def require_all(*args)
 35:     # Handle passing an array as an argument

 36:     args.flatten!
 37: 
 38:     options = {:method => :require}
 39:     options.merge!(args.pop) if args.last.is_a?(Hash)
 40: 
 41:     if args.empty?
 42:       puts "no files were loaded due to an empty Array" if $DEBUG
 43:       return false
 44:     end
 45: 
 46:     if args.size > 1
 47:       # Expand files below directories

 48:       files = args.map do |path|
 49:         if File.directory? path
 50:           Dir[File.join(path, '**', '*.rb')]
 51:         else
 52:           path
 53:         end
 54:       end.flatten
 55:     else
 56:       arg = args.first
 57:       begin
 58:         # Try assuming we're doing plain ol' require compat

 59:         stat = File.stat(arg)
 60: 
 61:         if stat.file?
 62:           files = [arg]
 63:         elsif stat.directory?
 64:           files = Dir.glob File.join(arg, '**', '*.rb')
 65:         else
 66:           raise ArgumentError, "#{arg} isn't a file or directory"
 67:         end
 68:       rescue SystemCallError
 69:         # If the stat failed, maybe we have a glob!

 70:         files = Dir.glob arg
 71: 
 72:         # Maybe it's an .rb file and the .rb was omitted

 73:         if File.file?(arg + '.rb')
 74:           file = File.expand_path(arg + '.rb')
 75:           options[:method] != :autoload ? Kernel.send(options[:method], file) : __autoload(file, file, options)
 76:           return true
 77:         end
 78: 
 79:         # If we ain't got no files, the glob failed

 80:         raise LoadError, "no such file to load -- #{arg}" if files.empty?
 81:       end
 82:     end
 83: 
 84:     # If there's nothing to load, you're doing it wrong!

 85:     raise LoadError, "no files to load" if files.empty?
 86: 
 87:     if options[:method] == :autoload
 88:       files.map! { |file| [file, File.expand_path(file)] }
 89:       files.each do |file, full_path|
 90:         __autoload(file, full_path, options)
 91:       end
 92: 
 93:       return true
 94:     end
 95: 
 96:     files.map! { |file| File.expand_path file }
 97:     files.sort!
 98: 
 99:     begin
100:       failed = []
101:       first_name_error = nil
102: 
103:       # Attempt to load each file, rescuing which ones raise NameError for

104:       # undefined constants.  Keep trying to successively reload files that 

105:       # previously caused NameErrors until they've all been loaded or no new

106:       # files can be loaded, indicating unresolvable dependencies.

107:       files.each do |file|
108:         begin
109:           Kernel.send(options[:method], file)
110:         rescue NameError => ex
111:           failed << file
112:           first_name_error ||= ex
113:         rescue ArgumentError => ex
114:           # Work around ActiveSuport freaking out... *sigh*

115:           #

116:           # ActiveSupport sometimes throws these exceptions and I really

117:           # have no idea why.  Code loading will work successfully if these

118:           # exceptions are swallowed, although I've run into strange 

119:           # nondeterministic behaviors with constants mysteriously vanishing.

120:           # I've gone spelunking through dependencies.rb looking for what 

121:           # exactly is going on, but all I ended up doing was making my eyes 

122:           # bleed.

123:           #

124:           # FIXME: If you can understand ActiveSupport's dependencies.rb 

125:           # better than I do I would *love* to find a better solution

126:           raise unless ex.message["is not missing constant"]
127: 
128:           STDERR.puts "Warning: require_all swallowed ActiveSupport 'is not missing constant' error"
129:           STDERR.puts ex.backtrace[0..9]
130:         end
131:       end
132: 
133:       # If this pass didn't resolve any NameErrors, we've hit an unresolvable

134:       # dependency, so raise one of the exceptions we encountered.

135:       if failed.size == files.size
136:         raise first_name_error
137:       else
138:         files = failed
139:       end
140:     end until failed.empty?
141: 
142:     true
143:   end