load_glob 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,3 @@
1
+ 1.0.0:
2
+
3
+ * Initial release
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,56 @@
1
+ h1. load_glob
2
+
3
+ Tired of futzing around with require statements everywhere, littering your code
4
+ with <code>require File.dirname(__FILE__)</code> crap? What if you could just
5
+ point something at a big directory full of code and have everything just
6
+ automagically load regardless of the dependency structure?
7
+
8
+ Wouldn't that be nice? Well, now you can!
9
+
10
+ <code>require 'load_glob'</code>
11
+
12
+ This gem defines a single method on main:
13
+
14
+ <code>load_glob(glob)</code>
15
+
16
+ which will enumerate the files in the given glob, determining the proper
17
+ order to load them in. So, to load all the Ruby files under the 'lib'
18
+ directory, just do:
19
+
20
+ <code>load_glob 'lib/**/*.rb'</code>
21
+
22
+ If the dependencies are unresolvable, it will throw the first unresolvable
23
+ NameError.
24
+
25
+ That's it!
26
+
27
+ h2. Methodology
28
+
29
+ I didn't invent the approach this gem uses. It was shamelessly stolen from
30
+ Merb. Once upon a time at MountainWest RubyConf we were discussing how
31
+ horrible ActiveSupport's dependencies.rb hijacking of const_missing and
32
+ someone described the approach Merb used to me. It was so simple and clean!
33
+ Here's how it works:
34
+
35
+ # Enumerate the files in the glob
36
+ # Try to load all of the files. If we encounter a NameError loading a
37
+ particular file, store that file in a "try to load it later" list.
38
+ # If all the files loaded, great, we're done! If not, go through the
39
+ "try to load it later" list again rescuing NameErrors the same way.
40
+ # If we walk the whole "try to load it later" list and it doesn't shrink
41
+ at all, we've encountered an unresolvable dependency. In this case,
42
+ load_glob will rethrow the first NameError it encountered.
43
+
44
+ h2. Questions? Concerns?
45
+
46
+ You can reach the author on github or freenode: "tarcieri"
47
+
48
+ Or by email: tony@medioh.com
49
+
50
+ The Github issue tracker is here:
51
+
52
+ http://github.com/tarcieri/load_glob/issues
53
+
54
+ h2. License
55
+
56
+ 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
data/lib/load_glob.rb ADDED
@@ -0,0 +1,59 @@
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 LoadGlob
8
+ # Load all files matching the given glob, handling dependencies between
9
+ # the files gracefully
10
+ def load_glob(glob)
11
+ files = Dir[glob].map { |file| File.expand_path file }
12
+
13
+ begin
14
+ failed = []
15
+ first_name_error = nil
16
+
17
+ # Attempt to load each file, rescuing which ones raise NameError for
18
+ # undefined constants. Keep trying to successively reload files that
19
+ # previously caused NameErrors until they've all been loaded or no new
20
+ # files can be loaded, indicating unresolvable dependencies.
21
+ files.each do |file|
22
+ begin
23
+ require file
24
+ rescue NameError => ex
25
+ failed << file
26
+ first_name_error ||= ex
27
+ rescue ArgumentError => ex
28
+ # Work around ActiveSuport freaking out... *sigh*
29
+ #
30
+ # ActiveSupport sometimes throws these exceptions and I really
31
+ # have no idea why. Code loading will work successfully if these
32
+ # exceptions are swallowed, although I've run into strange
33
+ # nondeterministic behaviors with constants mysteriously vanishing.
34
+ # I've gone spelunking through dependencies.rb looking for what
35
+ # exactly is going on, but all I ended up doing was making my eyes
36
+ # bleed.
37
+ #
38
+ # FIXME: If you can understand ActiveSupport's dependencies.rb
39
+ # better than I do I would *love* to find a better solution
40
+ raise ex unless ex["is not missing constant"]
41
+ STDERR.puts "Warning: load_glob swallowed ActiveSupport 'is not missing constant' error"
42
+ STDERR.puts ex.backtrace[0..9]
43
+ end
44
+ end
45
+
46
+ # If this pass didn't resolve any NameErrors, we've hit an unresolvable
47
+ # dependency, so raise one of the exceptions we encountered.
48
+ if failed.size == files.size
49
+ raise first_name_error
50
+ else
51
+ files = failed
52
+ end
53
+ end until failed.empty?
54
+
55
+ true
56
+ end
57
+ end
58
+
59
+ include LoadGlob
data/load_glob.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+
3
+ GEMSPEC = Gem::Specification.new do |s|
4
+ s.name = "load_glob"
5
+ s.version = "1.0.0"
6
+ s.authors = "Tony Arcieri"
7
+ s.email = "tony@medioh.com"
8
+ s.date = "2009-06-08"
9
+ s.summary = "Load all files matching a given glob, resolving dependencies automagically"
10
+ s.platform = Gem::Platform::RUBY
11
+
12
+ # Gem contents
13
+ s.files = Dir.glob("{lib,spec}/**/*") + ['Rakefile', 'load_glob.gemspec']
14
+
15
+ # RubyForge info
16
+ s.homepage = "http://github.com/tarcieri/load_glob"
17
+ s.rubyforge_project = "codeforpeople"
18
+
19
+ # RDoc settings
20
+ s.has_rdoc = true
21
+ s.rdoc_options = %w(--title load_glob --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 @@
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,18 @@
1
+ require File.dirname(__FILE__) + '/../lib/load_glob.rb'
2
+
3
+ describe "load_glob" do
4
+ it "handles load ordering when dependencies are resolvable" do
5
+ load_glob File.dirname(__FILE__) + '/fixtures/resolvable/*.rb'
6
+
7
+ defined?(A).should == "constant"
8
+ defined?(B).should == "constant"
9
+ defined?(C).should == "constant"
10
+ defined?(D).should == "constant"
11
+ end
12
+
13
+ it "raises NameError if dependencies can't be resolved" do
14
+ proc do
15
+ load_glob File.dirname(__FILE__) + '/fixtures/unresolvable/*.rb'
16
+ end.should raise_error(NameError)
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: load_glob
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Arcieri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-08 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/load_glob.rb
28
+ - spec/fixtures/resolvable/a.rb
29
+ - spec/fixtures/resolvable/b.rb
30
+ - spec/fixtures/resolvable/c.rb
31
+ - spec/fixtures/resolvable/d.rb
32
+ - spec/fixtures/unresolvable/a.rb
33
+ - spec/fixtures/unresolvable/b.rb
34
+ - spec/fixtures/unresolvable/c.rb
35
+ - spec/fixtures/unresolvable/d.rb
36
+ - spec/load_glob_spec.rb
37
+ - Rakefile
38
+ - load_glob.gemspec
39
+ - LICENSE
40
+ - README.textile
41
+ - CHANGES
42
+ has_rdoc: true
43
+ homepage: http://github.com/tarcieri/load_glob
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --title
49
+ - load_glob
50
+ - --main
51
+ - README.textile
52
+ - --line-numbers
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: codeforpeople
70
+ rubygems_version: 1.3.4
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Load all files matching a given glob, resolving dependencies automagically
74
+ test_files: []
75
+