require_all 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +4 -0
- data/lib/require_all.rb +24 -12
- data/require_all.gemspec +1 -1
- data/spec/require_all_spec.rb +14 -7
- metadata +1 -1
data/CHANGES
CHANGED
data/lib/require_all.rb
CHANGED
@@ -5,18 +5,25 @@
|
|
5
5
|
#++
|
6
6
|
|
7
7
|
module RequireAll
|
8
|
-
#
|
9
|
-
# the files gracefully
|
10
|
-
# One of the easiest ways to require_all is to give it a glob, which will
|
11
|
-
# enumerate all the matching files and load them in the proper order. For
|
12
|
-
# example, to load all the Ruby files under the 'lib' directory, just do:
|
8
|
+
# A wonderfully simple way to load your code.
|
13
9
|
#
|
14
|
-
#
|
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.
|
15
18
|
#
|
16
19
|
# If the dependencies between the matched files are unresolvable, it will
|
17
20
|
# throw the first unresolvable NameError.
|
18
21
|
#
|
19
|
-
#
|
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:
|
20
27
|
#
|
21
28
|
# require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }
|
22
29
|
#
|
@@ -24,8 +31,6 @@ module RequireAll
|
|
24
31
|
#
|
25
32
|
# require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
|
26
33
|
#
|
27
|
-
# It's just that easy! Code loading shouldn't be hard, especially in a language
|
28
|
-
# as versatile as ruby.
|
29
34
|
def require_all(*args)
|
30
35
|
# Handle passing an array as an argument
|
31
36
|
args = args.flatten
|
@@ -37,14 +42,21 @@ module RequireAll
|
|
37
42
|
arg = args.first
|
38
43
|
begin
|
39
44
|
# Try assuming we're doing plain ol' require compat
|
40
|
-
File.stat(arg)
|
41
|
-
|
45
|
+
stat = File.stat(arg)
|
46
|
+
|
47
|
+
if stat.file?
|
48
|
+
files = [arg]
|
49
|
+
elsif stat.directory?
|
50
|
+
files = Dir.glob "#{arg}/**/*.rb"
|
51
|
+
else
|
52
|
+
raise ArgumentError, "#{arg} isn't a file or directory"
|
53
|
+
end
|
42
54
|
rescue Errno::ENOENT
|
43
55
|
# If the stat failed, maybe we have a glob!
|
44
56
|
files = Dir.glob arg
|
45
57
|
|
46
58
|
# If we ain't got no files, the glob failed
|
47
|
-
raise LoadError,
|
59
|
+
raise LoadError, "no such file to load -- #{arg}" if files.empty?
|
48
60
|
end
|
49
61
|
end
|
50
62
|
|
data/require_all.gemspec
CHANGED
data/spec/require_all_spec.rb
CHANGED
@@ -19,22 +19,29 @@ describe "require_all" do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "syntactic sugar" do
|
22
|
-
before :
|
23
|
-
@base_dir = File.dirname(__FILE__) + '/fixtures/resolvable
|
24
|
-
@file_list = ['b.rb', 'c.rb', 'a.rb', 'd.rb'].map { |f| @base_dir
|
22
|
+
before :each do
|
23
|
+
@base_dir = File.dirname(__FILE__) + '/fixtures/resolvable'
|
24
|
+
@file_list = ['b.rb', 'c.rb', 'a.rb', 'd.rb'].map { |f| "#{@base_dir}/#{f}" }
|
25
25
|
end
|
26
26
|
|
27
27
|
it "works like a drop-in require replacement" do
|
28
|
-
require_all
|
29
|
-
defined?(C).should == "constant"
|
28
|
+
require_all(@base_dir + '/c.rb').should be_true
|
30
29
|
end
|
31
30
|
|
32
31
|
it "accepts lists of files" do
|
33
|
-
require_all
|
32
|
+
require_all(@file_list).should be_true
|
34
33
|
end
|
35
34
|
|
36
35
|
it "is totally cool with a splatted list of arguments" do
|
37
|
-
require_all
|
36
|
+
require_all(*@file_list).should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "will load all .rb files under a directory without a trailing slash" do
|
40
|
+
require_all(@base_dir).should be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "will load all .rb files under a directory with a trailing slash" do
|
44
|
+
require_all("#{@base_dir}/").should be_true
|
38
45
|
end
|
39
46
|
end
|
40
47
|
end
|