Class: Zeitwerk::Loader::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/zeitwerk/loader/file_system.rb

Overview

This private class encapsulates interactions with the file system.

It is used to list directories and check file types, and it encodes the conventions documented in the README.

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ FileSystem

: (Zeitwerk::Loader) -> void



11
12
13
# File 'lib/zeitwerk/loader/file_system.rb', line 11

def initialize(loader)
  @loader = loader
end

Instance Method Details

#dir?(path) ⇒ Boolean

: (String) -> bool

Returns:

  • (Boolean)


62
63
64
# File 'lib/zeitwerk/loader/file_system.rb', line 62

def dir?(path)
  File.directory?(path)
end

#hidden?(basename) ⇒ Boolean

: (String) -> bool

Returns:

  • (Boolean)


67
68
69
# File 'lib/zeitwerk/loader/file_system.rb', line 67

def hidden?(basename)
  basename.start_with?(".")
end

#ls(dir) ⇒ Object

: (String) { (String, String, Symbol) -> void } -> void



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zeitwerk/loader/file_system.rb', line 16

def ls(dir)
  children = relevant_dir_entries(dir)

  # The order in which a directory is listed depends on the file system.
  #
  # Since client code may run on different platforms, it seems convenient to
  # sort directory entries. This provides more deterministic behavior, with
  # consistent eager loading in particular.
  children.sort_by!(&:first)

  children.each do |basename, abspath, ftype|
    if :directory == ftype && !has_at_least_one_ruby_file?(abspath)
      @loader.__log { "directory #{abspath} is ignored because it has no Ruby files" }
      next
    end

    yield basename, abspath, ftype
  end
end

#rb_extension?(path) ⇒ Boolean

: (String) -> bool

Returns:

  • (Boolean)


57
58
59
# File 'lib/zeitwerk/loader/file_system.rb', line 57

def rb_extension?(path)
  path.end_with?(".rb")
end

#supported_ftype?(abspath) ⇒ Boolean

Encodes the documented conventions.

: (String) -> Symbol?

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/zeitwerk/loader/file_system.rb', line 48

def supported_ftype?(abspath)
  if rb_extension?(abspath)
    :file # By convention, we can avoid a syscall here.
  elsif dir?(abspath)
    :directory
  end
end

#walk_up(abspath) ⇒ Object

: (String) { (String) -> void } -> void



37
38
39
40
41
42
43
# File 'lib/zeitwerk/loader/file_system.rb', line 37

def walk_up(abspath)
  loop do
    yield abspath
    abspath, basename = File.split(abspath)
    break if basename == "/"
  end
end