require_all 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -3
- data/CHANGES +4 -0
- data/Rakefile +6 -7
- data/lib/require_all.rb +276 -277
- data/require_all.gemspec +22 -22
- data/spec/autoload_shared.rb +84 -74
- data/spec/autoload_spec.rb +65 -65
- data/spec/load_spec.rb +60 -56
- data/spec/require_shared.rb +78 -68
- data/spec/require_spec.rb +45 -45
- metadata +25 -11
- checksums.yaml +0 -7
data/.travis.yml
CHANGED
data/CHANGES
CHANGED
data/Rakefile
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
|
3
|
-
require "rspec/core/rake_task"
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
5
|
-
|
6
|
-
task :
|
7
|
-
task :release => :spec
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
task :default => :spec
|
6
|
+
task :release => :spec
|
data/lib/require_all.rb
CHANGED
@@ -1,277 +1,276 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (C)2009 Tony Arcieri
|
3
|
-
# You can redistribute this under the terms of the MIT license
|
4
|
-
# See file LICENSE for details
|
5
|
-
#++
|
6
|
-
|
7
|
-
module RequireAll
|
8
|
-
# A wonderfully simple way to load your code.
|
9
|
-
#
|
10
|
-
# The easiest way to use require_all is to just point it at a directory
|
11
|
-
# containing a bunch of .rb files. These files can be nested under
|
12
|
-
# subdirectories as well:
|
13
|
-
#
|
14
|
-
# require_all 'lib'
|
15
|
-
#
|
16
|
-
# This will find all the .rb files under the lib directory and load them.
|
17
|
-
# The proper order to load them in will be determined automatically.
|
18
|
-
#
|
19
|
-
# If the dependencies between the matched files are unresolvable, it will
|
20
|
-
# throw the first unresolvable NameError.
|
21
|
-
#
|
22
|
-
# You can also give it a glob, which will enumerate all the matching files:
|
23
|
-
#
|
24
|
-
# require_all 'lib/**/*.rb'
|
25
|
-
#
|
26
|
-
# It will also accept an array of files:
|
27
|
-
#
|
28
|
-
# require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }
|
29
|
-
#
|
30
|
-
# Or if you want, just list the files directly as arguments:
|
31
|
-
#
|
32
|
-
# require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
|
33
|
-
#
|
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 = 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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
files.
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
files.
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
# I
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
STDERR.puts
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
#
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
#
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
paths.
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
#
|
164
|
-
|
165
|
-
|
166
|
-
paths.
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
#
|
193
|
-
#
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
#
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
205
|
-
#
|
206
|
-
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
paths.
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
options
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
paths.
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
options
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
file_path
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
#
|
252
|
-
#
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
#
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
include RequireAll
|
1
|
+
#--
|
2
|
+
# Copyright (C)2009 Tony Arcieri
|
3
|
+
# You can redistribute this under the terms of the MIT license
|
4
|
+
# See file LICENSE for details
|
5
|
+
#++
|
6
|
+
|
7
|
+
module RequireAll
|
8
|
+
# A wonderfully simple way to load your code.
|
9
|
+
#
|
10
|
+
# The easiest way to use require_all is to just point it at a directory
|
11
|
+
# containing a bunch of .rb files. These files can be nested under
|
12
|
+
# subdirectories as well:
|
13
|
+
#
|
14
|
+
# require_all 'lib'
|
15
|
+
#
|
16
|
+
# This will find all the .rb files under the lib directory and load them.
|
17
|
+
# The proper order to load them in will be determined automatically.
|
18
|
+
#
|
19
|
+
# If the dependencies between the matched files are unresolvable, it will
|
20
|
+
# throw the first unresolvable NameError.
|
21
|
+
#
|
22
|
+
# You can also give it a glob, which will enumerate all the matching files:
|
23
|
+
#
|
24
|
+
# require_all 'lib/**/*.rb'
|
25
|
+
#
|
26
|
+
# It will also accept an array of files:
|
27
|
+
#
|
28
|
+
# require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }
|
29
|
+
#
|
30
|
+
# Or if you want, just list the files directly as arguments:
|
31
|
+
#
|
32
|
+
# require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
|
33
|
+
#
|
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 = 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
|
+
return if files.empty?
|
85
|
+
|
86
|
+
if options[:method] == :autoload
|
87
|
+
files.map! { |file_| [file_, File.expand_path(file_)] }
|
88
|
+
files.each do |file_, full_path|
|
89
|
+
__autoload(file_, full_path, options)
|
90
|
+
end
|
91
|
+
|
92
|
+
return true
|
93
|
+
end
|
94
|
+
|
95
|
+
files.map! { |file_| File.expand_path file_ }
|
96
|
+
files.sort!
|
97
|
+
|
98
|
+
begin
|
99
|
+
failed = []
|
100
|
+
first_name_error = nil
|
101
|
+
|
102
|
+
# Attempt to load each file, rescuing which ones raise NameError for
|
103
|
+
# undefined constants. Keep trying to successively reload files that
|
104
|
+
# previously caused NameErrors until they've all been loaded or no new
|
105
|
+
# files can be loaded, indicating unresolvable dependencies.
|
106
|
+
files.each do |file_|
|
107
|
+
begin
|
108
|
+
Kernel.send(options[:method], file_)
|
109
|
+
rescue NameError => ex
|
110
|
+
failed << file_
|
111
|
+
first_name_error ||= ex
|
112
|
+
rescue ArgumentError => ex
|
113
|
+
# Work around ActiveSuport freaking out... *sigh*
|
114
|
+
#
|
115
|
+
# ActiveSupport sometimes throws these exceptions and I really
|
116
|
+
# have no idea why. Code loading will work successfully if these
|
117
|
+
# exceptions are swallowed, although I've run into strange
|
118
|
+
# nondeterministic behaviors with constants mysteriously vanishing.
|
119
|
+
# I've gone spelunking through dependencies.rb looking for what
|
120
|
+
# exactly is going on, but all I ended up doing was making my eyes
|
121
|
+
# bleed.
|
122
|
+
#
|
123
|
+
# FIXME: If you can understand ActiveSupport's dependencies.rb
|
124
|
+
# better than I do I would *love* to find a better solution
|
125
|
+
raise unless ex.message["is not missing constant"]
|
126
|
+
|
127
|
+
STDERR.puts "Warning: require_all swallowed ActiveSupport 'is not missing constant' error"
|
128
|
+
STDERR.puts ex.backtrace[0..9]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# If this pass didn't resolve any NameErrors, we've hit an unresolvable
|
133
|
+
# dependency, so raise one of the exceptions we encountered.
|
134
|
+
if failed.size == files.size
|
135
|
+
raise first_name_error
|
136
|
+
else
|
137
|
+
files = failed
|
138
|
+
end
|
139
|
+
end until failed.empty?
|
140
|
+
|
141
|
+
true
|
142
|
+
end
|
143
|
+
|
144
|
+
# Works like require_all, but paths are relative to the caller rather than
|
145
|
+
# the current working directory
|
146
|
+
def require_rel(*paths)
|
147
|
+
# Handle passing an array as an argument
|
148
|
+
paths.flatten!
|
149
|
+
return false if paths.empty?
|
150
|
+
|
151
|
+
source_directory = File.dirname caller.first.sub(/:\d+$/, '')
|
152
|
+
paths.each do |path|
|
153
|
+
require_all File.join(source_directory, path)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Loads all files like require_all instead of requiring
|
158
|
+
def load_all(*paths)
|
159
|
+
require_all paths, :method => :load
|
160
|
+
end
|
161
|
+
|
162
|
+
# Loads all files by using relative paths of the caller rather than
|
163
|
+
# the current working directory
|
164
|
+
def load_rel(*paths)
|
165
|
+
paths.flatten!
|
166
|
+
return false if paths.empty?
|
167
|
+
|
168
|
+
source_directory = File.dirname caller.first.sub(/:\d+$/, '')
|
169
|
+
paths.each do |path|
|
170
|
+
require_all File.join(source_directory, path), :method => :load
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# Performs Kernel#autoload on all of the files rather than requiring immediately.
|
175
|
+
#
|
176
|
+
# Note that all Ruby files inside of the specified directories should have same module name as
|
177
|
+
# the directory itself and file names should reflect the class/module names.
|
178
|
+
# For example if there is a my_file.rb in directories dir1/dir2/ then
|
179
|
+
# there should be a declaration like this in my_file.rb:
|
180
|
+
# module Dir1
|
181
|
+
# module Dir2
|
182
|
+
# class MyFile
|
183
|
+
# ...
|
184
|
+
# end
|
185
|
+
# end
|
186
|
+
# end
|
187
|
+
#
|
188
|
+
# If the filename and namespaces won't match then my_file.rb will be loaded into wrong module!
|
189
|
+
# Better to fix these files.
|
190
|
+
#
|
191
|
+
# Set $DEBUG=true to see how files will be autoloaded if experiencing any problems.
|
192
|
+
#
|
193
|
+
# If trying to perform autoload on some individual file or some inner module, then you'd have
|
194
|
+
# to always specify *:base_dir* option to specify where top-level namespace resides.
|
195
|
+
# Otherwise it's impossible to know the namespace of the loaded files.
|
196
|
+
#
|
197
|
+
# For example loading only my_file.rb from dir1/dir2 with autoload_all:
|
198
|
+
#
|
199
|
+
# autoload_all File.dirname(__FILE__) + '/dir1/dir2/my_file',
|
200
|
+
# :base_dir => File.dirname(__FILE__) + '/dir1'
|
201
|
+
#
|
202
|
+
# WARNING: All modules will be created even if files themselves aren't loaded yet, meaning
|
203
|
+
# that all the code which depends of the modules being loaded or not will not work, like usages
|
204
|
+
# of define? and it's friends.
|
205
|
+
#
|
206
|
+
# Also, normal caveats of using Kernel#autoload apply - you have to remember that before
|
207
|
+
# applying any monkey-patches to code using autoload, you'll have to reference the full constant
|
208
|
+
# to load the code before applying your patch!
|
209
|
+
|
210
|
+
def autoload_all(*paths)
|
211
|
+
paths.flatten!
|
212
|
+
return false if paths.empty?
|
213
|
+
require "pathname"
|
214
|
+
|
215
|
+
options = {:method => :autoload}
|
216
|
+
options.merge!(paths.pop) if paths.last.is_a?(Hash)
|
217
|
+
|
218
|
+
paths.each do |path|
|
219
|
+
require_all path, {:base_dir => path}.merge(options)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
# Performs autoloading relatively from the caller instead of using current working directory
|
224
|
+
def autoload_rel(*paths)
|
225
|
+
paths.flatten!
|
226
|
+
return false if paths.empty?
|
227
|
+
require "pathname"
|
228
|
+
|
229
|
+
options = {:method => :autoload}
|
230
|
+
options.merge!(paths.pop) if paths.last.is_a?(Hash)
|
231
|
+
|
232
|
+
source_directory = File.dirname caller.first.sub(/:\d+$/, '')
|
233
|
+
paths.each do |path|
|
234
|
+
file_path = Pathname.new(source_directory).join(path).to_s
|
235
|
+
require_all file_path, {:method => :autoload,
|
236
|
+
:base_dir => source_directory}.merge(options)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
private
|
241
|
+
|
242
|
+
def __autoload(file, full_path, options)
|
243
|
+
last_module = "Object" # default constant where namespaces are created into
|
244
|
+
begin
|
245
|
+
base_dir = Pathname.new(options[:base_dir]).realpath
|
246
|
+
rescue Errno::ENOENT
|
247
|
+
raise LoadError, ":base_dir doesn't exist at #{options[:base_dir]}"
|
248
|
+
end
|
249
|
+
Pathname.new(file).realpath.descend do |entry|
|
250
|
+
# skip until *entry* is same as desired directory
|
251
|
+
# or anything inside of it avoiding to create modules
|
252
|
+
# from the top-level directories
|
253
|
+
next if (entry <=> base_dir) < 0
|
254
|
+
|
255
|
+
# get the module into which a new module is created or
|
256
|
+
# autoload performed
|
257
|
+
mod = Object.class_eval(last_module)
|
258
|
+
|
259
|
+
without_ext = entry.basename(entry.extname).to_s
|
260
|
+
const = without_ext.split("_").map {|word| word.capitalize}.join
|
261
|
+
|
262
|
+
if entry.directory?
|
263
|
+
mod.class_eval "module #{const} end"
|
264
|
+
last_module += "::#{const}"
|
265
|
+
else
|
266
|
+
mod.class_eval do
|
267
|
+
puts "autoloading #{mod}::#{const} from #{full_path}" if $DEBUG
|
268
|
+
autoload const, full_path
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
end
|
275
|
+
|
276
|
+
include RequireAll
|