require_all 1.5.0 → 2.0.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.
- checksums.yaml +4 -4
- data/.travis.yml +16 -16
- data/CHANGES +55 -45
- data/Gemfile +4 -4
- data/LICENSE +1 -1
- data/README.md +123 -128
- data/lib/require_all.rb +21 -56
- data/require_all.gemspec +1 -1
- data/spec/autoload_shared.rb +8 -8
- data/spec/autoload_spec.rb +12 -14
- data/spec/fixtures/error/a.rb +3 -0
- data/spec/fixtures/resolvable/a.rb +2 -0
- data/spec/load_spec.rb +6 -6
- data/spec/require_spec.rb +22 -9
- data/spec/spec_helper.rb +38 -30
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e7c0c747bfdb3c3c59c6561700e67736f43949f
|
4
|
+
data.tar.gz: 4e83ce18c619ab69e6d663afe349f8d2c3a112b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e409b3c92b49698fe477322570fb6965956bd5ec75683afd341ae2a483eb621f87b7c5db39642cf6768895bb31ea75d3f18bcd6501ba6819bd1731ad48737311
|
7
|
+
data.tar.gz: 96ce20ca7ef905cfaa035e98c2e1a02aaff7c5fda41455fc59ba90654f568deb107d072a8773c0ae7a550eef0c41be0a71bae8ae1175803360583f12c4fe8b15
|
data/.travis.yml
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
rvm:
|
2
|
-
- 2.
|
3
|
-
- 2.
|
4
|
-
- 2.
|
5
|
-
- jruby-19mode
|
6
|
-
- rbx-3
|
7
|
-
- ruby-head
|
8
|
-
notifications:
|
9
|
-
recipients:
|
10
|
-
- jarmo.p@gmail.com
|
11
|
-
matrix:
|
12
|
-
allow_failures:
|
13
|
-
- rvm: ruby-head
|
14
|
-
- rvm: jruby-19mode
|
15
|
-
- rvm: rbx-3
|
16
|
-
|
1
|
+
rvm:
|
2
|
+
- 2.5.0
|
3
|
+
- 2.4.3
|
4
|
+
- 2.3.6
|
5
|
+
- jruby-19mode
|
6
|
+
- rbx-3
|
7
|
+
- ruby-head
|
8
|
+
notifications:
|
9
|
+
recipients:
|
10
|
+
- jarmo.p@gmail.com
|
11
|
+
matrix:
|
12
|
+
allow_failures:
|
13
|
+
- rvm: ruby-head
|
14
|
+
- rvm: jruby-19mode
|
15
|
+
- rvm: rbx-3
|
16
|
+
|
data/CHANGES
CHANGED
@@ -1,45 +1,55 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
* Merged PR #
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
*
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
*
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
1
|
+
2.0.0:
|
2
|
+
|
3
|
+
* Merged PR #24 (https://github.com/jarmo/require_all/pull/24)
|
4
|
+
Prior to version 2, RequireAll attempted to automatically resolve dependencies between files, thus
|
5
|
+
allowing them to be required in any order. Whilst convenient, the approach used (of rescuing
|
6
|
+
`NameError`s and later retrying files that failed to load) was fundamentally unsafe and can result
|
7
|
+
in incorrect behaviour (for example issue #8, plus more detail and discussion in #21).
|
8
|
+
|
9
|
+
Thanks to Joe Horsnell (@joehorsnell)
|
10
|
+
|
11
|
+
1.5.0:
|
12
|
+
|
13
|
+
* Merged PR #13 (https://github.com/jarmo/require_all/pull/13).
|
14
|
+
* Merged PR #18 (https://github.com/jarmo/require_all/pull/18).
|
15
|
+
|
16
|
+
1.4.0:
|
17
|
+
|
18
|
+
* License is now correctly as MIT. Thanks to Eric Kessler for pull request #16.
|
19
|
+
|
20
|
+
1.3.3:
|
21
|
+
|
22
|
+
* Support empty directories without crashing. Issue #11. Thanks to Eric Kessler.
|
23
|
+
|
24
|
+
1.3.2:
|
25
|
+
|
26
|
+
* Add license to gemspec.
|
27
|
+
|
28
|
+
1.3.1:
|
29
|
+
|
30
|
+
* README improvements.
|
31
|
+
|
32
|
+
1.3.0:
|
33
|
+
|
34
|
+
* Make everything work with Ruby 1.9 and 2.0. Awesome! Thanks to Aaron Klaassen.
|
35
|
+
|
36
|
+
1.2.0:
|
37
|
+
|
38
|
+
* Add load_all, and load_rel which behave similarly to require_all/require_rel except that Kernel#load is used
|
39
|
+
* Add autoload_all and autoload_rel (see README and/or specs for examples of usage)
|
40
|
+
* Minor bug fixes
|
41
|
+
* Improved specs
|
42
|
+
|
43
|
+
1.1.0:
|
44
|
+
|
45
|
+
* Add require_rel (require_all relative to the current file)
|
46
|
+
* Fix bug in auto-appending .rb ala require
|
47
|
+
|
48
|
+
1.0.1:
|
49
|
+
|
50
|
+
* Allow require_all to take a directory name as an argument
|
51
|
+
|
52
|
+
1.0.0:
|
53
|
+
|
54
|
+
* Initial release (was originally load_glob, converted to require_all which is
|
55
|
+
a lot cooler, seriously trust me)
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gemspec
|
4
|
-
gem 'coveralls', :
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
gem 'coveralls', require: false
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,128 +1,123 @@
|
|
1
|
-
# require_all
|
2
|
-
[](http://badge.fury.io/rb/require_all)
|
3
|
-
[](http://travis-ci.org/jarmo/require_all)
|
4
|
-
[](https://coveralls.io/r/jarmo/require_all)
|
5
|
-
|
6
|
-
A wonderfully simple way to load your code.
|
7
|
-
|
8
|
-
Tired of futzing around with `require` statements everywhere, littering your code
|
9
|
-
with `require File.dirname(__FILE__)` crap? What if you could just
|
10
|
-
point something at a big directory full of code and have everything just
|
11
|
-
automagically load
|
12
|
-
|
13
|
-
Wouldn't that be nice? Well, now you can!
|
14
|
-
|
15
|
-
## Installation
|
16
|
-
|
17
|
-
Add this line to your application's Gemfile:
|
18
|
-
|
19
|
-
gem 'require_all'
|
20
|
-
|
21
|
-
And then execute:
|
22
|
-
|
23
|
-
$ bundle
|
24
|
-
|
25
|
-
Or install it yourself as:
|
26
|
-
|
27
|
-
$ gem install require_all
|
28
|
-
|
29
|
-
## Usage
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
require 'require_all'
|
33
|
-
|
34
|
-
# load all ruby files in the directory "lib" and its subdirectories
|
35
|
-
require_all 'lib'
|
36
|
-
|
37
|
-
# or load all files by using glob
|
38
|
-
require_all 'lib/**/*.rb'
|
39
|
-
|
40
|
-
# or load files in an Array
|
41
|
-
require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file? f }
|
42
|
-
|
43
|
-
# or load manually specified files
|
44
|
-
require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
|
45
|
-
```
|
46
|
-
|
47
|
-
You can also load files relative to the current file by using `require_rel`:
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
# Instead of
|
51
|
-
require File.dirname(__FILE__) + '/foobar'
|
52
|
-
|
53
|
-
# you can do simply like this
|
54
|
-
require_rel 'foobar'
|
55
|
-
```
|
56
|
-
|
57
|
-
You can give all the same argument types to the `require_rel` as for `require_all`.
|
58
|
-
|
59
|
-
It is recommended to use `require_rel` instead of `require_all` since it will require files relatively
|
60
|
-
to the current file (`__FILE__`) as opposed to loading files relative from the working directory.
|
61
|
-
|
62
|
-
`load_all` and `load_rel` methods also exist to use `Kernel#load` instead of `Kernel#require`!
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
## License
|
125
|
-
|
126
|
-
Jarmo Pertman
|
127
|
-
|
128
|
-
MIT (see the LICENSE file for details)
|
1
|
+
# require_all
|
2
|
+
[](http://badge.fury.io/rb/require_all)
|
3
|
+
[](http://travis-ci.org/jarmo/require_all)
|
4
|
+
[](https://coveralls.io/r/jarmo/require_all)
|
5
|
+
|
6
|
+
A wonderfully simple way to load your code.
|
7
|
+
|
8
|
+
Tired of futzing around with `require` statements everywhere, littering your code
|
9
|
+
with `require File.dirname(__FILE__)` crap? What if you could just
|
10
|
+
point something at a big directory full of code and have everything just
|
11
|
+
automagically load?
|
12
|
+
|
13
|
+
Wouldn't that be nice? Well, now you can!
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'require_all'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install require_all
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'require_all'
|
33
|
+
|
34
|
+
# load all ruby files in the directory "lib" and its subdirectories
|
35
|
+
require_all 'lib'
|
36
|
+
|
37
|
+
# or load all files by using glob
|
38
|
+
require_all 'lib/**/*.rb'
|
39
|
+
|
40
|
+
# or load files in an Array
|
41
|
+
require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file? f }
|
42
|
+
|
43
|
+
# or load manually specified files
|
44
|
+
require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
|
45
|
+
```
|
46
|
+
|
47
|
+
You can also load files relative to the current file by using `require_rel`:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# Instead of
|
51
|
+
require File.dirname(__FILE__) + '/foobar'
|
52
|
+
|
53
|
+
# you can do simply like this
|
54
|
+
require_rel 'foobar'
|
55
|
+
```
|
56
|
+
|
57
|
+
You can give all the same argument types to the `require_rel` as for `require_all`.
|
58
|
+
|
59
|
+
It is recommended to use `require_rel` instead of `require_all` since it will require files relatively
|
60
|
+
to the current file (`__FILE__`) as opposed to loading files relative from the working directory.
|
61
|
+
|
62
|
+
`load_all` and `load_rel` methods also exist to use `Kernel#load` instead of `Kernel#require`!
|
63
|
+
|
64
|
+
Files are required in alphabetical order and if there are files in nested directories, they are
|
65
|
+
required depth-first. If a `NameError` caused by a reference to an uninitialised constant is
|
66
|
+
encountered during the requiring process, then a `RequireAll::LoadError` will be thrown,
|
67
|
+
indicating the file that needs the dependency adding to.
|
68
|
+
|
69
|
+
## autoload_all
|
70
|
+
|
71
|
+
This library also includes methods for performing `autoload` - what a bargain!
|
72
|
+
|
73
|
+
Similar syntax is used as for `require_(all|rel)` and `load_(all|rel)` methods with some caveats:
|
74
|
+
|
75
|
+
* Directory and file names have to reflect namespaces and/or constant names:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# lib/dir1/dir2/my_file.rb
|
79
|
+
module Dir1
|
80
|
+
module Dir2
|
81
|
+
class MyFile
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# lib/loader.rb
|
87
|
+
autoload_all File.dirname(__FILE__) + "/dir1"
|
88
|
+
```
|
89
|
+
|
90
|
+
* A `base_dir` option has to be specified if loading directories or files from some other location
|
91
|
+
than top-level directory:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
# lib/dir1/other_file.rb
|
95
|
+
autoload_all File.dirname(__FILE__) + "/dir2/my_file.rb",
|
96
|
+
base_dir: File.dirname(__FILE__) + "/../dir1"
|
97
|
+
```
|
98
|
+
|
99
|
+
* All namespaces will be created dynamically by `autoload_all` - this means that `defined?(Dir1)` will
|
100
|
+
return `"constant"` even if `my_file.rb` is not yet loaded!
|
101
|
+
|
102
|
+
Of course there's also an `autoload_rel` method:
|
103
|
+
```ruby
|
104
|
+
autoload_rel "dir2/my_file.rb", base_dir: File.dirname(__FILE__) + "/../dir1"
|
105
|
+
```
|
106
|
+
|
107
|
+
If having some problems with `autoload_all` or `autoload_rel` then set `$DEBUG=true` to see how files
|
108
|
+
are mapped to their respective modules and classes.
|
109
|
+
|
110
|
+
## Version compatibility and upgrading
|
111
|
+
|
112
|
+
As of version 2, RequireAll will raise a `RequireAll::LoadError` if it encounters a `NameError`
|
113
|
+
caused by a reference to an uninitialised constant during the requiring process. As such, it is not
|
114
|
+
backwards compatible with version 1.x, but simple to upgrade by adding any requires to load
|
115
|
+
dependencies in files that need them. See [CHANGES](CHANGES) for more details.
|
116
|
+
|
117
|
+
## Questions? Comments? Concerns?
|
118
|
+
|
119
|
+
You can reach the author on github or by email [jarmo.p@gmail.com](mailto:jarmo.p@gmail.com)
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
MIT (see the [LICENSE](LICENSE) file for details)
|
data/lib/require_all.rb
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
#++
|
6
6
|
|
7
7
|
module RequireAll
|
8
|
+
LoadError = Class.new(::LoadError)
|
9
|
+
|
8
10
|
# A wonderfully simple way to load your code.
|
9
11
|
#
|
10
12
|
# The easiest way to use require_all is to just point it at a directory
|
@@ -14,10 +16,9 @@ module RequireAll
|
|
14
16
|
# require_all 'lib'
|
15
17
|
#
|
16
18
|
# 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
|
#
|
19
|
-
# If
|
20
|
-
#
|
20
|
+
# If a file required by require_all references a constant that is not yet
|
21
|
+
# loaded, a RequireAll::LoadError will be thrown.
|
21
22
|
#
|
22
23
|
# You can also give it a glob, which will enumerate all the matching files:
|
23
24
|
#
|
@@ -35,7 +36,7 @@ module RequireAll
|
|
35
36
|
# Handle passing an array as an argument
|
36
37
|
args.flatten!
|
37
38
|
|
38
|
-
options = {:
|
39
|
+
options = {method: :require}
|
39
40
|
options.merge!(args.pop) if args.last.is_a?(Hash)
|
40
41
|
|
41
42
|
if args.empty?
|
@@ -92,51 +93,15 @@ module RequireAll
|
|
92
93
|
return true
|
93
94
|
end
|
94
95
|
|
95
|
-
files.map
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
# Attempt to load each file, rescuing which ones raise NameError for
|
103
|
-
# undefined constants. Keep trying to successively reload files that
|
104
|
-
# previously caused NameErrors until they've all been loaded or no new
|
105
|
-
# files can be loaded, indicating unresolvable dependencies.
|
106
|
-
files.each do |file_|
|
107
|
-
begin
|
108
|
-
__require(options[:method], file_)
|
109
|
-
rescue NameError => ex
|
110
|
-
failed << file_
|
111
|
-
first_name_error ||= ex
|
112
|
-
rescue ArgumentError => ex
|
113
|
-
# Work around ActiveSuport freaking out... *sigh*
|
114
|
-
#
|
115
|
-
# ActiveSupport sometimes throws these exceptions and I really
|
116
|
-
# have no idea why. Code loading will work successfully if these
|
117
|
-
# exceptions are swallowed, although I've run into strange
|
118
|
-
# nondeterministic behaviors with constants mysteriously vanishing.
|
119
|
-
# I've gone spelunking through dependencies.rb looking for what
|
120
|
-
# exactly is going on, but all I ended up doing was making my eyes
|
121
|
-
# bleed.
|
122
|
-
#
|
123
|
-
# FIXME: If you can understand ActiveSupport's dependencies.rb
|
124
|
-
# better than I do I would *love* to find a better solution
|
125
|
-
raise unless ex.message["is not missing constant"]
|
126
|
-
|
127
|
-
STDERR.puts "Warning: require_all swallowed ActiveSupport 'is not missing constant' error"
|
128
|
-
STDERR.puts ex.backtrace[0..9]
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
# If this pass didn't resolve any NameErrors, we've hit an unresolvable
|
133
|
-
# dependency, so raise one of the exceptions we encountered.
|
134
|
-
if failed.size == files.size
|
135
|
-
raise first_name_error
|
136
|
-
else
|
137
|
-
files = failed
|
96
|
+
files.map { |file_| File.expand_path file_ }.sort.each do |file_|
|
97
|
+
begin
|
98
|
+
__require(options[:method], file_)
|
99
|
+
rescue NameError => e
|
100
|
+
# Only wrap NameError exceptions for uninitialized constants
|
101
|
+
raise e unless e.instance_of?(NameError) && e.message.include?('uninitialized constant')
|
102
|
+
raise LoadError, "Could not require #{file_} (#{e}). Please require the necessary files"
|
138
103
|
end
|
139
|
-
end
|
104
|
+
end
|
140
105
|
|
141
106
|
true
|
142
107
|
end
|
@@ -156,7 +121,7 @@ module RequireAll
|
|
156
121
|
|
157
122
|
# Loads all files like require_all instead of requiring
|
158
123
|
def load_all(*paths)
|
159
|
-
require_all paths, :
|
124
|
+
require_all paths, method: :load
|
160
125
|
end
|
161
126
|
|
162
127
|
# Loads all files by using relative paths of the caller rather than
|
@@ -167,7 +132,7 @@ module RequireAll
|
|
167
132
|
|
168
133
|
source_directory = File.dirname caller.first.sub(/:\d+$/, '')
|
169
134
|
paths.each do |path|
|
170
|
-
require_all File.join(source_directory, path), :
|
135
|
+
require_all File.join(source_directory, path), method: :load
|
171
136
|
end
|
172
137
|
end
|
173
138
|
|
@@ -197,7 +162,7 @@ module RequireAll
|
|
197
162
|
# For example loading only my_file.rb from dir1/dir2 with autoload_all:
|
198
163
|
#
|
199
164
|
# autoload_all File.dirname(__FILE__) + '/dir1/dir2/my_file',
|
200
|
-
# :
|
165
|
+
# base_dir: File.dirname(__FILE__) + '/dir1'
|
201
166
|
#
|
202
167
|
# WARNING: All modules will be created even if files themselves aren't loaded yet, meaning
|
203
168
|
# that all the code which depends of the modules being loaded or not will not work, like usages
|
@@ -212,11 +177,11 @@ module RequireAll
|
|
212
177
|
return false if paths.empty?
|
213
178
|
require "pathname"
|
214
179
|
|
215
|
-
options = {:
|
180
|
+
options = {method: :autoload}
|
216
181
|
options.merge!(paths.pop) if paths.last.is_a?(Hash)
|
217
182
|
|
218
183
|
paths.each do |path|
|
219
|
-
require_all path, {:
|
184
|
+
require_all path, {base_dir: path}.merge(options)
|
220
185
|
end
|
221
186
|
end
|
222
187
|
|
@@ -226,14 +191,14 @@ module RequireAll
|
|
226
191
|
return false if paths.empty?
|
227
192
|
require "pathname"
|
228
193
|
|
229
|
-
options = {:
|
194
|
+
options = {method: :autoload}
|
230
195
|
options.merge!(paths.pop) if paths.last.is_a?(Hash)
|
231
196
|
|
232
197
|
source_directory = File.dirname caller.first.sub(/:\d+$/, '')
|
233
198
|
paths.each do |path|
|
234
199
|
file_path = Pathname.new(source_directory).join(path).to_s
|
235
|
-
require_all file_path, {:
|
236
|
-
:
|
200
|
+
require_all file_path, {method: :autoload,
|
201
|
+
base_dir: source_directory}.merge(options)
|
237
202
|
end
|
238
203
|
end
|
239
204
|
|
data/require_all.gemspec
CHANGED
data/spec/autoload_shared.rb
CHANGED
@@ -9,18 +9,18 @@ shared_examples_for "#autoload_all syntactic sugar" do
|
|
9
9
|
|
10
10
|
it "accepts files with and without extensions" do
|
11
11
|
is_expected.not_to be_loaded("Autoloaded::Module2::LongerName")
|
12
|
-
expect(send(@method, @base_dir + '/module2/longer_name', :
|
12
|
+
expect(send(@method, @base_dir + '/module2/longer_name', base_dir: @autoload_base_dir)).to be_truthy
|
13
13
|
is_expected.to be_loaded("Autoloaded::Module2::LongerName")
|
14
14
|
|
15
15
|
is_expected.not_to be_loaded("Autoloaded::Module1::A")
|
16
|
-
expect(send(@method, @base_dir + '/module1/a.rb', :
|
16
|
+
expect(send(@method, @base_dir + '/module1/a.rb', base_dir: @autoload_base_dir)).to be_truthy
|
17
17
|
is_expected.to be_loaded("Autoloaded::Module1::A")
|
18
18
|
end
|
19
19
|
|
20
20
|
it "accepts lists of files" do
|
21
21
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
22
22
|
"Autoloaded::Module2::Module3::B")
|
23
|
-
expect(send(@method, @file_list, :
|
23
|
+
expect(send(@method, @file_list, base_dir: @autoload_base_dir)).to be_truthy
|
24
24
|
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
25
25
|
"Autoloaded::Module2::Module3::B")
|
26
26
|
end
|
@@ -28,7 +28,7 @@ shared_examples_for "#autoload_all syntactic sugar" do
|
|
28
28
|
it "is totally cool with a splatted list of arguments" do
|
29
29
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
30
30
|
"Autoloaded::Module2::Module3::B")
|
31
|
-
expect(send(@method, *(@file_list << {:
|
31
|
+
expect(send(@method, *(@file_list << {base_dir: @autoload_base_dir}))).to be_truthy
|
32
32
|
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
33
33
|
"Autoloaded::Module2::Module3::B")
|
34
34
|
end
|
@@ -36,7 +36,7 @@ shared_examples_for "#autoload_all syntactic sugar" do
|
|
36
36
|
it "will load all .rb files under a directory without a trailing slash" do
|
37
37
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
38
38
|
"Autoloaded::Module2::Module3::B")
|
39
|
-
expect(send(@method, @base_dir, :
|
39
|
+
expect(send(@method, @base_dir, base_dir: @autoload_base_dir)).to be_truthy
|
40
40
|
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
41
41
|
"Autoloaded::Module2::Module3::B")
|
42
42
|
end
|
@@ -44,7 +44,7 @@ shared_examples_for "#autoload_all syntactic sugar" do
|
|
44
44
|
it "will load all .rb files under a directory with a trailing slash" do
|
45
45
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
46
46
|
"Autoloaded::Module2::Module3::B")
|
47
|
-
expect(send(@method, "#{@base_dir}/", :
|
47
|
+
expect(send(@method, "#{@base_dir}/", base_dir: @autoload_base_dir)).to be_truthy
|
48
48
|
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
49
49
|
"Autoloaded::Module2::Module3::B")
|
50
50
|
end
|
@@ -52,7 +52,7 @@ shared_examples_for "#autoload_all syntactic sugar" do
|
|
52
52
|
it "will load all files specified by a glob" do
|
53
53
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
54
54
|
"Autoloaded::Module2::Module3::B")
|
55
|
-
expect(send(@method, "#{@base_dir}/**/*.rb", :
|
55
|
+
expect(send(@method, "#{@base_dir}/**/*.rb", base_dir: @autoload_base_dir)).to be_truthy
|
56
56
|
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName",
|
57
57
|
"Autoloaded::Module2::Module3::B")
|
58
58
|
end
|
@@ -78,7 +78,7 @@ shared_examples_for "#autoload_all syntactic sugar" do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
it "raises LoadError if :base_dir doesn't exist" do
|
81
|
-
expect {send(@method, @base_dir, :
|
81
|
+
expect {send(@method, @base_dir, base_dir: @base_dir + "/non_existing_dir")}.
|
82
82
|
to raise_exception(LoadError)
|
83
83
|
end
|
84
84
|
end
|
data/spec/autoload_spec.rb
CHANGED
@@ -4,21 +4,21 @@ require File.dirname(__FILE__) + '/autoload_shared.rb'
|
|
4
4
|
describe "autoload_all" do
|
5
5
|
|
6
6
|
subject { self }
|
7
|
-
|
7
|
+
|
8
8
|
it "provides require_all functionality by using 'autoload' instead of 'require'" do
|
9
9
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
10
|
-
autoload_all
|
10
|
+
autoload_all fixture_path('autoloaded')
|
11
11
|
is_expected.to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
12
12
|
end
|
13
13
|
|
14
14
|
it "doesn't autoload files with wrong module names" do
|
15
|
-
autoload_all
|
15
|
+
autoload_all fixture_path('autoloaded')
|
16
16
|
is_expected.not_to be_loaded("Autoloaded::WrongModule::WithWrongModule", "WrongModule::WithWrongModule")
|
17
17
|
end
|
18
18
|
|
19
19
|
it "autoloads class nested into another class" do
|
20
20
|
is_expected.not_to be_loaded("Autoloaded::Class1", "Autoloaded::Class1::C")
|
21
|
-
autoload_all
|
21
|
+
autoload_all fixture_path('autoloaded')
|
22
22
|
is_expected.to be_loaded("Autoloaded::Class1")
|
23
23
|
expect(Autoloaded::Class1).to be_a Class
|
24
24
|
is_expected.to be_loaded("Autoloaded::Class1::C")
|
@@ -26,17 +26,16 @@ describe "autoload_all" do
|
|
26
26
|
|
27
27
|
it "needs to specify base_dir for autoloading if loading something from under top-level module directory" do
|
28
28
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
29
|
-
autoload_all
|
29
|
+
autoload_all fixture_path('autoloaded/module1')
|
30
30
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
31
31
|
|
32
|
-
autoload_all
|
33
|
-
:base_dir => File.dirname(__FILE__) + "/fixtures/autoloaded"
|
32
|
+
autoload_all fixture_path('autoloaded/module1'), base_dir: fixture_path('autoloaded')
|
34
33
|
is_expected.to be_loaded("Autoloaded::Module1::A")
|
35
34
|
is_expected.not_to be_loaded("Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
36
35
|
end
|
37
36
|
|
38
37
|
before(:all) do
|
39
|
-
@base_dir =
|
38
|
+
@base_dir = fixture_path('autoloaded')
|
40
39
|
@method = :autoload_all
|
41
40
|
@autoload_base_dir = @base_dir
|
42
41
|
end
|
@@ -49,25 +48,24 @@ describe "autoload_rel" do
|
|
49
48
|
|
50
49
|
it "provides autoload_all functionality relative to the current file" do
|
51
50
|
is_expected.not_to be_loaded("Modules::Module1::First", "Modules::Module2::Second", "Modules::Zero")
|
52
|
-
require
|
51
|
+
require fixture_path('autoloaded_rel/modules/zero')
|
53
52
|
is_expected.to be_loaded("Modules::Module1::First", "Modules::Module2::Second", "Modules::Zero")
|
54
53
|
end
|
55
54
|
|
56
55
|
it "needs to specify base_dir for autoloading if loading something from under top-level module directory" do
|
57
56
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
58
|
-
autoload_rel
|
57
|
+
autoload_rel relative_fixture_path('autoloaded/module1')
|
59
58
|
is_expected.not_to be_loaded("Autoloaded::Module1::A", "Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
60
59
|
|
61
|
-
autoload_rel
|
62
|
-
:base_dir => File.dirname(__FILE__) + "/fixtures/autoloaded"
|
60
|
+
autoload_rel relative_fixture_path('autoloaded/module1'), base_dir: fixture_path('autoloaded')
|
63
61
|
is_expected.to be_loaded("Autoloaded::Module1::A")
|
64
62
|
is_expected.not_to be_loaded("Autoloaded::Module2::LongerName", "Autoloaded::Module2::Module3::B")
|
65
63
|
end
|
66
64
|
|
67
65
|
before(:all) do
|
68
|
-
@base_dir = '
|
66
|
+
@base_dir = relative_fixture_path('autoloaded')
|
69
67
|
@method = :autoload_rel
|
70
|
-
@autoload_base_dir =
|
68
|
+
@autoload_base_dir = fixture_path('autoloaded')
|
71
69
|
end
|
72
70
|
it_should_behave_like "#autoload_all syntactic sugar"
|
73
71
|
end
|
data/spec/load_spec.rb
CHANGED
@@ -2,11 +2,11 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
|
|
2
2
|
require File.dirname(__FILE__) + '/require_shared.rb'
|
3
3
|
|
4
4
|
describe "load_all" do
|
5
|
-
|
5
|
+
|
6
6
|
subject { self }
|
7
7
|
|
8
8
|
it "provides require_all functionality but using 'load' instead of 'require'" do
|
9
|
-
require_all
|
9
|
+
require_all fixture_path('resolvable')
|
10
10
|
expect(C.new).to be_cool
|
11
11
|
|
12
12
|
class C
|
@@ -18,12 +18,12 @@ describe "load_all" do
|
|
18
18
|
expect(C.new).not_to be_cool
|
19
19
|
C.send :remove_method, :cool?
|
20
20
|
|
21
|
-
load_all
|
21
|
+
load_all fixture_path('resolvable')
|
22
22
|
expect(C.new).to be_cool
|
23
23
|
end
|
24
24
|
|
25
25
|
before(:all) do
|
26
|
-
@base_dir =
|
26
|
+
@base_dir = fixture_path('autoloaded')
|
27
27
|
@method = :load_all
|
28
28
|
end
|
29
29
|
it_should_behave_like "#require_all syntactic sugar"
|
@@ -34,7 +34,7 @@ describe "load_rel" do
|
|
34
34
|
subject { self }
|
35
35
|
|
36
36
|
it "provides load_all functionality relative to the current file" do
|
37
|
-
require
|
37
|
+
require fixture_path('relative/d/d')
|
38
38
|
|
39
39
|
is_expected.to be_loaded("RelativeA", "RelativeC", "RelativeD")
|
40
40
|
expect(RelativeD.new).to be_ok
|
@@ -48,7 +48,7 @@ describe "load_rel" do
|
|
48
48
|
expect(RelativeD.new).not_to be_ok
|
49
49
|
RelativeD.send :remove_method, :ok?
|
50
50
|
|
51
|
-
load
|
51
|
+
load fixture_path('relative/d/d.rb')
|
52
52
|
expect(RelativeD.new).to be_ok
|
53
53
|
end
|
54
54
|
|
data/spec/require_spec.rb
CHANGED
@@ -5,22 +5,35 @@ describe "require_all" do
|
|
5
5
|
|
6
6
|
subject { self }
|
7
7
|
|
8
|
-
|
9
|
-
it "
|
10
|
-
require_all
|
8
|
+
context "when files correctly declare their dependencies" do
|
9
|
+
it "requires them successfully" do
|
10
|
+
require_all fixture_path('resolvable/*.rb')
|
11
11
|
|
12
12
|
is_expected.to be_loaded("A", "B", "C", "D")
|
13
13
|
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "errors" do
|
17
|
+
it "raises RequireAll:LoadError if files do not declare their dependencies" do
|
18
|
+
expect do
|
19
|
+
require_all fixture_path('unresolvable/*.rb')
|
20
|
+
end.to raise_error(RequireAll::LoadError) do |error|
|
21
|
+
expect(error.cause).to be_a NameError
|
22
|
+
expect(error.message).to match /Please require the necessary files/
|
23
|
+
end
|
24
|
+
end
|
14
25
|
|
15
|
-
it "raises
|
26
|
+
it "raises other NameErrors if encountered" do
|
16
27
|
expect do
|
17
|
-
require_all
|
18
|
-
end.to raise_error(NameError)
|
28
|
+
require_all fixture_path('error')
|
29
|
+
end.to raise_error(NameError) do |error|
|
30
|
+
expect(error.message).to match /undefined local variable or method `non_existent_method'/
|
31
|
+
end
|
19
32
|
end
|
20
33
|
end
|
21
34
|
|
22
35
|
before(:all) do
|
23
|
-
@base_dir =
|
36
|
+
@base_dir = fixture_path('autoloaded')
|
24
37
|
@method = :require_all
|
25
38
|
end
|
26
39
|
it_should_behave_like "#require_all syntactic sugar"
|
@@ -31,14 +44,14 @@ describe "require_rel" do
|
|
31
44
|
subject { self }
|
32
45
|
|
33
46
|
it "provides require_all functionality relative to the current file" do
|
34
|
-
require
|
47
|
+
require fixture_path('relative/b/b')
|
35
48
|
|
36
49
|
is_expected.to be_loaded("RelativeA", "RelativeB", "RelativeC")
|
37
50
|
is_expected.not_to be_loaded("RelativeD")
|
38
51
|
end
|
39
52
|
|
40
53
|
before(:all) do
|
41
|
-
@base_dir = '
|
54
|
+
@base_dir = relative_fixture_path('autoloaded')
|
42
55
|
@method = :require_rel
|
43
56
|
end
|
44
57
|
it_should_behave_like "#require_all syntactic sugar"
|
data/spec/spec_helper.rb
CHANGED
@@ -1,30 +1,38 @@
|
|
1
|
-
require "simplecov"
|
2
|
-
require "coveralls"
|
3
|
-
|
4
|
-
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
5
|
-
SimpleCov.start
|
6
|
-
|
7
|
-
require File.dirname(__FILE__) + '/../lib/require_all.rb'
|
8
|
-
|
9
|
-
module SpecHelper
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
1
|
+
require "simplecov"
|
2
|
+
require "coveralls"
|
3
|
+
|
4
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
5
|
+
SimpleCov.start
|
6
|
+
|
7
|
+
require File.dirname(__FILE__) + '/../lib/require_all.rb'
|
8
|
+
|
9
|
+
module SpecHelper
|
10
|
+
def fixture_path(fixture_name, relative_dir = File.dirname(__FILE__))
|
11
|
+
File.join(relative_dir, 'fixtures', fixture_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def relative_fixture_path(fixture_name)
|
15
|
+
fixture_path(fixture_name, '.')
|
16
|
+
end
|
17
|
+
|
18
|
+
def unload_all
|
19
|
+
%w{A B C D WrongModule Autoloaded Modules
|
20
|
+
RelativeA RelativeB RelativeC RelativeD}.each do |const|
|
21
|
+
Object.send(:remove_const, const) rescue nil
|
22
|
+
end
|
23
|
+
$LOADED_FEATURES.delete_if {|f| f =~ /autoloaded|error|relative|resolvable/}
|
24
|
+
end
|
25
|
+
|
26
|
+
def loaded?(*klazzes)
|
27
|
+
klazzes.all? {|klazz| Object.class_eval(klazz) rescue nil}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec.configure do |config|
|
32
|
+
config.include SpecHelper
|
33
|
+
config.color = true
|
34
|
+
|
35
|
+
config.before do
|
36
|
+
unload_all
|
37
|
+
end
|
38
|
+
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:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarmo Pertman
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- spec/fixtures/autoloaded_rel/modules/module1/first.rb
|
83
83
|
- spec/fixtures/autoloaded_rel/modules/module2/second.rb
|
84
84
|
- spec/fixtures/autoloaded_rel/modules/zero.rb
|
85
|
+
- spec/fixtures/error/a.rb
|
85
86
|
- spec/fixtures/relative/a.rb
|
86
87
|
- spec/fixtures/relative/b/b.rb
|
87
88
|
- spec/fixtures/relative/c/c.rb
|
@@ -139,6 +140,7 @@ test_files:
|
|
139
140
|
- spec/fixtures/autoloaded_rel/modules/module1/first.rb
|
140
141
|
- spec/fixtures/autoloaded_rel/modules/module2/second.rb
|
141
142
|
- spec/fixtures/autoloaded_rel/modules/zero.rb
|
143
|
+
- spec/fixtures/error/a.rb
|
142
144
|
- spec/fixtures/relative/a.rb
|
143
145
|
- spec/fixtures/relative/b/b.rb
|
144
146
|
- spec/fixtures/relative/c/c.rb
|