require_relative_dir 1.0.0 → 1.1.1
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/version.rb +1 -1
- data/lib/require_relative_dir.rb +38 -13
- data/require_relative_dir.gemspec +1 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4679afd9197abfafe51fe76ac942de5c4ea8e1865af82f3ea5bd483bb27300cc
|
4
|
+
data.tar.gz: b9da3db586f566657de4f8196f876bf5a0770a7a3b9667b5b4502ed758d3941e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19a2fc8efffc9c0ea847d0c864e91d76cfec5ad2586132f83f654b753432a531225866e160b05fb42a0c3a0c777559f9995a4ef0ab54d6f480e4f2ac12d7e6f7
|
7
|
+
data.tar.gz: '0295c2beaf7183c7eecfd4f7e6bd99a6f5f884542a8eaf3686d5b78a14be75928c63618bf98b6cd2a54cb9cb028c9d3f67702773aeefa261ff6a6eedbaf420e8'
|
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,31 +5,56 @@ 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
|
|
20
18
|
refine Object do
|
21
|
-
|
19
|
+
if respond_to?(:import_methods, true)
|
20
|
+
import_methods RequireRelativeDir
|
21
|
+
else
|
22
|
+
include RequireRelativeDir
|
23
|
+
end
|
22
24
|
private :require_relative_dir
|
23
25
|
end
|
24
26
|
|
25
27
|
extend self
|
26
28
|
|
27
29
|
# @api private
|
28
|
-
def self.remove_exceptions(paths, except)
|
29
|
-
except =
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
def self.remove_exceptions(paths, base, except)
|
31
|
+
except = as_ruby_files(except, base)
|
32
|
+
new_paths = paths - except
|
33
|
+
if new_paths.size + except.size != paths.size
|
34
|
+
not_found = (except - paths).map { |file| File.basename(file, '.rb') }
|
35
|
+
raise ArgumentError, "The following exceptions where not found: #{not_found.join(', ')}" unless not_found.empty?
|
36
|
+
end
|
37
|
+
new_paths
|
38
|
+
end
|
39
|
+
|
40
|
+
# @api private
|
41
|
+
def self.put_first(paths, base, first)
|
42
|
+
first = as_ruby_files(first, base)
|
43
|
+
first | paths
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.as_ruby_files(names, base)
|
47
|
+
names = Array(names).map(&:to_s)
|
48
|
+
names.map! do |name|
|
49
|
+
ext = '.rb' unless File.extname(name) == '.rb'
|
50
|
+
"#{base}/#{name}#{ext}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.find_base(dir_name)
|
55
|
+
caller_path = caller_locations(2..2).first.absolute_path
|
56
|
+
dir_name = File.basename(caller_path, '.*') if dir_name == SAME_AS_CALLER_NAME
|
57
|
+
base_path = File.dirname(caller_path)
|
58
|
+
File.expand_path("#{base_path}/#{dir_name}")
|
34
59
|
end
|
35
60
|
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.1
|
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: 2023-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Basic utility to require a directory.
|
14
14
|
email:
|
@@ -33,7 +33,8 @@ files:
|
|
33
33
|
homepage: https://github.com/marcandre/require_relative_dir
|
34
34
|
licenses:
|
35
35
|
- MIT
|
36
|
-
metadata:
|
36
|
+
metadata:
|
37
|
+
rubygems_mfa_required: 'true'
|
37
38
|
post_install_message:
|
38
39
|
rdoc_options: []
|
39
40
|
require_paths:
|
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
50
|
- !ruby/object:Gem::Version
|
50
51
|
version: '0'
|
51
52
|
requirements: []
|
52
|
-
rubygems_version: 3.
|
53
|
+
rubygems_version: 3.4.10
|
53
54
|
signing_key:
|
54
55
|
specification_version: 4
|
55
56
|
summary: Basic utility to require a directory.
|