Module: Tapioca::Runtime::Trackers::Autoload

Extended by:
T::Sig, Tracker
Defined in:
lib/tapioca/runtime/trackers/autoload.rb

Constant Summary collapse

NOOP_METHOD =
->(*_args, **_kwargs, &_block) {}

Class Method Summary collapse

Methods included from Tracker

disable!, enabled?, extended, with_disabled_tracker

Class Method Details

.eager_load_all!Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/tapioca/runtime/trackers/autoload.rb', line 19

def eager_load_all!
  with_disabled_exits do
    until @constant_names_registered_for_autoload.empty?
      # Grab the next constant name
      constant_name = T.must(@constant_names_registered_for_autoload.shift)
      # Trigger autoload by constantizing the registered name
      Reflection.constantize(constant_name, inherit: true)
    end
  end
end

.register(constant_name) ⇒ Object



31
32
33
34
35
# File 'lib/tapioca/runtime/trackers/autoload.rb', line 31

def register(constant_name)
  return unless enabled?

  @constant_names_registered_for_autoload << constant_name
end

.with_disabled_exits(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tapioca/runtime/trackers/autoload.rb', line 42

def with_disabled_exits(&block)
  original_abort = Kernel.instance_method(:abort)
  original_exit = Kernel.instance_method(:exit)

  begin
    Kernel.define_method(:abort, NOOP_METHOD)
    Kernel.define_method(:exit, NOOP_METHOD)

    block.call
  ensure
    Kernel.define_method(:exit, original_exit)
    Kernel.define_method(:abort, original_abort)
  end
end