dirload 0.7.3 → 0.7.4

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: ec0a5fb0f9791f9c86bd426942bd8b97fd37c4dde99ddea3a2988bf7700cdbf4
4
- data.tar.gz: d7dd2153288bbab8c9eba9212830f0c6d8035e654089526747871e52628a0636
3
+ metadata.gz: 84947a06acc71325c32000a7f633a3217fe94f13d61e09297f319ac7c34e4845
4
+ data.tar.gz: 3bba94dd297cb6fe5971c30c16ff43a560b7ef5e50ff4fb70b132b88eae61c52
5
5
  SHA512:
6
- metadata.gz: bd29235c6e86da989b986489d31187af619c45b5bb93b6b19d2c1c2a46ba246cdbf57cba83fd7afa92ba80ed0e10ad83c247ded9383c5f72819dd89dcf1c3768
7
- data.tar.gz: f754e46247f236b8571bf286590776d53cc244aa4d38ee8d08c0cf0790b64caffc541147d57bbdffda5ee77e3b1b30606bca4c1ffeba31662d11e302304bd108
6
+ metadata.gz: 78db695fe48228db71026cbd40d7cabd1f5799669f59c2e3a18248d93aa4f5e8293a977616901534a96afdbc76722c34cd6c23b776a0792bbb957f6f83eada3b
7
+ data.tar.gz: dc343eb1ae07953d2ebafe170ed9cc066230ad37737cb226f8257b8aea0a433d75bdf77ae8b383f4bef0682c437ca02454d71015c7bace62d5fc174ecfd3ef41
data/lib/dirload.rb CHANGED
@@ -5,6 +5,7 @@ require_relative 'adapters/rbx_adapter'
5
5
  require_relative 'adapters/ruby_adapter'
6
6
  require_relative 'loader'
7
7
  require_relative 'metadata'
8
+ require_relative 'unloader'
8
9
 
9
10
  module Dirload
10
11
  module_function
@@ -51,6 +52,7 @@ module Dirload
51
52
  end
52
53
 
53
54
  Dirload.step(:mapload, loaded_paths:)
55
+ Unloader.track(path: absolute_path, loaded_paths:)
54
56
  Dirload.step(:preload, loaded_paths:)
55
57
  Dirload.step(:evaluate, loaded_paths:)
56
58
 
@@ -58,6 +60,11 @@ module Dirload
58
60
  end
59
61
  end
60
62
 
63
+ # Removes the constants a directory defined and lets its Ruby files be required again, so it can be loaded again.
64
+ def unload(path, pwd = Dir.pwd)
65
+ Unloader.unload(path: File.expand_path(path, pwd))
66
+ end
67
+
61
68
  def file_paths(absolute_path:)
62
69
  Dir["#{absolute_path}/**/*"].filter { !File.directory?(it) }
63
70
  end
data/lib/loader.rb CHANGED
@@ -1,45 +1,60 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dirload
4
+ # Autoloads the constants a file uses from the files that define them, so a file can use a class or a constant like
5
+ # "Tag = Data.define" from another file whichever loads first.
4
6
  class Loader
5
- class MissingDependencyError < StandardError; end
6
-
7
7
  class << self
8
8
  def add_autoloads(file_proxy:)
9
- autoloads = []
9
+ file_proxy.dependencies.filter_map do |dependency|
10
+ name, dependency_proxy = definition(dependency:)
11
+ # Ruby autoloads with require, which only loads Ruby files, so an RBX file's classes load in their turn.
12
+ next unless dependency_proxy&.file_path&.end_with?('.rb') && !dependency_proxy.equal?(file_proxy)
13
+
14
+ parent_name, _, constant = name.rpartition('::')
15
+ parent = namespace_module(parent_name)
16
+ # A constant that's defined or already autoloads, like a gem's class, is left as it is.
17
+ next if parent.nil? || parent.const_defined?(constant, false)
18
+
19
+ parent.autoload(constant.to_sym, dependency_proxy.file_path)
20
+ { origin: file_proxy.file_path, name:, file_path: dependency_proxy.file_path }
21
+ end
22
+ end
10
23
 
11
- file_proxy.dependencies.each do |namespace_with_dependency|
12
- *namespace, dependency = namespace_with_dependency.split('::')
24
+ private
13
25
 
14
- dependency_proxies = find_dependency_proxies(namespace:, dependency:)
26
+ # Where a constant used in a namespace is defined, as Ruby looks it up. "Shelf::Tag" is Shelf::Tag if a file
27
+ # defines it and otherwise Tag. A constant no file defines comes from a gem or Ruby itself.
28
+ def definition(dependency:)
29
+ *namespace, constant = dependency.split('::')
15
30
 
16
- current_namespace = Object
17
- namespace.each do |module_name|
18
- current_namespace.const_set(module_name, Module.new) unless current_namespace.const_defined?(module_name)
19
- current_namespace = current_namespace.const_get(module_name)
20
- end
31
+ loop do
32
+ name = [*namespace, constant].join('::')
33
+ proxies = Lowkey[name]
34
+ return [name, proxies.first] if proxies.is_a?(Array) && proxies.any?
35
+ return if namespace.empty?
21
36
 
22
- dependency_proxies.each do |dependency_proxy|
23
- autoloads << { origin: file_proxy.file_path, current_namespace:, dependency:, file_path: dependency_proxy.file_path }
24
- current_namespace.autoload(dependency.to_sym, dependency_proxy.file_path)
25
- end
37
+ namespace.pop
26
38
  end
27
-
28
- autoloads
29
39
  end
30
40
 
31
- def find_dependency_proxies(namespace:, dependency:)
32
- return Lowkey[dependency] || raise(MissingDependencyError) if namespace.empty?
33
-
34
- namespace_with_dependency = [namespace, dependency].join('::')
35
- return Lowkey[namespace_with_dependency] if Lowkey[namespace_with_dependency]
41
+ # The module a constant is in, without loading anything to find it. A module the files only define as a module,
42
+ # like "module Shelves", is made now so its constants can autoload, as "module Shelves" reopens it. A class can't
43
+ # be made early, so a constant in a class that isn't loaded yet is left to load in its turn.
44
+ def namespace_module(name)
45
+ path = []
46
+ name.split('::').reduce(Object) do |namespace, constant|
47
+ path << constant
48
+ next namespace.const_get(constant, false) if namespace.const_defined?(constant, false) && !namespace.autoload?(constant)
49
+ return nil unless only_module?(path.join('::')) && !namespace.const_defined?(constant, false)
50
+
51
+ namespace.const_set(constant, Module.new)
52
+ end
53
+ end
36
54
 
37
- namespace.pop
38
- find_dependency_proxies(namespace:, dependency:)
39
- # A dependency that isn't mapped could come from a gem or from another constant already loaded.
40
- # TODO: Map more dependencies and try to remove this workaround.
41
- rescue MissingDependencyError
42
- []
55
+ def only_module?(name)
56
+ definitions = Array(Lowkey[name]).map { |file_proxy| file_proxy.definitions[name] }
57
+ definitions.any? && definitions.none? { |definition| definition.nil? || definition.is_a?(Lowkey::ClassProxy) }
43
58
  end
44
59
  end
45
60
  end
data/lib/unloader.rb ADDED
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dirload
4
+ # Unloads a directory dirload() loaded so it can be loaded again. The constants its files define go and its Ruby files
5
+ # can be required again. Constants that were defined before it loaded stay, like a gem's class the directory reopens.
6
+ module Unloader
7
+ class << self
8
+ # Notes the constants the directory defines once its files are mapped, before its autoloads define any.
9
+ def track(path:, loaded_paths:)
10
+ names = loaded_paths.keys.flat_map { |file_path| defined_names(Lowkey[file_path]) }.uniq
11
+ directories[path] = { names: names.reject { |name| defined_constant?(name) }, ruby_paths: loaded_paths.keys.grep(/\.rb\z/) }
12
+ end
13
+
14
+ # The names of the outermost constants removed, as their classes' nested constants go with them.
15
+ def unload(path:)
16
+ directory = directories.delete(path)
17
+ return [] unless directory
18
+
19
+ directory[:ruby_paths].each { |file_path| $LOADED_FEATURES.delete(file_path) }
20
+ outermost(directory[:names]).select { |name| remove_constant(name) }
21
+ end
22
+
23
+ private
24
+
25
+ def directories
26
+ @directories ||= {}
27
+ end
28
+
29
+ # Its classes and modules and the constants it assigns, like "Tag = Data.define" or "COUNT = 5" in a class.
30
+ def defined_names(file_proxy)
31
+ return [] unless file_proxy
32
+
33
+ [*file_proxy.definitions.keys, *file_proxy.constants]
34
+ end
35
+
36
+ def outermost(names)
37
+ names.reject { |name| names.any? { |other| name.start_with?("#{other}::") } }
38
+ end
39
+
40
+ def defined_constant?(name)
41
+ Object.const_defined?(name)
42
+ rescue NameError
43
+ false
44
+ end
45
+
46
+ def remove_constant(name)
47
+ parent_name, _, constant = name.rpartition('::')
48
+ parent = parent_name.empty? ? Object : Object.const_get(parent_name)
49
+ return false unless parent.const_defined?(constant, false)
50
+
51
+ parent.send(:remove_const, constant)
52
+ true
53
+ rescue NameError
54
+ false
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dirload
4
- VERSION = '0.7.3'
4
+ VERSION = '0.7.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -13,16 +13,22 @@ dependencies:
13
13
  name: lowkey
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.7'
16
19
  - - ">="
17
20
  - !ruby/object:Gem::Version
18
- version: '0'
21
+ version: 0.7.7
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
22
25
  requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '0.7'
23
29
  - - ">="
24
30
  - !ruby/object:Gem::Version
25
- version: '0'
31
+ version: 0.7.7
26
32
  description: An autoloader that lets you use any module namespace with any folder
27
33
  structure and mix in manual requires.
28
34
  email:
@@ -38,6 +44,7 @@ files:
38
44
  - lib/dirload.rb
39
45
  - lib/loader.rb
40
46
  - lib/metadata.rb
47
+ - lib/unloader.rb
41
48
  - lib/version.rb
42
49
  homepage: https://github.com/low-rb/dirload
43
50
  licenses: []