Sha256: 10f7f8c0f4195a323147fa62685540de590e1adfec6b4aea11c5bc538afb68be

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

# We need to override autoload, because MRI has special behaviors associated with it
# that we can't reuse, hence we need to do workarounds.
#
# Basically, when trying to use a constant set to be autoloaded in an optionnal way, like:
#   * module A; ...; end
#   * A ||= 1
# When autoloading the file, the above won't work and will raise a "uninitialized constant A"
# because ruby doesn't understand that custom require is currently requiring the correct file.
#
# Our solution is to track autoloads ourself, and when requiring a path that has autoloads,
# we remove the autoloads from the constants first.

require 'binding_of_caller'

module DeepCover
  module KernelAutoloadOverride
    def autoload(name, path)
      mod = binding.of_caller(1).eval('Module.nesting').first || Object
      DeepCover.autoload_tracker.add(mod, name, path)
      mod.autoload_without_deep_cover(name, path)
    end

    extend ModuleOverride
    override ::Kernel, ::Kernel.singleton_class
  end

  module ModuleAutoloadOverride
    def autoload(name, path)
      DeepCover.autoload_tracker.add(self, name, path)
      autoload_without_deep_cover(name, path)
    end

    extend ModuleOverride
    override Module
  end

  module AutoloadOverride
    def self.active=(flag)
      KernelAutoloadOverride.active = ModuleAutoloadOverride.active = flag
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
deep-cover-0.5.2 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.5.1 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.5.0 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.4.0 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.3.0 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.2.0 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.1.16 lib/deep_cover/core_ext/autoload_overrides.rb
deep-cover-0.1.15 lib/deep_cover/core_ext/autoload_overrides.rb