Module: Onload

Defined in:
lib/onload.rb,
lib/onload/file.rb,
lib/onload/railtie.rb,
lib/onload/version.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, ZeitwerkLoaderPatch Classes: File, Railtie

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject Also known as: enabled?

Returns the value of attribute enabled.



9
10
11
# File 'lib/onload.rb', line 9

def enabled
  @enabled
end

Class Method Details

.basename(file) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/onload.rb', line 78

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

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

  basename
end

.disableObject



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

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

.each_extensionObject



50
51
52
53
54
# File 'lib/onload.rb', line 50

def each_extension
  return to_enum(__method__) unless block_given?

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

.enableObject



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

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

.globObject



104
105
106
# File 'lib/onload.rb', line 104

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

.install!Object



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

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
end

.process?(path) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/onload.rb', line 46

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

.processorsObject



74
75
76
# File 'lib/onload.rb', line 74

def processors
  @processors ||= {}
end

.register(extension, processor_klass) ⇒ Object



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

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

.unprocessed_file_for(file) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/onload.rb', line 56

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



68
69
70
71
72
# File 'lib/onload.rb', line 68

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