lazy_require 0.1.0 → 0.2.1
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/README.md +101 -4
- data/lib/lazy_require.rb +35 -30
- data/lib/lazy_require/version.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 126e4dbfcad94a1ce5e9b68b9da5fc3572214a25
|
4
|
+
data.tar.gz: 1b3ef93c27246fbf5d726a29b37f6c6ebd673c55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b00987706a8588ed20b44128189a92cbddc1a67f130941d7820d1709258eb54f5096f60fd5fe31fc8e6798419aecab750a16db46cde9c1cfc333395d940f5bd8
|
7
|
+
data.tar.gz: 10c9c68c904f010eb2e7ef987e5248a694d4dfcd90d5599d99626a726ac053152e94c9b91ffd21b810e3cb6a5d645ebbfb6c17e020c4742cd03a7fd128ad4a67
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# LazyRequire
|
2
|
+
[](https://codeclimate.com/github/thomas07vt/lazy_require)
|
2
3
|
|
3
|
-
|
4
|
+
Isn't it annoying when you try to load one file that is dependent on another file that hasn't been loaded yet?
|
5
|
+
**LazyRequire** lets you require all your project files without worrying about the load order.
|
4
6
|
|
5
|
-
|
7
|
+
It has a similar function to the Rails autoloader, but is much simpler.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -20,9 +22,104 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
$ gem install lazy_require
|
22
24
|
|
25
|
+
|
26
|
+
|
23
27
|
## Usage
|
28
|
+
Given this folder structure:
|
29
|
+
|
30
|
+
```terminal
|
31
|
+
app_root
|
32
|
+
|- lib/
|
33
|
+
|- first.rb
|
34
|
+
|- second.rb
|
35
|
+
|- app.rb
|
36
|
+
```
|
37
|
+
|
38
|
+
If you have files like these inside lib:
|
39
|
+
```ruby
|
40
|
+
# first.rb
|
41
|
+
# The First class depends on the Second class
|
42
|
+
class First < Second
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# second.rb
|
48
|
+
class Second
|
49
|
+
# Does something special
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
You might want to load all files inside your lib directory from the app.rb root file.
|
54
|
+
Doing something like this could easily fail if load order matters:
|
55
|
+
```ruby
|
56
|
+
# App.rb
|
57
|
+
|
58
|
+
# This might cause issues
|
59
|
+
Dir['./lib/**/*.rb'].each { |file| require file }
|
60
|
+
|
61
|
+
class App
|
62
|
+
# Some stuff here
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
In the above files, its required to load the "second.rb" file prior to the "first.rb" file.
|
67
|
+
You could add a line like this, explicitly adding the require line:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# first.rb
|
71
|
+
|
72
|
+
require_relative './second.rb'
|
73
|
+
# The First class depends on the Second class
|
74
|
+
class First < Second
|
75
|
+
end
|
76
|
+
```
|
24
77
|
|
25
|
-
|
78
|
+
But that can get cumbersome, especially when you start moving or renaming files in your project.
|
79
|
+
|
80
|
+
With **LazyRequire**, you can ask it to load all the files in your project/folder, without having to think about the load order. If **LazyRequire** tries to load a file, which has an unloaded dependency, it will simply skip that file and try again later. So your app.rb file can look something like this:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
# App.rb
|
84
|
+
|
85
|
+
LazyRequire.load_all('./lib/**/*.rb')
|
86
|
+
|
87
|
+
class App
|
88
|
+
# Some stuff here
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
**LazyRequire.load_all()** accepts any glob pattern and will try to require all files that it finds with that glob pattern. If it it successfully loads all files it will return true:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
2.3.0 :010 > LazyRequire.load_all('./spec/support/load_all/**/*.rb')
|
96
|
+
#=> true
|
97
|
+
```
|
98
|
+
|
99
|
+
If it cannot load any of the files, it will raise an exception:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
2.3.0 :006 > LazyRequire.load_all('./spec/support/errors/**/*.rb')
|
103
|
+
RuntimeError: Could not load files:
|
104
|
+
./spec/support/errors/top_two.rb
|
105
|
+
./spec/support/errors/top.rb
|
106
|
+
|
107
|
+
from ../code/lazy_require/lib/lazy_require.rb:12:in `load'
|
108
|
+
from ../code/lazy_require/lib/lazy_require.rb:22:in `load_all'
|
109
|
+
from (irb):6
|
110
|
+
```
|
111
|
+
|
112
|
+
If you want to load a specific collection of files and avoid using the glob pattern, you can do that to using the #load() method.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
files = [
|
116
|
+
'./spec/support/errors/top_two.rb',
|
117
|
+
'./spec/support/errors/top.rb',
|
118
|
+
]
|
119
|
+
|
120
|
+
LazyRequire.load(files)
|
121
|
+
#=> true
|
122
|
+
```
|
26
123
|
|
27
124
|
## Development
|
28
125
|
|
@@ -32,7 +129,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
129
|
|
33
130
|
## Contributing
|
34
131
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
132
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thomas07vt/lazy_require.
|
36
133
|
|
37
134
|
|
38
135
|
## License
|
data/lib/lazy_require.rb
CHANGED
@@ -2,44 +2,49 @@ require "lazy_require/version"
|
|
2
2
|
|
3
3
|
module LazyRequire
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
failed_files = try_to_require(files)
|
8
|
-
|
9
|
-
if failed_files.length == files.length
|
10
|
-
raise "Could not load files: \n#{failed_files.join("\n")}\n"
|
11
|
-
elsif failed_files.length != 0
|
12
|
-
self.load(failed_files)
|
13
|
-
end
|
5
|
+
class << self
|
6
|
+
alias_method :kernal_require, :require
|
14
7
|
|
15
|
-
|
16
|
-
|
8
|
+
def require(files)
|
9
|
+
files = toArray(files)
|
10
|
+
failed_files = try_to_require(files)
|
17
11
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
if failed_files.length == files.length
|
13
|
+
kernal_require "#{files.first}"
|
14
|
+
elsif failed_files.length != 0
|
15
|
+
self.require(failed_files)
|
16
|
+
end
|
22
17
|
|
23
|
-
|
18
|
+
true
|
19
|
+
end
|
24
20
|
|
25
|
-
|
26
|
-
|
27
|
-
files
|
28
|
-
else
|
29
|
-
[files]
|
21
|
+
def require_all(glob)
|
22
|
+
files = Dir[glob]
|
23
|
+
self.require(files)
|
30
24
|
end
|
31
|
-
end
|
32
25
|
|
33
|
-
|
34
|
-
|
35
|
-
files
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
26
|
+
private
|
27
|
+
|
28
|
+
def toArray(files)
|
29
|
+
if files.is_a?(Array)
|
30
|
+
files
|
31
|
+
else
|
32
|
+
[files]
|
40
33
|
end
|
41
34
|
end
|
42
|
-
|
35
|
+
|
36
|
+
def try_to_require(files)
|
37
|
+
failed = []
|
38
|
+
files.each do |file|
|
39
|
+
begin
|
40
|
+
kernal_require "#{file}"
|
41
|
+
rescue NameError
|
42
|
+
failed << file
|
43
|
+
end
|
44
|
+
end
|
45
|
+
failed
|
46
|
+
end
|
47
|
+
|
43
48
|
end
|
44
49
|
|
45
50
|
|
data/lib/lazy_require/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_require
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Thomas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|