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 CHANGED
@@ -1,3 +1,7 @@
1
+ 1.0.1:
2
+
3
+ * Allow require_all to take a directory name as an argument
4
+
1
5
  1.0.0:
2
6
 
3
7
  * Initial release (was originally load_glob, converted to require_all which is
@@ -5,18 +5,25 @@
5
5
  #++
6
6
 
7
7
  module RequireAll
8
- # Load all files matching the given glob, handling dependencies between
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
- # require_all 'lib/**/*.rb'
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
- # Don't want to give it a glob? Just give it a list of files:
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
- files = [arg]
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, 'no such file to load -- #{arg}' if files.empty?
59
+ raise LoadError, "no such file to load -- #{arg}" if files.empty?
48
60
  end
49
61
  end
50
62
 
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  GEMSPEC = Gem::Specification.new do |s|
4
4
  s.name = "require_all"
5
- s.version = "1.0.0"
5
+ s.version = "1.0.1"
6
6
  s.authors = "Tony Arcieri"
7
7
  s.email = "tony@medioh.com"
8
8
  s.date = "2009-06-09"
@@ -19,22 +19,29 @@ describe "require_all" do
19
19
  end
20
20
 
21
21
  describe "syntactic sugar" do
22
- before :all 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 }
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 @base_dir + 'c.rb'
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 @file_list
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 *@file_list
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require_all
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Arcieri