rdp-require_all 1.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,13 @@
1
+ 1.1.0:
2
+
3
+ * Add require_rel (require_all relative to the current file)
4
+ * Fix bug in auto-appending .rb ala require
5
+
6
+ 1.0.1:
7
+
8
+ * Allow require_all to take a directory name as an argument
9
+
10
+ 1.0.0:
11
+
12
+ * Initial release (was originally load_glob, converted to require_all which is
13
+ a lot cooler, seriously trust me)
data/LICENSE ADDED
@@ -0,0 +1,58 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see COPYING.txt file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under this terms.
43
+
44
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
45
+ files under the ./missing directory. See each file for the copying
46
+ condition.
47
+
48
+ 5. The scripts and library files supplied as input to or produced as
49
+ output from the software do not automatically fall under the
50
+ copyright of the software, but belong to whomever generated them,
51
+ and may be sold commercially, and may be aggregated with this
52
+ software.
53
+
54
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
55
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
56
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57
+ PURPOSE.
58
+
data/README.textile ADDED
@@ -0,0 +1,83 @@
1
+ h1. require_all
2
+
3
+ A wonderfully simple way to load your code.
4
+
5
+ Tired of futzing around with require statements everywhere, littering your code
6
+ with <code>require File.dirname(__FILE__)</code> crap? What if you could just
7
+ point something at a big directory full of code and have everything just
8
+ automagically load regardless of the dependency structure?
9
+
10
+ Wouldn't that be nice? Well, now you can!
11
+
12
+ <code>require 'require_all'</code>
13
+
14
+ You can use require_all in a multitude of different ways.
15
+
16
+ The easiest way to use require_all is to just point it at a directory
17
+ containing a bunch of .rb files:
18
+
19
+ <code>require_all 'lib'</code>
20
+
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.
27
+
28
+ You can also give it a glob, which will enumerate all the matching files:
29
+
30
+ <code>require_all 'lib/**/*.rb'</code>
31
+
32
+ It will also accept an array of files:
33
+
34
+ <code>require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file? f }</code>
35
+
36
+ Or if you want, just list the files directly as arguments:
37
+
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.
54
+
55
+ It's just that easy! Code loading shouldn't be hard.
56
+
57
+ h2. Methodology
58
+
59
+ I didn't invent the approach this gem uses. It was shamelessly stolen from
60
+ Merb (which apparently stole it from elsewhere). Here's how it works:
61
+
62
+ # Enumerate the files to be loaded
63
+ # Try to load all of the files. If we encounter a NameError loading a
64
+ particular file, store that file in a "try to load it later" list.
65
+ # If all the files loaded, great, we're done! If not, go through the
66
+ "try to load it later" list again rescuing NameErrors the same way.
67
+ # If we walk the whole "try to load it later" list and it doesn't shrink
68
+ at all, we've encountered an unresolvable dependency. In this case,
69
+ require_all will rethrow the first NameError it encountered.
70
+
71
+ h2. Questions? Comments? Concerns?
72
+
73
+ You can reach the author on github or freenode: "tarcieri"
74
+
75
+ Or by email: "tony@medioh.com":mailto:tony@medioh.com
76
+
77
+ Got issues with require_all to report? Post 'em here:
78
+
79
+ "Github Tracker":http://github.com/tarcieri/require_all/issues
80
+
81
+ h2. License
82
+
83
+ MIT (see the LICENSE file for details)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+
4
+ Dir['tasks/**/*.rake'].each { |task| load task }
5
+
6
+ task :default => :spec
7
+
8
+ task :clean do
9
+ %w[pkg coverage].each do |dir|
10
+ rm_rf dir
11
+ end
12
+ end
@@ -0,0 +1,140 @@
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
+ if args.size > 1
39
+ # Expand files below directories
40
+ files = args.map do |path|
41
+ if File.directory? path
42
+ Dir[File.join(path, '**', '*.rb')]
43
+ else
44
+ path
45
+ end
46
+ end.flatten
47
+ else
48
+ arg = args.first
49
+ begin
50
+ # Try assuming we're doing plain ol' require compat
51
+ stat = File.stat(arg)
52
+
53
+ if stat.file?
54
+ files = [arg]
55
+ elsif stat.directory?
56
+ files = Dir.glob File.join(arg, '**', '*.rb')
57
+ else
58
+ raise ArgumentError, "#{arg} isn't a file or directory"
59
+ end
60
+ rescue Errno::ENOENT
61
+ # If the stat failed, maybe we have a glob!
62
+ files = Dir.glob arg
63
+
64
+ # Maybe it's an .rb file and the .rb was omitted
65
+ if File.file?(arg + '.rb')
66
+ require(arg + '.rb')
67
+ return true
68
+ end
69
+
70
+ # If we ain't got no files, the glob failed
71
+ raise LoadError, "no such file to load -- #{arg}" if files.empty?
72
+ end
73
+ end
74
+
75
+ # If there's nothing to load, you're doing it wrong!
76
+ raise LoadError, "no files to load" if files.empty?
77
+
78
+ files.map! { |file| File.expand_path file }
79
+ files.sort!
80
+
81
+ begin
82
+ failed = []
83
+ first_name_error = nil
84
+
85
+ # Attempt to load each file, rescuing which ones raise NameError for
86
+ # undefined constants. Keep trying to successively reload files that
87
+ # previously caused NameErrors until they've all been loaded or no new
88
+ # files can be loaded, indicating unresolvable dependencies.
89
+ files.each do |file|
90
+ begin
91
+ require file
92
+ rescue NameError => ex
93
+ failed << file
94
+ first_name_error ||= ex
95
+ rescue ArgumentError => ex
96
+ # Work around ActiveSuport freaking out... *sigh*
97
+ #
98
+ # ActiveSupport sometimes throws these exceptions and I really
99
+ # have no idea why. Code loading will work successfully if these
100
+ # exceptions are swallowed, although I've run into strange
101
+ # nondeterministic behaviors with constants mysteriously vanishing.
102
+ # I've gone spelunking through dependencies.rb looking for what
103
+ # exactly is going on, but all I ended up doing was making my eyes
104
+ # bleed.
105
+ #
106
+ # FIXME: If you can understand ActiveSupport's dependencies.rb
107
+ # better than I do I would *love* to find a better solution
108
+ raise unless ex.message["is not missing constant"]
109
+
110
+ STDERR.puts "Warning: require_all swallowed ActiveSupport 'is not missing constant' error"
111
+ STDERR.puts ex.backtrace[0..9]
112
+ end
113
+ end
114
+
115
+ # If this pass didn't resolve any NameErrors, we've hit an unresolvable
116
+ # dependency, so raise one of the exceptions we encountered.
117
+ if failed.size == files.size
118
+ raise first_name_error
119
+ else
120
+ files = failed
121
+ end
122
+ end until failed.empty?
123
+
124
+ true
125
+ end
126
+
127
+ # Works like require_all, but paths are relative to the caller rather than
128
+ # the current working directory
129
+ def require_rel(*paths)
130
+ # Handle passing an array as an argument
131
+ paths.flatten!
132
+
133
+ source_directory = File.dirname caller.first.sub(/:\d+$/, '')
134
+ paths.each do |path|
135
+ require_all File.join(source_directory, path)
136
+ end
137
+ end
138
+ end
139
+
140
+ include RequireAll
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+
3
+ GEMSPEC = Gem::Specification.new do |s|
4
+ s.name = "rdp-require_all"
5
+ s.version = "1.1.0.1"
6
+ s.authors = "Tony Arcieri"
7
+ s.email = "tony@medioh.com"
8
+ s.date = "2009-07-18"
9
+ s.summary = "A wonderfully simple way to load your code"
10
+ s.platform = Gem::Platform::RUBY
11
+
12
+ # Gem contents
13
+ s.files = Dir.glob("{lib,spec}/**/*") + ['Rakefile', 'require_all.gemspec']
14
+
15
+ # RubyForge info
16
+ s.homepage = "http://github.com/tarcieri/require_all"
17
+ s.rubyforge_project = "codeforpeople"
18
+
19
+ # RDoc settings
20
+ s.has_rdoc = true
21
+ s.rdoc_options = %w(--title require_all --main README.textile --line-numbers)
22
+ s.extra_rdoc_files = ["LICENSE", "README.textile", "CHANGES"]
23
+
24
+ # Extensions
25
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
26
+ end
@@ -0,0 +1,2 @@
1
+ class RelativeA
2
+ end
@@ -0,0 +1,5 @@
1
+ require_rel '../a'
2
+ require_rel '../c'
3
+
4
+ class RelativeB
5
+ end
@@ -0,0 +1,2 @@
1
+ class RelativeC
2
+ end
@@ -0,0 +1 @@
1
+ class A < C; end
@@ -0,0 +1 @@
1
+ class B < A; end
@@ -0,0 +1 @@
1
+ class C; end
@@ -0,0 +1 @@
1
+ class D < C; end
@@ -0,0 +1 @@
1
+ class A < C; end
@@ -0,0 +1 @@
1
+ class B < A; end
@@ -0,0 +1 @@
1
+ class C < Nonexistent; end
@@ -0,0 +1 @@
1
+ class D < C; end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../lib/require_all.rb'
2
+
3
+ describe "require_all" do
4
+ describe "dependency resolution" do
5
+ it "handles load ordering when dependencies are resolvable" do
6
+ require_all File.dirname(__FILE__) + '/fixtures/resolvable/*.rb'
7
+
8
+ defined?(A).should == "constant"
9
+ defined?(B).should == "constant"
10
+ defined?(C).should == "constant"
11
+ defined?(D).should == "constant"
12
+ end
13
+
14
+ it "raises NameError if dependencies can't be resolved" do
15
+ proc do
16
+ require_all File.dirname(__FILE__) + '/fixtures/unresolvable/*.rb'
17
+ end.should raise_error(NameError)
18
+ end
19
+ end
20
+
21
+ describe "syntactic sugar" do
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
+ end
26
+
27
+ it "works like a drop-in require replacement" do
28
+ require_all(@base_dir + '/c').should be_true
29
+ end
30
+
31
+ it "accepts lists of files" do
32
+ require_all(@file_list).should be_true
33
+ end
34
+
35
+ it "is totally cool with a splatted list of arguments" do
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
45
+ end
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
57
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rdp-require_all
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tony Arcieri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-18 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: tony@medioh.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.textile
25
+ - CHANGES
26
+ files:
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
31
+ - spec/fixtures/resolvable/a.rb
32
+ - spec/fixtures/resolvable/b.rb
33
+ - spec/fixtures/resolvable/c.rb
34
+ - spec/fixtures/resolvable/d.rb
35
+ - spec/fixtures/unresolvable/a.rb
36
+ - spec/fixtures/unresolvable/b.rb
37
+ - spec/fixtures/unresolvable/c.rb
38
+ - spec/fixtures/unresolvable/d.rb
39
+ - spec/require_all_spec.rb
40
+ - Rakefile
41
+ - require_all.gemspec
42
+ - LICENSE
43
+ - README.textile
44
+ - CHANGES
45
+ has_rdoc: true
46
+ homepage: http://github.com/tarcieri/require_all
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --title
52
+ - require_all
53
+ - --main
54
+ - README.textile
55
+ - --line-numbers
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: codeforpeople
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A wonderfully simple way to load your code
77
+ test_files: []
78
+