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

VERSION =
"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject Also known as: enabled?

Returns the value of attribute enabled.



7
8
9
# File 'lib/onload.rb', line 7

def enabled
  @enabled
end

Class Method Details

.basename(file) ⇒ Object



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

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

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

  basename
end

.disableObject



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

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

.each_extensionObject



48
49
50
51
52
# File 'lib/onload.rb', line 48

def each_extension
  return to_enum(__method__) unless block_given?

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

.enableObject



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

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

.globObject



102
103
104
# File 'lib/onload.rb', line 102

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

.install!Object



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

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)


44
45
46
# File 'lib/onload.rb', line 44

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

.processorsObject



72
73
74
# File 'lib/onload.rb', line 72

def processors
  @processors ||= {}
end

.register(extension, processor_klass) ⇒ Object



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

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

.unprocessed_file_for(file) ⇒ Object



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

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



66
67
68
69
70
# File 'lib/onload.rb', line 66

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