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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc2c0b7e0c3e204cce27a38f26147a277dc70a9f3dc5cccf61bd095db32a6600
4
- data.tar.gz: f52022c23ec705627cf213d13d837c342dc6733e5122ddbaa089b28b263d0cd9
3
+ metadata.gz: 4679afd9197abfafe51fe76ac942de5c4ea8e1865af82f3ea5bd483bb27300cc
4
+ data.tar.gz: b9da3db586f566657de4f8196f876bf5a0770a7a3b9667b5b4502ed758d3941e
5
5
  SHA512:
6
- metadata.gz: 2cab7b31b54641a231ab3985883a8b095a2f125444c22ba193f3ebf79bba5e43980a5abe07c469f56aaf14fe0480a67264a27a6a232a5c528d5d020170961af3
7
- data.tar.gz: 15af8406a2a364e64830f9e8e4f5ec73d17b38e50412fbbccfac6f5b5788c9c12bfc5a5196f268645033b159fec8e506e4351b2822263068d474091fa31cd3cf
6
+ metadata.gz: 19a2fc8efffc9c0ea847d0c864e91d76cfec5ad2586132f83f654b753432a531225866e160b05fb42a0c3a0c777559f9995a4ef0ab54d6f480e4f2ac12d7e6f7
7
+ data.tar.gz: '0295c2beaf7183c7eecfd4f7e6bd99a6f5f884542a8eaf3686d5b78a14be75928c63618bf98b6cd2a54cb9cb028c9d3f67702773aeefa261ff6a6eedbaf420e8'
@@ -15,7 +15,7 @@ jobs:
15
15
  fail-fast: false
16
16
  matrix:
17
17
  os: [ ubuntu ]
18
- ruby: [ 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, head ]
18
+ ruby: [ 2.1, 2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2, head ]
19
19
  usage: [ modern, legacy ]
20
20
  steps:
21
21
  - name: checkout
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`
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RequireRelativeDir
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.1'
5
5
  end
@@ -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
- caller_path = caller_locations(1..1).first.absolute_path
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
- include RequireRelativeDir
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 = Array(except).map(&:to_s)
30
- except.map! { |exception| File.extname(exception) == '.rb' ? exception[0...-3] : exception }
31
- paths.reject { |file| except.delete File.basename(file, '.rb') }
32
- ensure
33
- raise ArgumentError, "The following exceptions where not found: #{except.join(', ')}" unless except.empty?
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
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ['lib']
26
26
  spec.required_ruby_version = '> 2.1.0'
27
+ spec.metadata['rubygems_mfa_required'] = 'true'
27
28
  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.0.0
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: 2020-11-09 00:00:00.000000000 Z
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.1.4
53
+ rubygems_version: 3.4.10
53
54
  signing_key:
54
55
  specification_version: 4
55
56
  summary: Basic utility to require a directory.