class Autoloaded::LoadPathedDirectory
Enumerates the source files in a directory, relativizing their paths using the Ruby load path.
@since 1.3
@api private
Constants
- SOURCE_FILE_EXTENSION
The file extension of source files.
Attributes
The full path of a source directory.
@return [String] a directory path
Public Class Methods
Constructs a new LoadPathedDirectory with the specified path.
@param [String] path the value of #path
@raise [ArgumentError] path is nil
or a relative path
# File lib/autoloaded/load_pathed_directory.rb, line 43 def initialize(path) raise ::ArgumentError, "can't be nil" if path.nil? @path = path.dup.freeze if ::Pathname.new(@path).relative? raise ::ArgumentError, "can't be relative" end end
Private Class Methods
# File lib/autoloaded/load_pathed_directory.rb, line 16 def self.join(path1, path2) paths = [path1, path2].reject do |path| path.to_s.empty? end (paths.length < 2) ? paths.first : ::File.join(*paths) end
# File lib/autoloaded/load_pathed_directory.rb, line 23 def self.ruby_load_paths $: end
# File lib/autoloaded/load_pathed_directory.rb, line 27 def self.source_basename(source_filename) ::File.basename source_filename, SOURCE_FILE_EXTENSION end
Public Instance Methods
Enumerates the source files in #path, relativizing their paths if possible using the longest applicable entry in the Ruby load path (that is, $:
). File names are rendered without SOURCE_FILE_EXTENSION. Yielded paths are guaranteed usable in require
statements unless elements of the Ruby load path are removed or changed.
@yield [String] each source file in #path, formatted for use in require
statements
@return [LoadPathedDirectory] the LoadPathedDirectory
@see path
@see ruby-doc.org/core/Kernel.html#method-i-require Kernel#require
# File lib/autoloaded/load_pathed_directory.rb, line 65 def each_source_filename if (ruby_load_path = closest_ruby_load_path) ::Dir.chdir ruby_load_path do glob = self.class.join(path_from(ruby_load_path), "*#{SOURCE_FILE_EXTENSION}") ::Dir.glob glob do |file| yield without_source_file_extension(file) end end else glob = self.class.join(path, "*#{SOURCE_FILE_EXTENSION}") ::Dir.glob glob do |file| yield without_source_file_extension(file) end end self end
Private Instance Methods
# File lib/autoloaded/load_pathed_directory.rb, line 86 def closest_ruby_load_path closest = self.class.ruby_load_paths.inject(score: 0) do |close, load_path| score = path.length - path_from(load_path).length (close[:score] < score) ? {score: score, load_path: load_path} : close end closest[:load_path] end
# File lib/autoloaded/load_pathed_directory.rb, line 94 def path_from(other_path) # Don't use Pathname#relative_path_from because we want to avoid introducing # double dots. The intent is to render the path as relative, if and only if # it is a subdirectory of 'other_path'. pattern = /^#{::Regexp.escape other_path.to_s.chomp(::File::SEPARATOR)}#{::Regexp.escape ::File::SEPARATOR}?/ path.gsub pattern, '' end
# File lib/autoloaded/load_pathed_directory.rb, line 102 def without_source_file_extension(path) if (dirname = ::File.dirname(path)) == '.' dirname = nil end basename = ::File.basename(path, SOURCE_FILE_EXTENSION) self.class.join dirname, basename end