lowload 0.3.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9c841ed28307b4d9211362f9e30c4d73d0cf662ce537d2f448a36e030dc3c55e
4
+ data.tar.gz: 69e0f32e92d565083b51d2758d8f8acd3b28ea334b32e2143a9614a50f6c4f2d
5
+ SHA512:
6
+ metadata.gz: 82a9791ac47f662317e94bdbda71c036efe6d5c13896a8a20a712fba57bca88b1461cf2318f56fc7a205ed2bbfb29331398a22c434406d2a2bad76a298a3c101
7
+ data.tar.gz: 8306cba1fd04398aa6ed5d83f56a389aa643e091679a71d034ef981c3699f9d10f1e7895dc8e88a2cfbddd67be6005f6c6019197584c9bd9e59bce84f1ce5465
data/lib/loader.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowLoad
4
+ class Loader
5
+ class MissingDependencyError < StandardError; end
6
+
7
+ class << self
8
+ def add_autoloads(file_proxy:)
9
+ autoloads = []
10
+
11
+ file_proxy.dependencies.each do |namespace_with_dependency|
12
+ *namespace, dependency = namespace_with_dependency.split('::')
13
+
14
+ definition_proxies = find_definition_proxies(namespace:, dependency:)
15
+
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
21
+
22
+ definition_proxies.each do |definition_proxy|
23
+ autoloads << { origin: file_proxy.file_path, current_namespace:, dependency:, file_path: definition_proxy.file_path }
24
+ current_namespace.autoload(dependency.to_sym, definition_proxy.file_path)
25
+ end
26
+ end
27
+
28
+ autoloads
29
+ end
30
+
31
+ def find_definition_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]
36
+
37
+ namespace.pop
38
+ find_definition_proxies(namespace:, dependency:)
39
+ end
40
+ end
41
+ end
42
+ end
data/lib/lowload.rb ADDED
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lowkey'
4
+ require_relative 'loader'
5
+
6
+ def top_level_binding
7
+ binding
8
+ end
9
+
10
+ module LowLoad
11
+ class << self
12
+ # Files are mapped, autoloaded, then loaded into Ruby in 3 separate stages.
13
+ def dirload(path, pwd = Dir.pwd)
14
+ absolute_path = File.expand_path(path, pwd)
15
+ file_paths = Dir["#{absolute_path}/**/*"]
16
+
17
+ # Map all definitions and dependencies first.
18
+ file_paths.each do |file_path|
19
+ Lowkey.load(file_path)
20
+ end
21
+
22
+ # Then autoload all dependencies for those files.
23
+ file_paths.each do |file_path| # rubocop:disable Style/CombinableLoops
24
+ Loader.add_autoloads(file_proxy: Lowkey[file_path])
25
+ end
26
+
27
+ # Now we can load the files into Ruby.
28
+ file_paths.each do |file_path| # rubocop:disable Style/CombinableLoops
29
+ lowload(file_path)
30
+ end
31
+ end
32
+
33
+ # Dependencies must first be loaded by dirload() or required by the file.
34
+ def lowload(file_path)
35
+ case File.extname(file_path).delete_prefix('.')
36
+ when 'rb'
37
+ load(file_path)
38
+ when 'rbx'
39
+ load_rbx(file_proxy: Lowkey[file_path] || Lowkey.load(file_path))
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def load_rbx(file_proxy:)
46
+ file_proxy.definitions.each_value do |class_proxy|
47
+ next unless class_proxy[:render]
48
+
49
+ class_proxy[:render].body.wrap(prefix: '%q{', suffix: '}')
50
+ end
51
+
52
+ # Not a security risk because "eval" is equivalent to "load" or "require_relative" in this context.
53
+ eval(file_proxy.export, top_level_binding, file_proxy.file_path, 0) # rubocop:disable Security/Eval
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowLoad
4
+ class FileProxy
5
+ attr_reader :path, :scope
6
+ attr_accessor :start_line, :end_line
7
+
8
+ def initialize(path:, scope:, start_line:, end_line: nil)
9
+ @path = path
10
+ @start_line = start_line
11
+ @end_line = end_line || start_line
12
+ @scope = scope
13
+ end
14
+
15
+ def lines?
16
+ start_line && end_line
17
+ end
18
+ end
19
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowLoad
4
+ VERSION = '0.3.0'
5
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lowload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - maedi
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: lowkey
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: LowLoad lets you use any module namespace convention and mix in manual
27
+ require statements.
28
+ email:
29
+ - maediprichard@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/loader.rb
35
+ - lib/lowload.rb
36
+ - lib/maps/dependency_map.rb
37
+ - lib/version.rb
38
+ homepage: https://github.com/low-rb/lowload
39
+ licenses: []
40
+ metadata:
41
+ homepage_uri: https://github.com/low-rb/lowload
42
+ source_code_uri: https://github.com/low-rb/lowload/src/branch/main
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.3.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.7.2
58
+ specification_version: 4
59
+ summary: An autoloader that is dumb but lets you do you
60
+ test_files: []