Module: Onload::ActiveSupportDependenciesPatch

Included in:
ActiveSupport::Dependencies
Defined in:
lib/onload/ext/activesupport/dependencies.rb

Instance Method Summary collapse

Instance Method Details

#load_missing_constant(from_mod, const_name) ⇒ Object

For some reason, using autoload and a patched Kernel#load doesn’t work by itself for automatically loading unprocessed files. Due to what I can only surmise is one of the side-effects of autoload, requiring any unprocessed file that’s been marked by autoload will result in a NameError, i.e. Ruby reports the constant isn’t defined. Pretty surprising considering we’re literally in the process of defining that constant. The trick is to essentially undo the autoload by removing the constant just before loading the unprocessed file that defines it.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/onload/ext/activesupport/dependencies.rb', line 26

def load_missing_constant(from_mod, const_name)
  if require_path = from_mod.autoload?(const_name)
    path = search_for_file(require_path)

    if path && Onload.process?(path)
      from_mod.send(:remove_const, const_name)
      require require_path
      return from_mod.const_get(const_name)
    end
  end

  super
end

#search_for_file(path_suffix) ⇒ Object

Allow activesupport to find unprocessed files.



8
9
10
11
12
13
14
15
16
# File 'lib/onload/ext/activesupport/dependencies.rb', line 8

def search_for_file(path_suffix)
  autoload_paths.each do |root|
    path = ::File.join(root, path_suffix)
    unprocessed_path = Onload.unprocessed_file_for(path)
    return unprocessed_path if unprocessed_path
  end

  super
end