Module: ActiveSupport::Dependencies
Overview
:nodoc:
Defined Under Namespace
Modules: Blamable, Loadable, ModuleConstMissing Classes: ClassCache, WatchStack
Constant Summary collapse
- Reference =
ClassCache.new
Instance Method Summary collapse
- #associate_with(file_name) ⇒ Object
-
#autoload_module!(into, const_name, qualified_name, path_suffix) ⇒ Object
Attempt to autoload the provided module name by searching for a directory matching the expected path suffix.
-
#autoloadable_module?(path_suffix) ⇒ Boolean
Does the provided path_suffix correspond to an autoloadable module? Instead of returning a boolean, the autoload base for this module is returned.
-
#autoloaded?(desc) ⇒ Boolean
Determine if the given constant has been automatically loaded.
- #clear ⇒ Object
-
#constantize(name) ⇒ Object
Get the reference for class named
name
. - #depend_on(file_name, swallow_load_errors = false, message = "No such file to load -- %s.rb") ⇒ Object
- #hook! ⇒ Object
- #load? ⇒ Boolean
-
#load_file(path, const_paths = loadable_constants_for_path(path)) ⇒ Object
Load the file at the provided path.
-
#load_missing_constant(from_mod, const_name) ⇒ Object
Load the constant named
const_name
which is missing fromfrom_mod
. - #load_once_path?(path) ⇒ Boolean
-
#loadable_constants_for_path(path, bases = autoload_paths) ⇒ Object
Given
path
, a filesystem path to a ruby file, return an array of constant paths which would cause Dependencies to attempt to load this file. -
#local_const_defined?(mod, const) ⇒ Boolean
:nodoc:.
-
#mark_for_unload(const_desc) ⇒ Object
Mark the provided constant name for unloading.
-
#new_constants_in(*descs) ⇒ Object
Run the provided block and detect the new constants that were loaded during its execution.
-
#qualified_const_defined?(path) ⇒ Boolean
Is the provided constant path defined?.
-
#qualified_name_for(mod, name) ⇒ Object
Return the constant path for the provided parent and constant name.
- #ref(name) ⇒ Object
-
#reference(klass) ⇒ Object
Store a reference to a class
klass
. -
#remove_constant(const) ⇒ Object
:nodoc:.
-
#remove_unloadable_constants! ⇒ Object
Remove the constants that have been autoloaded, and those that have been marked for unloading.
- #require_or_load(file_name, const_path = nil) ⇒ Object
-
#search_for_file(path_suffix) ⇒ Object
Search for a file in autoload_paths matching the provided suffix.
-
#to_constant_name(desc) ⇒ Object
Convert the provided const desc to a qualified constant name (as a string).
- #unhook! ⇒ Object
-
#will_unload?(const_desc) ⇒ Boolean
Will the provided constant descriptor be unloaded?.
Instance Method Details
#associate_with(file_name) ⇒ Object
312 313 314 |
# File 'lib/active_support/dependencies.rb', line 312 def associate_with(file_name) depend_on(file_name, true) end |
#autoload_module!(into, const_name, qualified_name, path_suffix) ⇒ Object
Attempt to autoload the provided module name by searching for a directory matching the expected path suffix. If found, the module is created and assigned to into
‘s constants with the name const_name
. Provided that the directory was loaded from a reloadable base path, it is added to the set of constants that are to be unloaded.
433 434 435 436 437 438 439 |
# File 'lib/active_support/dependencies.rb', line 433 def autoload_module!(into, const_name, qualified_name, path_suffix) return nil unless base_path = autoloadable_module?(path_suffix) mod = Module.new into.const_set const_name, mod autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path) return mod end |
#autoloadable_module?(path_suffix) ⇒ Boolean
Does the provided path_suffix correspond to an autoloadable module? Instead of returning a boolean, the autoload base for this module is returned.
417 418 419 420 421 422 |
# File 'lib/active_support/dependencies.rb', line 417 def autoloadable_module?(path_suffix) autoload_paths.each do |load_path| return load_path if File.directory? File.join(load_path, path_suffix) end nil end |
#autoloaded?(desc) ⇒ Boolean
Determine if the given constant has been automatically loaded.
599 600 601 602 603 604 605 |
# File 'lib/active_support/dependencies.rb', line 599 def autoloaded?(desc) # No name => anonymous module. return false if desc.is_a?(Module) && desc.anonymous? name = to_constant_name desc return false unless qualified_const_defined? name return autoloaded_constants.include?(name) end |
#clear ⇒ Object
316 317 318 319 320 |
# File 'lib/active_support/dependencies.rb', line 316 def clear log_call loaded.clear remove_unloadable_constants! end |
#constantize(name) ⇒ Object
Get the reference for class named name
.
594 595 596 |
# File 'lib/active_support/dependencies.rb', line 594 def constantize(name) Reference.get(name) end |
#depend_on(file_name, swallow_load_errors = false, message = "No such file to load -- %s.rb") ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/active_support/dependencies.rb', line 300 def depend_on(file_name, swallow_load_errors = false, = "No such file to load -- %s.rb") path = search_for_file(file_name) require_or_load(path || file_name) rescue LoadError => load_error unless swallow_load_errors if file_name = load_error.[/ -- (.*?)(\.rb)?$/, 1] raise LoadError.new( % file_name).copy_blame!(load_error) end raise end end |
#hook! ⇒ Object
283 284 285 286 287 288 |
# File 'lib/active_support/dependencies.rb', line 283 def hook! Object.class_eval { include Loadable } Module.class_eval { include ModuleConstMissing } Exception.class_eval { include Blamable } true end |
#load? ⇒ Boolean
296 297 298 |
# File 'lib/active_support/dependencies.rb', line 296 def load? mechanism == :load end |
#load_file(path, const_paths = loadable_constants_for_path(path)) ⇒ Object
Load the file at the provided path. const_paths
is a set of qualified constant names. When loading the file, Dependencies will watch for the addition of these constants. Each that is defined will be marked as autoloaded, and will be removed when Dependencies.clear is next called.
If the second parameter is left off, then Dependencies will construct a set of names that the file at path
may define. See loadable_constants_for_path
for more details.
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/active_support/dependencies.rb', line 449 def load_file(path, const_paths = loadable_constants_for_path(path)) log_call path, const_paths const_paths = [const_paths].compact unless const_paths.is_a? Array parent_paths = const_paths.collect { |const_path| /(.*)::[^:]+\Z/ =~ const_path ? $1 : :Object } result = nil newly_defined_paths = new_constants_in(*parent_paths) do result = Kernel.load path end autoloaded_constants.concat newly_defined_paths unless load_once_path?(path) autoloaded_constants.uniq! log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty? return result end |
#load_missing_constant(from_mod, const_name) ⇒ Object
Load the constant named const_name
which is missing from from_mod
. If it is not possible to load the constant into from_mod, try its parent module using const_missing.
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/active_support/dependencies.rb', line 474 def load_missing_constant(from_mod, const_name) log_call from_mod, const_name unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod) raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" end raise NameError, "#{from_mod} is not missing constant #{const_name}!" if local_const_defined?(from_mod, const_name) qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore file_path = search_for_file(path_suffix) if file_path && ! loaded.include?(File.(file_path)) # We found a matching file to load require_or_load file_path raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless local_const_defined?(from_mod, const_name) return from_mod.const_get(const_name) elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) return mod elsif (parent = from_mod.parent) && parent != from_mod && ! from_mod.parents.any? { |p| local_const_defined?(p, const_name) } # If our parents do not have a constant named +const_name+ then we are free # to attempt to load upwards. If they do have such a constant, then this # const_missing must be due to from_mod::const_name, which should not # return constants from from_mod's parents. begin return parent.const_missing(const_name) rescue NameError => e raise unless e.missing_name? qualified_name_for(parent, const_name) end end raise NameError, "uninitialized constant #{qualified_name}", caller.reject {|l| l.starts_with? __FILE__ } end |
#load_once_path?(path) ⇒ Boolean
424 425 426 |
# File 'lib/active_support/dependencies.rb', line 424 def load_once_path?(path) autoload_once_paths.any? { |base| path.starts_with? base } end |
#loadable_constants_for_path(path, bases = autoload_paths) ⇒ Object
Given path
, a filesystem path to a ruby file, return an array of constant paths which would cause Dependencies to attempt to load this file.
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/active_support/dependencies.rb', line 384 def loadable_constants_for_path(path, bases = autoload_paths) path = $1 if path =~ /\A(.*)\.rb\Z/ = File.(path) paths = [] bases.each do |root| = File.(root) next unless %r{\A#{Regexp.escape()}(/|\\)} =~ nesting = [(.size)..-1] nesting = nesting[1..-1] if nesting && nesting[0] == ?/ next if nesting.blank? paths << nesting.camelize end paths.uniq! paths end |
#local_const_defined?(mod, const) ⇒ Boolean
:nodoc:
373 374 375 |
# File 'lib/active_support/dependencies.rb', line 373 def local_const_defined?(mod, const) mod.const_defined?(const) end |
#mark_for_unload(const_desc) ⇒ Object
Mark the provided constant name for unloading. This constant will be unloaded on each request, not just the next one.
615 616 617 618 619 620 621 622 623 |
# File 'lib/active_support/dependencies.rb', line 615 def mark_for_unload(const_desc) name = to_constant_name const_desc if explicitly_unloadable_constants.include? name return false else explicitly_unloadable_constants << name return true end end |
#new_constants_in(*descs) ⇒ Object
Run the provided block and detect the new constants that were loaded during its execution. Constants may only be regarded as ‘new’ once – so if the block calls new_constants_in
again, then the constants defined within the inner call will not be reported in this one.
If the provided block does not run to completion, and instead raises an exception, any new constants are regarded as being only partially defined and will be removed immediately.
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/active_support/dependencies.rb', line 633 def new_constants_in(*descs) log_call(*descs) constant_watch_stack.watch_namespaces(descs) aborting = true begin yield # Now yield to the code that is to define new constants. aborting = false ensure new_constants = constant_watch_stack.new_constants log "New constants: #{new_constants * ', '}" return new_constants unless aborting log "Error during loading, removing partially loaded constants " new_constants.each {|c| remove_constant(c) }.clear end return [] end |
#qualified_const_defined?(path) ⇒ Boolean
Is the provided constant path defined?
361 362 363 364 365 366 367 368 |
# File 'lib/active_support/dependencies.rb', line 361 def qualified_const_defined?(path) names = path.sub(/^::/, '').to_s.split('::') names.inject(Object) do |mod, name| return false unless local_const_defined?(mod, name) mod.const_get name end end |
#qualified_name_for(mod, name) ⇒ Object
Return the constant path for the provided parent and constant name.
466 467 468 469 |
# File 'lib/active_support/dependencies.rb', line 466 def qualified_name_for(mod, name) mod_name = to_constant_name mod mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}" end |
#ref(name) ⇒ Object
583 584 585 |
# File 'lib/active_support/dependencies.rb', line 583 def ref(name) Reference.new(name) end |
#reference(klass) ⇒ Object
Store a reference to a class klass
.
589 590 591 |
# File 'lib/active_support/dependencies.rb', line 589 def reference(klass) Reference.store klass end |
#remove_constant(const) ⇒ Object
:nodoc:
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
# File 'lib/active_support/dependencies.rb', line 668 def remove_constant(const) #:nodoc: return false unless qualified_const_defined? const # Normalize ::Foo, Foo, Object::Foo, and ::Object::Foo to Object::Foo names = const.to_s.sub(/^::(Object)?/, 'Object::').split("::") to_remove = names.pop parent = Inflector.constantize(names * '::') log "removing constant #{const}" constantized = constantize(const) constantized.before_remove_const if constantized.respond_to?(:before_remove_const) parent.instance_eval { remove_const to_remove } return true end |
#remove_unloadable_constants! ⇒ Object
Remove the constants that have been autoloaded, and those that have been marked for unloading. Before each constant is removed a callback is sent to its class/module if it implements before_remove_const
.
The callback implementation should be restricted to cleaning up caches, etc. as the environment will be in an inconsistent state, e.g. other constants may have already been unloaded and not accessible.
519 520 521 522 523 524 |
# File 'lib/active_support/dependencies.rb', line 519 def remove_unloadable_constants! autoloaded_constants.each { |const| remove_constant const } autoloaded_constants.clear Reference.clear! explicitly_unloadable_constants.each { |const| remove_constant const } end |
#require_or_load(file_name, const_path = nil) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/active_support/dependencies.rb', line 322 def require_or_load(file_name, const_path = nil) log_call file_name, const_path file_name = $1 if file_name =~ /^(.*)\.rb$/ = File.(file_name) return if loaded.include?() # Record that we've seen this file *before* loading it to avoid an # infinite loop with mutual dependencies. loaded << begin if load? log "loading #{file_name}" # Enable warnings if this file has not been loaded before and # warnings_on_first_load is set. load_args = ["#{file_name}.rb"] load_args << const_path unless const_path.nil? if !warnings_on_first_load or history.include?() result = load_file(*load_args) else enable_warnings { result = load_file(*load_args) } end else log "requiring #{file_name}" result = require file_name end rescue Exception loaded.delete raise end # Record history *after* loading so first load gets warnings. history << return result end |
#search_for_file(path_suffix) ⇒ Object
Search for a file in autoload_paths matching the provided suffix.
405 406 407 408 409 410 411 412 413 |
# File 'lib/active_support/dependencies.rb', line 405 def search_for_file(path_suffix) path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb") autoload_paths.each do |root| path = File.join(root, path_suffix) return path if File.file? path end nil # Gee, I sure wish we had first_match ;-) end |
#to_constant_name(desc) ⇒ Object
Convert the provided const desc to a qualified constant name (as a string). A module, class, symbol, or string may be provided.
657 658 659 660 661 662 663 664 665 666 |
# File 'lib/active_support/dependencies.rb', line 657 def to_constant_name(desc) #:nodoc: case desc when String then desc.sub(/^::/, '') when Symbol then desc.to_s when Module desc.name.presence || raise(ArgumentError, "Anonymous modules have no name to be referenced by") else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" end end |
#unhook! ⇒ Object
290 291 292 293 294 |
# File 'lib/active_support/dependencies.rb', line 290 def unhook! ModuleConstMissing.exclude_from(Module) Loadable.exclude_from(Object) true end |
#will_unload?(const_desc) ⇒ Boolean
Will the provided constant descriptor be unloaded?
608 609 610 611 |
# File 'lib/active_support/dependencies.rb', line 608 def will_unload?(const_desc) autoloaded?(const_desc) || explicitly_unloadable_constants.include?(to_constant_name(const_desc)) end |