34: def require_all(*args)
35:
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:
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:
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:
70: files = Dir.glob arg
71:
72:
73: if File.file?(arg + '.rb')
74: file = arg + '.rb'
75: options[:method] != :autoload ? Kernel.send(options[:method], file) : __autoload(file, file, options)
76: return true
77: end
78:
79:
80: raise LoadError, "no such file to load -- #{arg}" if files.empty?
81: end
82: end
83:
84:
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:
104:
105:
106:
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:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
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:
134:
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