require_all 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +5 -0
- data/README.textile +32 -15
- data/lib/require_all.rb +14 -1
- data/require_all.gemspec +2 -2
- data/spec/fixtures/relative/a.rb +2 -0
- data/spec/fixtures/relative/b/b.rb +5 -0
- data/spec/fixtures/relative/c/c.rb +2 -0
- data/spec/require_all_spec.rb +11 -1
- metadata +5 -2
data/CHANGES
CHANGED
data/README.textile
CHANGED
@@ -11,38 +11,55 @@ Wouldn't that be nice? Well, now you can!
|
|
11
11
|
|
12
12
|
<code>require 'require_all'</code>
|
13
13
|
|
14
|
-
You can
|
14
|
+
You can use require_all in a multitude of different ways.
|
15
15
|
|
16
|
-
|
16
|
+
The easiest way to use require_all is to just point it at a directory
|
17
|
+
containing a bunch of .rb files:
|
17
18
|
|
18
|
-
|
19
|
-
enumerate all the matching files and load them in the proper order. For
|
20
|
-
example, to load all the Ruby files under the 'lib' directory, just do:
|
19
|
+
<code>require_all 'lib'</code>
|
21
20
|
|
22
|
-
|
21
|
+
This will find all the .rb files under the lib directory (including all
|
22
|
+
subdirectories as well) and load them.
|
23
|
+
|
24
|
+
The proper order to in which to load them is determined automatically. If the
|
25
|
+
dependencies between the matched files are unresolvable, it will throw the
|
26
|
+
first unresolvable NameError.
|
23
27
|
|
24
|
-
|
25
|
-
throw the first unresolvable NameError.
|
28
|
+
You can also give it a glob, which will enumerate all the matching files:
|
26
29
|
|
27
|
-
|
30
|
+
<code>require_all 'lib/**/*.rb'</code>
|
28
31
|
|
29
|
-
|
32
|
+
It will also accept an array of files:
|
33
|
+
|
34
|
+
<code>require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file? f }</code>
|
30
35
|
|
31
36
|
Or if you want, just list the files directly as arguments:
|
32
37
|
|
33
38
|
<code>require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'</code>
|
39
|
+
|
40
|
+
Still have the require File.dirname(__FILE__) blues? The require_all gem also
|
41
|
+
provides a require_rel statement which requires files to relative to the
|
42
|
+
current file. So you can replace statements like:
|
43
|
+
|
44
|
+
<code>require File.dirname(__FILE__) + '/foobar'</code>
|
45
|
+
|
46
|
+
with just a simple require_rel:
|
47
|
+
|
48
|
+
<code>require_rel 'foobar'</code>
|
49
|
+
|
50
|
+
Even better, require_rel still has the full power of require_all, so you can
|
51
|
+
use require_rel to load entire directories of code too. If "foobar" is a
|
52
|
+
directory this will load all the .rb files found under that directory with
|
53
|
+
automagic dependency handling.
|
34
54
|
|
35
55
|
It's just that easy! Code loading shouldn't be hard.
|
36
56
|
|
37
57
|
h2. Methodology
|
38
58
|
|
39
59
|
I didn't invent the approach this gem uses. It was shamelessly stolen from
|
40
|
-
Merb
|
41
|
-
horrible ActiveSupport's dependencies.rb hijacking of const_missing and
|
42
|
-
someone described the approach Merb used to me. It was so simple and clean!
|
43
|
-
Here's how it works:
|
60
|
+
Merb (which apparently stole it from elsewhere). Here's how it works:
|
44
61
|
|
45
|
-
# Enumerate the files
|
62
|
+
# Enumerate the files to be loaded
|
46
63
|
# Try to load all of the files. If we encounter a NameError loading a
|
47
64
|
particular file, store that file in a "try to load it later" list.
|
48
65
|
# If all the files loaded, great, we're done! If not, go through the
|
data/lib/require_all.rb
CHANGED
@@ -47,7 +47,7 @@ module RequireAll
|
|
47
47
|
if stat.file?
|
48
48
|
files = [arg]
|
49
49
|
elsif stat.directory?
|
50
|
-
files = Dir.glob
|
50
|
+
files = Dir.glob File.join(arg, '**', '*.rb')
|
51
51
|
else
|
52
52
|
raise ArgumentError, "#{arg} isn't a file or directory"
|
53
53
|
end
|
@@ -55,6 +55,12 @@ module RequireAll
|
|
55
55
|
# If the stat failed, maybe we have a glob!
|
56
56
|
files = Dir.glob arg
|
57
57
|
|
58
|
+
# Maybe it's an .rb file and the .rb was omitted
|
59
|
+
if File.file?(arg + '.rb')
|
60
|
+
require(arg + '.rb')
|
61
|
+
return true
|
62
|
+
end
|
63
|
+
|
58
64
|
# If we ain't got no files, the glob failed
|
59
65
|
raise LoadError, "no such file to load -- #{arg}" if files.empty?
|
60
66
|
end
|
@@ -107,6 +113,13 @@ module RequireAll
|
|
107
113
|
|
108
114
|
true
|
109
115
|
end
|
116
|
+
|
117
|
+
# Works like require_all, but paths are relative to the caller rather than
|
118
|
+
# the current working directory
|
119
|
+
def require_rel(path)
|
120
|
+
source_directory = File.dirname caller.first.sub(/:\d+$/, '')
|
121
|
+
require_all File.join(source_directory, path)
|
122
|
+
end
|
110
123
|
end
|
111
124
|
|
112
125
|
include RequireAll
|
data/require_all.gemspec
CHANGED
@@ -2,10 +2,10 @@ require 'rubygems'
|
|
2
2
|
|
3
3
|
GEMSPEC = Gem::Specification.new do |s|
|
4
4
|
s.name = "require_all"
|
5
|
-
s.version = "1.0
|
5
|
+
s.version = "1.1.0"
|
6
6
|
s.authors = "Tony Arcieri"
|
7
7
|
s.email = "tony@medioh.com"
|
8
|
-
s.date = "2009-
|
8
|
+
s.date = "2009-07-18"
|
9
9
|
s.summary = "A wonderfully simple way to load your code"
|
10
10
|
s.platform = Gem::Platform::RUBY
|
11
11
|
|
data/spec/require_all_spec.rb
CHANGED
@@ -25,7 +25,7 @@ describe "require_all" do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "works like a drop-in require replacement" do
|
28
|
-
require_all(@base_dir + '/c
|
28
|
+
require_all(@base_dir + '/c').should be_true
|
29
29
|
end
|
30
30
|
|
31
31
|
it "accepts lists of files" do
|
@@ -44,4 +44,14 @@ describe "require_all" do
|
|
44
44
|
require_all("#{@base_dir}/").should be_true
|
45
45
|
end
|
46
46
|
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "require_rel" do
|
50
|
+
it "provides require_all functionality relative to the current file" do
|
51
|
+
require File.dirname(__FILE__) + '/fixtures/relative/b/b'
|
52
|
+
|
53
|
+
defined?(RelativeA).should == "constant"
|
54
|
+
defined?(RelativeB).should == "constant"
|
55
|
+
defined?(RelativeC).should == "constant"
|
56
|
+
end
|
47
57
|
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
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Arcieri
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-18 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,6 +25,9 @@ extra_rdoc_files:
|
|
25
25
|
- CHANGES
|
26
26
|
files:
|
27
27
|
- lib/require_all.rb
|
28
|
+
- spec/fixtures/relative/a.rb
|
29
|
+
- spec/fixtures/relative/b/b.rb
|
30
|
+
- spec/fixtures/relative/c/c.rb
|
28
31
|
- spec/fixtures/resolvable/a.rb
|
29
32
|
- spec/fixtures/resolvable/b.rb
|
30
33
|
- spec/fixtures/resolvable/c.rb
|