module Zeitwerk::Loader::Helpers

Private Instance Methods

cdef?(parent, cname) click to toggle source

@sig (Module, Symbol) -> bool

# File lib/zeitwerk/loader/helpers.rb, line 130
def cdef?(parent, cname)
  parent.const_defined?(cname, false)
end
cget(parent, cname) click to toggle source

@raise [NameError] @sig (Module, Symbol) -> Object

# File lib/zeitwerk/loader/helpers.rb, line 136
def cget(parent, cname)
  parent.const_get(cname, false)
end
cpath(parent, cname) click to toggle source

Symbol#name was introduced in Ruby 3.0. It returns always the same frozen object, so we may save a few string allocations.

# File lib/zeitwerk/loader/helpers.rb, line 120
def cpath(parent, cname)
  Object == parent ? cname.name : "#{real_mod_name(parent)}::#{cname.name}"
end
dir?(path) click to toggle source

@sig (String) -> bool

# File lib/zeitwerk/loader/helpers.rb, line 69
def dir?(path)
  File.directory?(path)
end
has_at_least_one_ruby_file?(dir) click to toggle source

@sig (String) -> bool

# File lib/zeitwerk/loader/helpers.rb, line 47
def has_at_least_one_ruby_file?(dir)
  to_visit = [dir]

  while dir = to_visit.shift
    ls(dir) do |_basename, abspath|
      if dir?(abspath)
        to_visit << abspath
      else
        return true
      end
    end
  end

  false
end
hidden?(basename) click to toggle source

@sig (String) -> bool

# File lib/zeitwerk/loader/helpers.rb, line 74
def hidden?(basename)
  basename.start_with?(".")
end
log(message) click to toggle source

@sig (String) -> void

# File lib/zeitwerk/loader/helpers.rb, line 9
def log(message)
  method_name = logger.respond_to?(:debug) ? :debug : :call
  logger.send(method_name, "Zeitwerk@#{tag}: #{message}")
end
ls(dir) { |basename, freeze| ... } click to toggle source

@sig (String) { (String, String) -> void } -> void

# File lib/zeitwerk/loader/helpers.rb, line 17
def ls(dir)
  children = Dir.children(dir)

  # The order in which a directory is listed depends on the file system.
  #
  # Since client code may run in different platforms, it seems convenient to
  # order directory entries. This provides consistent eager loading across
  # platforms, for example.
  children.sort!

  children.each do |basename|
    next if hidden?(basename)

    abspath = File.join(dir, basename)
    next if ignored_paths.member?(abspath)

    if dir?(abspath)
      next if roots.key?(abspath)
      next if !has_at_least_one_ruby_file?(abspath)
    else
      next unless ruby?(abspath)
    end

    # We freeze abspath because that saves allocations when passed later to
    # File methods. See #125.
    yield basename, abspath.freeze
  end
end
ruby?(path) click to toggle source

@sig (String) -> bool

# File lib/zeitwerk/loader/helpers.rb, line 64
def ruby?(path)
  path.end_with?(".rb")
end
strict_autoload_path(parent, cname) click to toggle source
# File lib/zeitwerk/loader/helpers.rb, line 107
def strict_autoload_path(parent, cname)
  parent.autoload?(cname) if cdef?(parent, cname)
end
walk_up(abspath) { |abspath| ... } click to toggle source

@sig (String) { (String) -> void } -> void

# File lib/zeitwerk/loader/helpers.rb, line 79
def walk_up(abspath)
  loop do
    yield abspath
    abspath, basename = File.split(abspath)
    break if basename == "/"
  end
end