Module: Onload

Defined in:
lib/onload.rb,
lib/onload/file.rb,
lib/onload/config.rb,
lib/onload/railtie.rb,
lib/onload/version.rb,
lib/onload/ignore_file.rb,
lib/onload/core_ext/kernel.rb,
lib/onload/ext/zeitwerk/loader.rb,
lib/onload/ext/bootsnap/autoload.rb,
lib/onload/ext/activesupport/dependencies.rb

Defined Under Namespace

Modules: ActiveSupportDependenciesPatch, BootsnapAutoloadPatch, KernelLoadPatch, KernelRequirePatch, ZeitwerkFileSystemPatch, ZeitwerkLoaderHelpers, ZeitwerkLoaderPatch Classes: Config, File, IgnoreFile, MalformedIgnoreFileError, Railtie

Constant Summary collapse

UNLOADABLE_EXTENSIONS =
%w(.bundle .so .dll).freeze
VERSION =
"1.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject Also known as: enabled?

Returns the value of attribute enabled.



11
12
13
# File 'lib/onload.rb', line 11

def enabled
  @enabled
end

.installedObject (readonly) Also known as: installed?

Returns the value of attribute installed.



14
15
16
# File 'lib/onload.rb', line 14

def installed
  @installed
end

Class Method Details

.basename(file) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/onload.rb', line 85

def basename(file)
  basename = ::File.basename(file)

  if (idx = basename.index("."))
    return basename[0...idx]
  end

  basename
end

.configObject



115
116
117
# File 'lib/onload.rb', line 115

def config
  @config ||= Config.new
end

.disableObject



95
96
97
98
99
100
101
# File 'lib/onload.rb', line 95

def disable
  old_enabled = enabled?
  self.enabled = false
  yield
ensure
  self.enabled = old_enabled
end

.each_extensionObject



57
58
59
60
61
# File 'lib/onload.rb', line 57

def each_extension
  return to_enum(__method__) unless block_given?

  processors.each { |ext, _| yield ext }
end

.enableObject



103
104
105
106
107
108
109
# File 'lib/onload.rb', line 103

def enable
  old_enabled = enabled?
  self.enabled = true
  yield
ensure
  self.enabled = old_enabled
end

.globObject



111
112
113
# File 'lib/onload.rb', line 111

def glob
  @glob ||= "*{#{each_extension.to_a.join(",")}}"
end

.install!Object



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
# File 'lib/onload.rb', line 21

def install!
  if Kernel.const_defined?(:Rails)
    require "onload/railtie"

    if Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled?
      require "onload/core_ext/kernel_zeitwerk"
      require "onload/ext/zeitwerk/loader"
    else
      require "onload/core_ext/kernel"
      require "onload/ext/activesupport/dependencies"
    end
  else
    begin
      require "zeitwerk"
    rescue LoadError
      require "onload/core_ext/kernel"
    else
      require "onload/core_ext/kernel_zeitwerk"
      require "onload/ext/zeitwerk/loader"
    end
  end

  begin
    require "bootsnap"
  rescue LoadError
  else
    require "onload/ext/bootsnap/autoload"
  end

  @installed = true
end

.process?(path) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/onload.rb', line 53

def process?(path)
  each_extension.any? { |ext| path.end_with?(ext) }
end

.processorsObject



81
82
83
# File 'lib/onload.rb', line 81

def processors
  @processors ||= {}
end

.register(extension, processor_klass) ⇒ Object



17
18
19
# File 'lib/onload.rb', line 17

def register(extension, processor_klass)
  processors[extension] = processor_klass
end

.unprocessed_file_for(file) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/onload.rb', line 63

def unprocessed_file_for(file)
  base_name = basename(file)

  unprocessed_files_in(::File.dirname(file)).each do |existing_file|
    if basename(existing_file) == base_name
      return existing_file
    end
  end

  nil
end

.unprocessed_files_in(path) ⇒ Object



75
76
77
78
79
# File 'lib/onload.rb', line 75

def unprocessed_files_in(path)
  path_cache[path] ||= begin
    Dir.glob(::File.join(path, glob))
  end
end

.with_config(override_hash) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/onload.rb', line 119

def with_config(override_hash)
  new_config = config.dup.tap do |new_config|
    override_hash.each_pair do |k, v|
      new_config.send("#{k}=", v)
    end
  end

  old_config = config
  @config = new_config

  yield

  nil
ensure
  @config = old_config
end