-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
gem_loader.rb
71 lines (52 loc) · 1.96 KB
/
gem_loader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
module Zeitwerk
# @private
class GemLoader < Loader
include RealModName
# Users should not create instances directly, the public interface is
# `Zeitwerk::Loader.for_gem`.
private_class_method :new
# @private
# @sig (String, bool) -> Zeitwerk::GemLoader
def self.__new(root_file, namespace:, warn_on_extra_files:)
new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
end
# @sig (String, bool) -> void
def initialize(root_file, namespace:, warn_on_extra_files:)
super()
@namespace = namespace
@tag = File.basename(root_file, ".rb")
@tag = real_mod_name(namespace) + "-" + @tag unless namespace.equal?(Object)
@inflector = GemInflector.new(root_file)
@root_file = File.expand_path(root_file)
@root_dir = File.dirname(root_file)
@warn_on_extra_files = warn_on_extra_files
push_dir(@root_dir, namespace: namespace)
end
# @sig () -> void
def setup
warn_on_extra_files if @warn_on_extra_files
super
end
private
# @sig () -> void
def warn_on_extra_files
expected_namespace_dir = @root_file.delete_suffix(".rb")
ls(@root_dir) do |basename, abspath|
next if abspath == @root_file
next if abspath == expected_namespace_dir
basename_without_ext = basename.delete_suffix(".rb")
cname = cname_for(basename_without_ext, abspath, real_mod_name(@namespace))
ftype = dir?(abspath) ? "directory" : "file"
warn(<<~EOS)
WARNING: Zeitwerk defines the constant #{cname} after the #{ftype}
#{abspath}
To prevent that, please configure the loader to ignore it:
loader.ignore("\#{__dir__}/#{basename}")
Otherwise, there is a flag to silence this warning:
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
EOS
end
end
end
end