require_relative_dir 1.0.0 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc2c0b7e0c3e204cce27a38f26147a277dc70a9f3dc5cccf61bd095db32a6600
4
- data.tar.gz: f52022c23ec705627cf213d13d837c342dc6733e5122ddbaa089b28b263d0cd9
3
+ metadata.gz: 1ceb9301310b33670b1cf305a4f9eb2121356e153e4462708143515352ae4938
4
+ data.tar.gz: ad2bb7325488969ab7ab9b9c5516940d125abb6350335b62a873195b7a4ccf8d
5
5
  SHA512:
6
- metadata.gz: 2cab7b31b54641a231ab3985883a8b095a2f125444c22ba193f3ebf79bba5e43980a5abe07c469f56aaf14fe0480a67264a27a6a232a5c528d5d020170961af3
7
- data.tar.gz: 15af8406a2a364e64830f9e8e4f5ec73d17b38e50412fbbccfac6f5b5788c9c12bfc5a5196f268645033b159fec8e506e4351b2822263068d474091fa31cd3cf
6
+ metadata.gz: cba67309c2b6e5ddc99ae8adf3b7d32909dac9df08101504daa98bff6f2db8abd5d322c09210f42942b4a2fde5d89bb1973750ae3feb1ead018382a9da971217
7
+ data.tar.gz: 941ba5b79cc0aa292668d8176548d30dab58163f115971b65d9dcdb9df72758dbb6bf1ebad2f234bf2d57cfc3b12fbf5cab95b9ade905112353381f95d71c1e0
@@ -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.2, 2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 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`
@@ -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
- 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
 
@@ -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 = 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?
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
@@ -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.0'
5
5
  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.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: 2020-11-09 00:00:00.000000000 Z
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.1.4
52
+ rubygems_version: 3.2.3
53
53
  signing_key:
54
54
  specification_version: 4
55
55
  summary: Basic utility to require a directory.