require_relative_dir 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/require_relative_dir.yml +1 -1
- data/README.md +5 -0
- data/lib/require_relative_dir.rb +33 -12
- data/lib/require_relative_dir/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ceb9301310b33670b1cf305a4f9eb2121356e153e4462708143515352ae4938
|
4
|
+
data.tar.gz: ad2bb7325488969ab7ab9b9c5516940d125abb6350335b62a873195b7a4ccf8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cba67309c2b6e5ddc99ae8adf3b7d32909dac9df08101504daa98bff6f2db8abd5d322c09210f42942b4a2fde5d89bb1973750ae3feb1ead018382a9da971217
|
7
|
+
data.tar.gz: 941ba5b79cc0aa292668d8176548d30dab58163f115971b65d9dcdb9df72758dbb6bf1ebad2f234bf2d57cfc3b12fbf5cab95b9ade905112353381f95d71c1e0
|
data/README.md
CHANGED
@@ -21,12 +21,17 @@ using RequireRelativeDir
|
|
21
21
|
|
22
22
|
require_relative_dir 'concerns' # requires all of 'your_gem_or_app/concerns/'
|
23
23
|
require_relative_dir 'utils', except: 'big_and_unused_for_now'
|
24
|
+
require_relative_dir 'tools', first: 'base'
|
24
25
|
|
25
26
|
class Whatever
|
26
27
|
# ...
|
27
28
|
end
|
28
29
|
```
|
29
30
|
|
31
|
+
Options:
|
32
|
+
* `except`: files to exclude
|
33
|
+
* `first`: files to load before the rest
|
34
|
+
|
30
35
|
### Legacy usage
|
31
36
|
|
32
37
|
While the above usage with `using` is the recommended way, the method `require_relative_dir`
|
data/lib/require_relative_dir.rb
CHANGED
@@ -5,15 +5,13 @@ require 'require_relative_dir/version'
|
|
5
5
|
module RequireRelativeDir
|
6
6
|
SAME_AS_CALLER_NAME = Object.new
|
7
7
|
|
8
|
-
def require_relative_dir(dir_name = SAME_AS_CALLER_NAME, except: nil)
|
9
|
-
|
10
|
-
dir_name = File.basename(caller_path, '.*') if dir_name == SAME_AS_CALLER_NAME
|
11
|
-
base_path = File.dirname(caller_path)
|
12
|
-
path = File.expand_path("#{base_path}/#{dir_name}")
|
8
|
+
def require_relative_dir(dir_name = SAME_AS_CALLER_NAME, except: nil, first: nil)
|
9
|
+
path = RequireRelativeDir.find_base(dir_name)
|
13
10
|
raise LoadError, "Directory '#{path}' not found" unless Dir.exist?(path)
|
14
11
|
|
15
12
|
paths = Dir["#{path}/*.rb"].sort
|
16
|
-
paths = RequireRelativeDir.remove_exceptions(paths, except) if except
|
13
|
+
paths = RequireRelativeDir.remove_exceptions(paths, path, except) if except
|
14
|
+
paths = RequireRelativeDir.put_first(paths, path, first) if first
|
17
15
|
paths.each { |file| require file }
|
18
16
|
end
|
19
17
|
|
@@ -25,11 +23,34 @@ module RequireRelativeDir
|
|
25
23
|
extend self
|
26
24
|
|
27
25
|
# @api private
|
28
|
-
def self.remove_exceptions(paths, except)
|
29
|
-
except =
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
def self.remove_exceptions(paths, base, except)
|
27
|
+
except = as_ruby_files(except, base)
|
28
|
+
new_paths = paths - except
|
29
|
+
if new_paths.size + except.size != paths.size
|
30
|
+
not_found = (except - paths).map { |file| File.basename(file, '.rb') }
|
31
|
+
raise ArgumentError, "The following exceptions where not found: #{not_found.join(', ')}" unless not_found.empty?
|
32
|
+
end
|
33
|
+
new_paths
|
34
|
+
end
|
35
|
+
|
36
|
+
# @api private
|
37
|
+
def self.put_first(paths, base, first)
|
38
|
+
first = as_ruby_files(first, base)
|
39
|
+
first | paths
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.as_ruby_files(names, base)
|
43
|
+
names = Array(names).map(&:to_s)
|
44
|
+
names.map! do |name|
|
45
|
+
ext = '.rb' unless File.extname(name) == '.rb'
|
46
|
+
"#{base}/#{name}#{ext}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.find_base(dir_name)
|
51
|
+
caller_path = caller_locations(2..2).first.absolute_path
|
52
|
+
dir_name = File.basename(caller_path, '.*') if dir_name == SAME_AS_CALLER_NAME
|
53
|
+
base_path = File.dirname(caller_path)
|
54
|
+
File.expand_path("#{base_path}/#{dir_name}")
|
34
55
|
end
|
35
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: require_relative_dir
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marc-Andre Lafortune
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Basic utility to require a directory.
|
14
14
|
email:
|
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
|
-
rubygems_version: 3.
|
52
|
+
rubygems_version: 3.2.3
|
53
53
|
signing_key:
|
54
54
|
specification_version: 4
|
55
55
|
summary: Basic utility to require a directory.
|