Module: ActiveSupport::Dependencies::Loadable

Defined in:
lib/active_support/dependencies.rb

Overview

Object includes this module.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exclude_from(base) ⇒ Object


236
237
238
239
240
241
242
243
244
# File 'lib/active_support/dependencies.rb', line 236

def self.exclude_from(base)
  base.class_eval do
    define_method(:load, Kernel.instance_method(:load))
    private :load

    define_method(:require, Kernel.instance_method(:require))
    private :require
  end
end

.include_into(base) ⇒ Object


246
247
248
249
250
251
252
253
254
255
256
# File 'lib/active_support/dependencies.rb', line 246

def self.include_into(base)
  base.include(self)

  if base.instance_method(:load).owner == base
    base.remove_method(:load)
  end

  if base.instance_method(:require).owner == base
    base.remove_method(:require)
  end
end

Instance Method Details

#load_dependency(file) ⇒ Object

:nodoc:


293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/active_support/dependencies.rb', line 293

def load_dependency(file)
  if Dependencies.load? && Dependencies.constant_watch_stack.watching?
    descs = Dependencies.constant_watch_stack.watching.flatten.uniq

    Dependencies.new_constants_in(*descs) { yield }
  else
    yield
  end
rescue Exception => exception  # errors from loading file
  exception.blame_file! file if exception.respond_to? :blame_file!
  raise
end

#require_dependency(file_name, message = "No such file to load -- %s.rb") ⇒ Object

Warning: This method is obsolete in :zeitwerk mode. In :zeitwerk mode semantics match Ruby's and you do not need to be defensive with load order. Just refer to classes and modules normally. If the constant name is dynamic, camelize if needed, and constantize.

In :classic mode, interprets a file using mechanism and marks its defined constants as autoloaded. file_name can be either a string or respond to to_path.

In :classic mode, use this method in code that absolutely needs a certain constant to be defined at that point. A typical use case is to make constant name resolution deterministic for constants with the same relative name in different namespaces whose evaluation would depend on load order otherwise.

Engines that do not control the mode in which their parent application runs should call require_dependency where needed in case the runtime mode is :classic.


282
283
284
285
286
287
288
289
# File 'lib/active_support/dependencies.rb', line 282

def require_dependency(file_name, message = "No such file to load -- %s.rb")
  file_name = file_name.to_path if file_name.respond_to?(:to_path)
  unless file_name.is_a?(String)
    raise ArgumentError, "the file name must either be a String or implement #to_path -- you passed #{file_name.inspect}"
  end

  Dependencies.depend_on(file_name, message)
end

#require_or_load(file_name) ⇒ Object


258
259
260
# File 'lib/active_support/dependencies.rb', line 258

def require_or_load(file_name)
  Dependencies.require_or_load(file_name)
end

#unloadable(const_desc) ⇒ Object

Mark the given constant as unloadable. Unloadable constants are removed each time dependencies are cleared.

Note that marking a constant for unloading need only be done once. Setup or init scripts may list each unloadable constant that may need unloading; each constant will be removed for every subsequent clear, as opposed to for the first clear.

The provided constant descriptor may be a (non-anonymous) module or class, or a qualified constant name as a string or symbol.

Returns true if the constant was not previously marked for unloading, false otherwise.


319
320
321
# File 'lib/active_support/dependencies.rb', line 319

def unloadable(const_desc)
  Dependencies.mark_for_unload const_desc
end