Module: ActiveSupport::Dependencies
Overview
:nodoc:
Defined Under Namespace
Modules: Blamable, ClassConstMissing, Loadable, ModuleConstMissing Classes: LoadingModule
Class Method Summary collapse
-
.load_once_paths ⇒ Object
Deprecated, use autoload_once_paths.
-
.load_once_paths=(paths) ⇒ Object
Deprecated, use autoload_once_paths=.
-
.load_paths ⇒ Object
Deprecated, use autoload_paths.
-
.load_paths=(paths) ⇒ Object
Deprecated, use autoload_paths=.
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 expect 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
- #depend_on(file_name, swallow_load_errors = false) ⇒ 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. -
#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.
-
#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
-
#uninherited_const_defined?(mod, const) ⇒ Boolean
:nodoc:.
-
#will_unload?(const_desc) ⇒ Boolean
Will the provided constant descriptor be unloaded?.
Class Method Details
.load_once_paths ⇒ Object
Deprecated, use autoload_once_paths.
47 48 49 50 |
# File 'lib/active_support/dependencies.rb', line 47 def self.load_once_paths ActiveSupport::Deprecation.warn("ActiveSupport::Dependencies.load_once_paths is deprecated and removed in Rails 3, please use autoload_once_paths instead", caller) autoload_once_paths end |
.load_once_paths=(paths) ⇒ Object
Deprecated, use autoload_once_paths=.
53 54 55 56 |
# File 'lib/active_support/dependencies.rb', line 53 def self.load_once_paths=(paths) ActiveSupport::Deprecation.warn("ActiveSupport::Dependencies.load_once_paths= is deprecated and removed in Rails 3, please use autoload_once_paths= instead", caller) self.autoload_once_paths = paths end |
.load_paths ⇒ Object
Deprecated, use autoload_paths.
30 31 32 33 |
# File 'lib/active_support/dependencies.rb', line 30 def self.load_paths ActiveSupport::Deprecation.warn("ActiveSupport::Dependencies.load_paths is deprecated, please use autoload_paths instead", caller) autoload_paths end |
.load_paths=(paths) ⇒ Object
Deprecated, use autoload_paths=.
36 37 38 39 |
# File 'lib/active_support/dependencies.rb', line 36 def self.load_paths=(paths) ActiveSupport::Deprecation.warn("ActiveSupport::Dependencies.load_paths= is deprecated, please use autoload_paths= instead", caller) self.autoload_paths = paths end |
Instance Method Details
#associate_with(file_name) ⇒ Object
255 256 257 |
# File 'lib/active_support/dependencies.rb', line 255 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 expect 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.
383 384 385 386 387 388 389 |
# File 'lib/active_support/dependencies.rb', line 383 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.
367 368 369 370 371 372 |
# File 'lib/active_support/dependencies.rb', line 367 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.
482 483 484 485 486 487 488 |
# File 'lib/active_support/dependencies.rb', line 482 def autoloaded?(desc) # No name => anonymous module. return false if desc.is_a?(Module) && desc.name.blank? name = to_constant_name desc return false unless qualified_const_defined? name return autoloaded_constants.include?(name) end |
#clear ⇒ Object
259 260 261 262 263 |
# File 'lib/active_support/dependencies.rb', line 259 def clear log_call loaded.clear remove_unloadable_constants! end |
#depend_on(file_name, swallow_load_errors = false) ⇒ Object
248 249 250 251 252 253 |
# File 'lib/active_support/dependencies.rb', line 248 def depend_on(file_name, swallow_load_errors = false) path = search_for_file(file_name) require_or_load(path || file_name) rescue LoadError raise unless swallow_load_errors end |
#hook! ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/active_support/dependencies.rb', line 230 def hook! Object.instance_eval { include Loadable } Module.instance_eval { include ModuleConstMissing } Class.instance_eval { include ClassConstMissing } Exception.instance_eval { include Blamable } true end |
#load? ⇒ Boolean
244 245 246 |
# File 'lib/active_support/dependencies.rb', line 244 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.
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/active_support/dependencies.rb', line 399 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 = load_without_new_constant_marking 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.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/active_support/dependencies.rb', line 424 def load_missing_constant(from_mod, const_name) log_call from_mod, const_name if from_mod == Kernel if ::Object.const_defined?(const_name) log "Returning Object::#{const_name} for Kernel::#{const_name}" return ::Object.const_get(const_name) else log "Substituting Object for Kernel" from_mod = Object end end # If we have an anonymous module, all we can do is attempt to load from Object. from_mod = Object if from_mod.name.blank? unless qualified_const_defined?(from_mod.name) && from_mod.name.constantize.object_id == from_mod.object_id raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" end raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if uninherited_const_defined?(from_mod, const_name) qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore name_error = NameError.new("uninitialized constant #{qualified_name}") 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 uninherited_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| uninherited_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) raise name_error end else raise name_error end end |
#load_once_path?(path) ⇒ Boolean
374 375 376 |
# File 'lib/active_support/dependencies.rb', line 374 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.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/active_support/dependencies.rb', line 334 def loadable_constants_for_path(path, bases = autoload_paths) path = $1 if path =~ /\A(.*)\.rb\Z/ = File.(path) bases.collect 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? nesting_camel = nesting.camelize begin qualified_const_defined?(nesting_camel) rescue NameError next end [ nesting_camel ] end.flatten.compact.uniq 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.
498 499 500 501 502 503 504 505 506 |
# File 'lib/active_support/dependencies.rb', line 498 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.
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/active_support/dependencies.rb', line 516 def new_constants_in(*descs) log_call(*descs) # Build the watch frames. Each frame is a tuple of # [module_name_as_string, constants_defined_elsewhere] watch_frames = descs.collect do |desc| if desc.is_a? Module mod_name = desc.name initial_constants = desc.local_constant_names elsif desc.is_a?(String) || desc.is_a?(Symbol) mod_name = desc.to_s # Handle the case where the module has yet to be defined. initial_constants = if qualified_const_defined?(mod_name) mod_name.constantize.local_constant_names else [] end else raise Argument, "#{desc.inspect} does not describe a module!" end [mod_name, initial_constants] end constant_watch_stack_mutex.synchronize do constant_watch_stack.concat watch_frames end aborting = true begin yield # Now yield to the code that is to define new constants. aborting = false ensure # Find the new constants. new_constants = watch_frames.collect do |mod_name, prior_constants| # Module still doesn't exist? Treat it as if it has no constants. next [] unless qualified_const_defined?(mod_name) mod = mod_name.constantize next [] unless mod.is_a? Module new_constants = mod.local_constant_names - prior_constants # Make sure no other frames takes credit for these constants. constant_watch_stack_mutex.synchronize do constant_watch_stack.each do |frame_name, constants| constants.concat new_constants if frame_name == mod_name end end new_constants.collect do |suffix| mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" end end.flatten log "New constants: #{new_constants * ', '}" if aborting log "Error during loading, removing partially loaded constants " new_constants.each { |name| remove_constant name } new_constants.clear end end return new_constants ensure # Remove the stack frames that we added. if defined?(watch_frames) && ! watch_frames.blank? frame_ids = watch_frames.collect { |frame| frame.object_id } constant_watch_stack_mutex.synchronize do constant_watch_stack.delete_if do |watch_frame| frame_ids.include? watch_frame.object_id end end end end |
#qualified_const_defined?(path) ⇒ Boolean
Is the provided constant path defined?
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/active_support/dependencies.rb', line 304 def qualified_const_defined?(path) raise NameError, "#{path.inspect} is not a valid constant name!" unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path names = path.to_s.split('::') names.shift if names.first.empty? # We can't use defined? because it will invoke const_missing for the parent # of the name we are checking. names.inject(Object) do |mod, name| return false unless uninherited_const_defined?(mod, name) mod.const_get name end return true end |
#qualified_name_for(mod, name) ⇒ Object
Return the constant path for the provided parent and constant name.
416 417 418 419 |
# File 'lib/active_support/dependencies.rb', line 416 def qualified_name_for(mod, name) mod_name = to_constant_name mod (%w(Object Kernel).include? mod_name) ? name.to_s : "#{mod_name}::#{name}" end |
#remove_constant(const) ⇒ Object
:nodoc:
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/active_support/dependencies.rb', line 617 def remove_constant(const) #:nodoc: return false unless qualified_const_defined? const const = $1 if /\A::(.*)\Z/ =~ const.to_s names = const.to_s.split('::') if names.size == 1 # It's under Object parent = Object else parent = (names[0..-2] * '::').constantize end log "removing constant #{const}" parent.instance_eval { remove_const names.last } return true end |
#remove_unloadable_constants! ⇒ Object
Remove the constants that have been autoloaded, and those that have been marked for unloading.
475 476 477 478 479 |
# File 'lib/active_support/dependencies.rb', line 475 def remove_unloadable_constants! autoloaded_constants.each { |const| remove_constant const } autoloaded_constants.clear explicitly_unloadable_constants.each { |const| remove_constant const } end |
#require_or_load(file_name, const_path = nil) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/active_support/dependencies.rb', line 265 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 iff 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.
356 357 358 359 360 361 362 363 |
# File 'lib/active_support/dependencies.rb', line 356 def search_for_file(path_suffix) path_suffix = path_suffix + '.rb' unless path_suffix.ends_with? '.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.
606 607 608 609 610 611 612 613 614 615 |
# File 'lib/active_support/dependencies.rb', line 606 def to_constant_name(desc) #:nodoc: name = case desc when String then desc.starts_with?('::') ? desc[2..-1] : desc when Symbol then desc.to_s when Module raise ArgumentError, "Anonymous modules have no name to be referenced by" if desc.name.blank? desc.name else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" end end |
#unhook! ⇒ Object
238 239 240 241 242 |
# File 'lib/active_support/dependencies.rb', line 238 def unhook! ModuleConstMissing.excluded(Module) Loadable.excluded(Object) true end |
#uninherited_const_defined?(mod, const) ⇒ Boolean
:nodoc:
323 324 325 |
# File 'lib/active_support/dependencies.rb', line 323 def uninherited_const_defined?(mod, const) mod.const_defined?(const) end |
#will_unload?(const_desc) ⇒ Boolean
Will the provided constant descriptor be unloaded?
491 492 493 494 |
# File 'lib/active_support/dependencies.rb', line 491 def will_unload?(const_desc) autoloaded?(const_desc) || explicitly_unloadable_constants.include?(to_constant_name(const_desc)) end |