lazy_require 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cf0fbe761105702360d3854efe5d6dcecf56b34
4
- data.tar.gz: 36750dfd97102eeaa954e0fc4f722624db7f7470
3
+ metadata.gz: 126e4dbfcad94a1ce5e9b68b9da5fc3572214a25
4
+ data.tar.gz: 1b3ef93c27246fbf5d726a29b37f6c6ebd673c55
5
5
  SHA512:
6
- metadata.gz: 2bb28136852cd2e42b7aec30d2dc29741cb2ddfe38500b747414ff360b4f762fdbadbdda1d3dfa2eb46dac8f7d001875793a871230f6ad18e40df1886c8310f9
7
- data.tar.gz: bb1d276a83d739e5df4ed612938fa196f36adadeee50984a8776765d1f4d48ad4a1336efe0806460f64482a3f006e0ed8f5da588792d3446aeec8d566665a876
6
+ metadata.gz: b00987706a8588ed20b44128189a92cbddc1a67f130941d7820d1709258eb54f5096f60fd5fe31fc8e6798419aecab750a16db46cde9c1cfc333395d940f5bd8
7
+ data.tar.gz: 10c9c68c904f010eb2e7ef987e5248a694d4dfcd90d5599d99626a726ac053152e94c9b91ffd21b810e3cb6a5d645ebbfb6c17e020c4742cd03a7fd128ad4a67
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # LazyRequire
2
+ [![Code Climate](https://codeclimate.com/github/thomas07vt/lazy_require/badges/gpa.svg)](https://codeclimate.com/github/thomas07vt/lazy_require)
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lazy_require`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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/[USERNAME]/lazy_require.
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
- def self.load(files)
6
- files = toArray(files)
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
- true
16
- end
8
+ def require(files)
9
+ files = toArray(files)
10
+ failed_files = try_to_require(files)
17
11
 
18
- def self.load_all(glob)
19
- files = Dir[glob]
20
- self.load(files)
21
- end
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
- private
18
+ true
19
+ end
24
20
 
25
- def self.toArray(files)
26
- if files.is_a?(Array)
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
- def self.try_to_require(files)
34
- failed = []
35
- files.each do |file|
36
- begin
37
- require "#{file}"
38
- rescue NameError => e
39
- failed << file
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
- failed
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
 
@@ -1,3 +1,5 @@
1
+
1
2
  module LazyRequire
2
- VERSION = "0.1.0"
3
+ VERSION = "0.2.1"
3
4
  end
5
+
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.0
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-04-25 00:00:00.000000000 Z
11
+ date: 2016-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler