Module: AnonymousLoader
- Defined in:
- lib/anonymous_loader.rb,
lib/anonymous_loader/version.rb
Overview
Loads Ruby source under anonymous modules to avoid top-level namespace collisions.
Defined Under Namespace
Modules: Version Classes: Error, FileNotFoundError, PathRequest, VersionMismatchError, VersionRequest
Constant Summary collapse
Class Method Summary collapse
-
.load(files:, root: nil, **_options) ⇒ Module
Evaluate one or more Ruby files inside a fresh anonymous module.
-
.load_path(**options) ⇒ Module
Resolve and anonymously load a single file.
-
.resolve_path(**options) ⇒ String
Resolve a file from an explicit path, RubyGems metadata, or $LOAD_PATH.
Class Method Details
.load(files:, root: nil, **_options) ⇒ Module
Evaluate one or more Ruby files inside a fresh anonymous module.
The source’s natural constant nesting is preserved inside the anonymous
module. For example, a file declaring module Auth; module Sanitizer; end
becomes anonymous_namespace::Auth::Sanitizer, not Object::Auth.
33 34 35 36 37 38 39 40 |
# File 'lib/anonymous_loader.rb', line 33 def load(files:, root: nil, **) namespace = Module.new Array(files).each do |file| path = (file, root) namespace.module_eval(File.read(path), path, 1) end namespace end |
.load_path(**options) ⇒ Module
Resolve and anonymously load a single file.
51 52 53 |
# File 'lib/anonymous_loader.rb', line 51 def load_path(**) load(files: resolve_path(**)) end |
.resolve_path(**options) ⇒ String
Resolve a file from an explicit path, RubyGems metadata, or $LOAD_PATH.
RubyGems metadata is preferred when available. The $LOAD_PATH fallback
supports Bundler standalone setups where files are loadable but the
dependency is absent from Gem.loaded_specs and GEM_PATH.
68 69 70 71 72 73 74 |
# File 'lib/anonymous_loader.rb', line 68 def resolve_path(**) request = path_request() resolved = explicit_path_for(request) || resolve_from_gemspec(request) || resolve_from_load_path(request) return resolved if resolved raise FileNotFoundError, (request) end |