2005-11-23 16:31:51 -05:00
|
|
|
require 'set'
|
2007-10-02 01:32:14 -04:00
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
|
|
require 'active_support/core_ext/load_error'
|
|
|
|
require 'active_support/core_ext/kernel'
|
2004-12-29 16:03:21 -05:00
|
|
|
|
2005-07-06 06:43:42 -04:00
|
|
|
module Dependencies #:nodoc:
|
2004-12-29 16:03:21 -05:00
|
|
|
extend self
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2005-11-24 15:26:01 -05:00
|
|
|
# Should we turn on Ruby warnings on the first load of dependent files?
|
|
|
|
mattr_accessor :warnings_on_first_load
|
|
|
|
self.warnings_on_first_load = false
|
|
|
|
|
2005-11-23 16:31:51 -05:00
|
|
|
# All files ever loaded.
|
|
|
|
mattr_accessor :history
|
|
|
|
self.history = Set.new
|
|
|
|
|
|
|
|
# All files currently loaded.
|
2004-12-29 16:03:21 -05:00
|
|
|
mattr_accessor :loaded
|
2005-11-23 16:31:51 -05:00
|
|
|
self.loaded = Set.new
|
2004-12-29 16:03:21 -05:00
|
|
|
|
2005-11-23 16:31:51 -05:00
|
|
|
# Should we load files or require them?
|
2004-12-29 16:03:21 -05:00
|
|
|
mattr_accessor :mechanism
|
2005-11-23 16:31:51 -05:00
|
|
|
self.mechanism = :load
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
# The set of directories from which we may automatically load files. Files
|
|
|
|
# under these directories will be reloaded on each request in development mode,
|
|
|
|
# unless the directory also appears in load_once_paths.
|
|
|
|
mattr_accessor :load_paths
|
|
|
|
self.load_paths = []
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
# The set of directories from which automatically loaded constants are loaded
|
|
|
|
# only once. All directories in this set must also be present in +load_paths+.
|
|
|
|
mattr_accessor :load_once_paths
|
|
|
|
self.load_once_paths = []
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-12 23:29:04 -04:00
|
|
|
# An array of qualified constant names that have been loaded. Adding a name to
|
|
|
|
# this array will cause it to be unloaded the next time Dependencies are cleared.
|
2006-08-08 17:21:04 -04:00
|
|
|
mattr_accessor :autoloaded_constants
|
|
|
|
self.autoloaded_constants = []
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-10-15 19:32:31 -04:00
|
|
|
# An array of constant names that need to be unloaded on every request. Used
|
|
|
|
# to allow arbitrary constants to be marked for unloading.
|
|
|
|
mattr_accessor :explicitly_unloadable_constants
|
|
|
|
self.explicitly_unloadable_constants = []
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-12 23:29:04 -04:00
|
|
|
# Set to true to enable logging of const_missing and file loads
|
|
|
|
mattr_accessor :log_activity
|
|
|
|
self.log_activity = false
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
# An internal stack used to record which constants are loaded by any block.
|
|
|
|
mattr_accessor :constant_watch_stack
|
|
|
|
self.constant_watch_stack = []
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2005-01-24 05:58:17 -05:00
|
|
|
def load?
|
|
|
|
mechanism == :load
|
|
|
|
end
|
2005-11-23 16:31:51 -05:00
|
|
|
|
2004-12-29 16:03:21 -05:00
|
|
|
def depend_on(file_name, swallow_load_errors = false)
|
2006-08-29 02:52:39 -04:00
|
|
|
path = search_for_file(file_name)
|
2006-08-08 17:21:04 -04:00
|
|
|
require_or_load(path || file_name)
|
2005-11-28 15:39:26 -05:00
|
|
|
rescue LoadError
|
|
|
|
raise unless swallow_load_errors
|
2004-12-29 16:03:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def associate_with(file_name)
|
|
|
|
depend_on(file_name, true)
|
|
|
|
end
|
2005-11-23 16:31:51 -05:00
|
|
|
|
2004-12-29 16:03:21 -05:00
|
|
|
def clear
|
2006-08-12 23:29:04 -04:00
|
|
|
log_call
|
2005-11-23 16:31:51 -05:00
|
|
|
loaded.clear
|
2006-10-15 19:32:31 -04:00
|
|
|
remove_unloadable_constants!
|
2004-12-29 16:03:21 -05:00
|
|
|
end
|
2005-11-21 02:06:33 -05:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
def require_or_load(file_name, const_path = nil)
|
2006-08-12 23:29:04 -04:00
|
|
|
log_call file_name, const_path
|
2005-11-28 15:39:26 -05:00
|
|
|
file_name = $1 if file_name =~ /^(.*)\.rb$/
|
2006-08-08 18:08:09 -04:00
|
|
|
expanded = File.expand_path(file_name)
|
|
|
|
return if loaded.include?(expanded)
|
2005-11-23 16:31:51 -05:00
|
|
|
|
2005-11-28 15:39:26 -05:00
|
|
|
# Record that we've seen this file *before* loading it to avoid an
|
|
|
|
# infinite loop with mutual dependencies.
|
2006-08-08 18:08:09 -04:00
|
|
|
loaded << expanded
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2005-11-28 15:39:26 -05:00
|
|
|
if load?
|
2006-08-12 23:29:04 -04:00
|
|
|
log "loading #{file_name}"
|
2005-11-24 00:43:27 -05:00
|
|
|
begin
|
2005-11-24 15:26:01 -05:00
|
|
|
# Enable warnings iff this file has not been loaded before and
|
|
|
|
# warnings_on_first_load is set.
|
2006-08-08 17:21:04 -04:00
|
|
|
load_args = ["#{file_name}.rb"]
|
|
|
|
load_args << const_path unless const_path.nil?
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 18:08:09 -04:00
|
|
|
if !warnings_on_first_load or history.include?(expanded)
|
2006-08-16 16:33:31 -04:00
|
|
|
result = load_file(*load_args)
|
2005-11-24 00:43:27 -05:00
|
|
|
else
|
2006-08-16 16:33:31 -04:00
|
|
|
enable_warnings { result = load_file(*load_args) }
|
2005-11-24 00:43:27 -05:00
|
|
|
end
|
2006-08-17 13:19:11 -04:00
|
|
|
rescue Exception
|
2006-08-08 18:08:09 -04:00
|
|
|
loaded.delete expanded
|
2005-11-24 00:43:27 -05:00
|
|
|
raise
|
2005-11-23 16:31:51 -05:00
|
|
|
end
|
2005-11-21 02:06:33 -05:00
|
|
|
else
|
2006-08-12 23:29:04 -04:00
|
|
|
log "requiring #{file_name}"
|
2006-08-16 16:33:31 -04:00
|
|
|
result = require file_name
|
2005-11-21 02:06:33 -05:00
|
|
|
end
|
2005-11-23 16:31:51 -05:00
|
|
|
|
2005-11-24 00:43:27 -05:00
|
|
|
# Record history *after* loading so first load gets warnings.
|
2006-08-08 18:08:09 -04:00
|
|
|
history << expanded
|
2006-08-16 16:33:31 -04:00
|
|
|
return result
|
2006-04-02 23:06:23 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
# Is the provided constant path defined?
|
|
|
|
def qualified_const_defined?(path)
|
|
|
|
raise NameError, "#{path.inspect} is not a valid constant name!" unless
|
|
|
|
/^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2007-09-13 20:34:43 -04:00
|
|
|
names = path.to_s.split('::')
|
2006-08-16 13:50:52 -04:00
|
|
|
names.shift if names.first.empty?
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-16 13:50:52 -04:00
|
|
|
# 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 mod.const_defined? name
|
|
|
|
mod.const_get name
|
|
|
|
end
|
|
|
|
return true
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
# 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.
|
2008-03-30 01:02:25 -04:00
|
|
|
#
|
2006-12-24 09:53:35 -05:00
|
|
|
def loadable_constants_for_path(path, bases = load_paths)
|
2006-08-08 17:21:04 -04:00
|
|
|
path = $1 if path =~ /\A(.*)\.rb\Z/
|
|
|
|
expanded_path = File.expand_path(path)
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
bases.collect do |root|
|
2006-09-06 16:31:28 -04:00
|
|
|
expanded_root = File.expand_path(root)
|
|
|
|
next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
nesting = expanded_path[(expanded_root.size)..-1]
|
|
|
|
nesting = nesting[1..-1] if nesting && nesting[0] == ?/
|
|
|
|
next if nesting.blank?
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-09-06 16:31:28 -04:00
|
|
|
[
|
|
|
|
nesting.camelize,
|
|
|
|
# Special case: application.rb might define ApplicationControlller.
|
|
|
|
('ApplicationController' if nesting == 'application')
|
|
|
|
]
|
2006-09-02 17:13:30 -04:00
|
|
|
end.flatten.compact.uniq
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
# Search for a file in load_paths matching the provided suffix.
|
|
|
|
def search_for_file(path_suffix)
|
2006-08-08 17:21:04 -04:00
|
|
|
path_suffix = path_suffix + '.rb' unless path_suffix.ends_with? '.rb'
|
2006-08-29 02:52:39 -04:00
|
|
|
load_paths.each do |root|
|
2006-08-08 17:21:04 -04:00
|
|
|
path = File.join(root, path_suffix)
|
|
|
|
return path if File.file? path
|
|
|
|
end
|
|
|
|
nil # Gee, I sure wish we had first_match ;-)
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
# Does the provided path_suffix correspond to an autoloadable module?
|
2008-03-30 01:02:25 -04:00
|
|
|
# Instead of returning a boolean, the autoload base for this module is returned.
|
2006-08-08 17:21:04 -04:00
|
|
|
def autoloadable_module?(path_suffix)
|
2006-08-29 02:52:39 -04:00
|
|
|
load_paths.each do |load_path|
|
|
|
|
return load_path if File.directory? File.join(load_path, path_suffix)
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2006-08-29 02:52:39 -04:00
|
|
|
nil
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-12-24 09:53:35 -05:00
|
|
|
def load_once_path?(path)
|
|
|
|
load_once_paths.any? { |base| path.starts_with? base }
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
# 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.
|
|
|
|
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 load_once_paths.include?(base_path)
|
|
|
|
return mod
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
# 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.
|
2008-03-30 01:02:25 -04:00
|
|
|
#
|
2006-08-08 17:21:04 -04:00
|
|
|
# If the second parameter is left off, then Dependencies will construct a set
|
|
|
|
# of names that the file at +path+ may define. See
|
2006-08-29 02:52:39 -04:00
|
|
|
# +loadable_constants_for_path+ for more details.
|
|
|
|
def load_file(path, const_paths = loadable_constants_for_path(path))
|
2006-08-12 23:29:04 -04:00
|
|
|
log_call path, const_paths
|
2006-08-08 17:21:04 -04:00
|
|
|
const_paths = [const_paths].compact unless const_paths.is_a? Array
|
2006-11-01 18:21:13 -05:00
|
|
|
parent_paths = const_paths.collect { |const_path| /(.*)::[^:]+\Z/ =~ const_path ? $1 : :Object }
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
result = nil
|
|
|
|
newly_defined_paths = new_constants_in(*parent_paths) do
|
|
|
|
result = load_without_new_constant_marking path
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-12-24 09:53:35 -05:00
|
|
|
autoloaded_constants.concat newly_defined_paths unless load_once_path?(path)
|
2006-08-08 17:21:04 -04:00
|
|
|
autoloaded_constants.uniq!
|
2006-08-12 23:29:04 -04:00
|
|
|
log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty?
|
2006-08-16 16:33:31 -04:00
|
|
|
return result
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
# Return the constant path for the provided parent and constant name.
|
|
|
|
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
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
# Load the constant named +const_name+ which is missing from +from_mod+. If
|
2007-12-14 21:12:39 -05:00
|
|
|
# it is not possible to load the constant into from_mod, try its parent module
|
2006-08-08 17:21:04 -04:00
|
|
|
# using const_missing.
|
|
|
|
def load_missing_constant(from_mod, const_name)
|
2006-08-12 23:29:04 -04:00
|
|
|
log_call from_mod, const_name
|
2006-09-05 18:36:28 -04:00
|
|
|
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
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2006-08-16 16:54:25 -04:00
|
|
|
# If we have an anonymous module, all we can do is attempt to load from Object.
|
2007-09-27 07:01:20 -04:00
|
|
|
from_mod = Object if from_mod.name.blank?
|
|
|
|
|
2006-08-16 23:35:36 -04:00
|
|
|
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
|
2007-12-14 21:12:39 -05:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name)
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
qualified_name = qualified_name_for from_mod, const_name
|
|
|
|
path_suffix = qualified_name.underscore
|
|
|
|
name_error = NameError.new("uninitialized constant #{qualified_name}")
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-29 02:52:39 -04:00
|
|
|
file_path = search_for_file(path_suffix)
|
2006-08-08 18:08:09 -04:00
|
|
|
if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load
|
2006-08-29 02:52:39 -04:00
|
|
|
require_or_load file_path
|
2006-08-08 17:21:04 -04:00
|
|
|
raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless from_mod.const_defined?(const_name)
|
|
|
|
return from_mod.const_get(const_name)
|
2006-08-29 02:52:39 -04:00
|
|
|
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
|
2006-08-08 17:21:04 -04:00
|
|
|
return mod
|
|
|
|
elsif (parent = from_mod.parent) && parent != from_mod &&
|
|
|
|
! from_mod.parents.any? { |p| p.const_defined?(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
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-10-15 19:32:31 -04:00
|
|
|
# Remove the constants that have been autoloaded, and those that have been
|
|
|
|
# marked for unloading.
|
|
|
|
def remove_unloadable_constants!
|
|
|
|
autoloaded_constants.each { |const| remove_constant const }
|
|
|
|
autoloaded_constants.clear
|
|
|
|
explicitly_unloadable_constants.each { |const| remove_constant const }
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-08 17:21:04 -04:00
|
|
|
# Determine if the given constant has been automatically loaded.
|
|
|
|
def autoloaded?(desc)
|
2006-12-31 18:53:05 -05:00
|
|
|
# No name => anonymous module.
|
|
|
|
return false if desc.is_a?(Module) && desc.name.blank?
|
2006-08-08 17:21:04 -04:00
|
|
|
name = to_constant_name desc
|
|
|
|
return false unless qualified_const_defined? name
|
|
|
|
return autoloaded_constants.include?(name)
|
2006-08-05 18:52:15 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-10-15 19:32:31 -04:00
|
|
|
# Will the provided constant descriptor be unloaded?
|
|
|
|
def will_unload?(const_desc)
|
|
|
|
autoloaded?(desc) ||
|
|
|
|
explicitly_unloadable_constants.include?(to_constant_name(const_desc))
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-10-15 19:32:31 -04:00
|
|
|
# Mark the provided constant name for unloading. This constant will be
|
|
|
|
# unloaded on each request, not just the next one.
|
|
|
|
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
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
# 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.
|
2008-03-30 01:02:25 -04:00
|
|
|
#
|
2006-11-07 15:45:36 -05:00
|
|
|
# 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.
|
2006-11-01 18:21:13 -05:00
|
|
|
def new_constants_in(*descs)
|
|
|
|
log_call(*descs)
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
# 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
|
2007-12-22 13:23:37 -05:00
|
|
|
initial_constants = desc.local_constant_names
|
2006-11-01 18:21:13 -05:00
|
|
|
elsif desc.is_a?(String) || desc.is_a?(Symbol)
|
|
|
|
mod_name = desc.to_s
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
# Handle the case where the module has yet to be defined.
|
|
|
|
initial_constants = if qualified_const_defined?(mod_name)
|
2007-12-22 13:23:37 -05:00
|
|
|
mod_name.constantize.local_constant_names
|
2006-11-01 18:21:13 -05:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise Argument, "#{desc.inspect} does not describe a module!"
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
[mod_name, initial_constants]
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
constant_watch_stack.concat watch_frames
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
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)
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
mod = mod_name.constantize
|
|
|
|
next [] unless mod.is_a? Module
|
2007-12-22 13:23:37 -05:00
|
|
|
new_constants = mod.local_constant_names - prior_constants
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
# Make sure no other frames takes credit for these constants.
|
|
|
|
constant_watch_stack.each do |frame_name, constants|
|
|
|
|
constants.concat new_constants if frame_name == mod_name
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
new_constants.collect do |suffix|
|
|
|
|
mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}"
|
|
|
|
end
|
|
|
|
end.flatten
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
log "New constants: #{new_constants * ', '}"
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-07 15:45:36 -05:00
|
|
|
if aborting
|
|
|
|
log "Error during loading, removing partially loaded constants "
|
|
|
|
new_constants.each { |name| remove_constant name }
|
|
|
|
new_constants.clear
|
2006-11-01 18:21:13 -05:00
|
|
|
end
|
2006-11-07 15:45:36 -05:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-11-01 18:21:13 -05:00
|
|
|
return new_constants
|
|
|
|
ensure
|
|
|
|
# Remove the stack frames that we added.
|
|
|
|
if defined?(watch_frames) && ! watch_frames.empty?
|
|
|
|
frame_ids = watch_frames.collect(&:object_id)
|
|
|
|
constant_watch_stack.delete_if do |watch_frame|
|
|
|
|
frame_ids.include? watch_frame.object_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2007-01-26 16:37:38 -05:00
|
|
|
class LoadingModule #:nodoc:
|
2006-04-02 23:06:23 -04:00
|
|
|
# Old style environment.rb referenced this method directly. Please note, it doesn't
|
2007-09-22 13:52:11 -04:00
|
|
|
# actually *do* anything any more.
|
2006-04-02 23:06:23 -04:00
|
|
|
def self.root(*args)
|
|
|
|
if defined?(RAILS_DEFAULT_LOGGER)
|
|
|
|
RAILS_DEFAULT_LOGGER.warn "Your environment.rb uses the old syntax, it may not continue to work in future releases."
|
|
|
|
RAILS_DEFAULT_LOGGER.warn "For upgrade instructions please see: http://manuals.rubyonrails.com/read/book/19"
|
|
|
|
end
|
|
|
|
end
|
2005-01-23 19:30:15 -05:00
|
|
|
end
|
2006-08-08 17:21:04 -04:00
|
|
|
|
|
|
|
# Convert the provided const desc to a qualified constant name (as a string).
|
|
|
|
# A module, class, symbol, or string may be provided.
|
2007-09-27 07:01:20 -04:00
|
|
|
def to_constant_name(desc) #:nodoc:
|
2006-08-08 17:21:04 -04:00
|
|
|
name = case desc
|
|
|
|
when String then desc.starts_with?('::') ? desc[2..-1] : desc
|
|
|
|
when Symbol then desc.to_s
|
2006-10-15 19:32:31 -04:00
|
|
|
when Module
|
|
|
|
raise ArgumentError, "Anonymous modules have no name to be referenced by" if desc.name.blank?
|
|
|
|
desc.name
|
2006-08-08 17:21:04 -04:00
|
|
|
else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
|
|
|
def remove_constant(const) #:nodoc:
|
2006-10-15 19:32:31 -04:00
|
|
|
return false unless qualified_const_defined? const
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2006-12-09 00:21:33 -05:00
|
|
|
const = $1 if /\A::(.*)\Z/ =~ const.to_s
|
2007-09-13 20:34:43 -04:00
|
|
|
names = const.to_s.split('::')
|
2006-12-09 00:21:33 -05:00
|
|
|
if names.size == 1 # It's under Object
|
2006-10-15 19:32:31 -04:00
|
|
|
parent = Object
|
|
|
|
else
|
|
|
|
parent = (names[0..-2] * '::').constantize
|
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2006-10-15 19:32:31 -04:00
|
|
|
log "removing constant #{const}"
|
2007-09-13 20:34:43 -04:00
|
|
|
parent.instance_eval { remove_const names.last }
|
2006-10-15 19:32:31 -04:00
|
|
|
return true
|
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
|
|
|
protected
|
2006-08-12 23:29:04 -04:00
|
|
|
def log_call(*args)
|
|
|
|
arg_str = args.collect(&:inspect) * ', '
|
|
|
|
/in `([a-z_\?\!]+)'/ =~ caller(1).first
|
2008-03-30 01:02:25 -04:00
|
|
|
selector = $1 || '<unknown>'
|
2006-08-12 23:29:04 -04:00
|
|
|
log "called #{selector}(#{arg_str})"
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-08-12 23:29:04 -04:00
|
|
|
def log(msg)
|
|
|
|
if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity
|
|
|
|
RAILS_DEFAULT_LOGGER.debug "Dependencies: #{msg}"
|
|
|
|
end
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2004-12-29 16:03:21 -05:00
|
|
|
end
|
|
|
|
|
2007-09-13 20:34:43 -04:00
|
|
|
Object.instance_eval do
|
|
|
|
define_method(:require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load)
|
|
|
|
define_method(:require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency)
|
|
|
|
define_method(:require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association)
|
|
|
|
end
|
2004-12-29 16:03:21 -05:00
|
|
|
|
2005-05-19 15:17:08 -04:00
|
|
|
class Module #:nodoc:
|
2005-10-03 11:28:08 -04:00
|
|
|
# Rename the original handler so we can chain it to the new one
|
|
|
|
alias :rails_original_const_missing :const_missing
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2005-05-19 15:17:08 -04:00
|
|
|
# Use const_missing to autoload associations so we don't have to
|
|
|
|
# require_association when using single-table inheritance.
|
|
|
|
def const_missing(class_id)
|
2006-08-08 17:21:04 -04:00
|
|
|
Dependencies.load_missing_constant self, class_id
|
2004-12-29 16:03:21 -05:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2006-10-15 19:32:31 -04:00
|
|
|
def unloadable(const_desc = self)
|
|
|
|
super(const_desc)
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2005-05-19 15:17:08 -04:00
|
|
|
end
|
2005-02-20 08:47:57 -05:00
|
|
|
|
2006-01-28 19:37:39 -05:00
|
|
|
class Class
|
2007-12-14 21:12:39 -05:00
|
|
|
def const_missing(const_name)
|
2006-01-28 19:37:39 -05:00
|
|
|
if [Object, Kernel].include?(self) || parent == self
|
|
|
|
super
|
|
|
|
else
|
2006-08-05 18:52:15 -04:00
|
|
|
begin
|
2006-08-16 05:18:17 -04:00
|
|
|
begin
|
2007-12-14 21:12:39 -05:00
|
|
|
Dependencies.load_missing_constant self, const_name
|
2006-08-16 05:18:17 -04:00
|
|
|
rescue NameError
|
2007-12-14 21:12:39 -05:00
|
|
|
parent.send :const_missing, const_name
|
2006-08-16 05:18:17 -04:00
|
|
|
end
|
2006-08-05 18:52:15 -04:00
|
|
|
rescue NameError => e
|
|
|
|
# Make sure that the name we are missing is the one that caused the error
|
2007-12-14 21:12:39 -05:00
|
|
|
parent_qualified_name = Dependencies.qualified_name_for parent, const_name
|
2006-08-05 18:52:15 -04:00
|
|
|
raise unless e.missing_name? parent_qualified_name
|
2007-12-14 21:12:39 -05:00
|
|
|
qualified_name = Dependencies.qualified_name_for self, const_name
|
2006-08-05 18:52:15 -04:00
|
|
|
raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e)
|
|
|
|
end
|
2006-01-28 19:37:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-18 03:05:58 -04:00
|
|
|
class Object
|
2006-11-01 18:21:13 -05:00
|
|
|
alias_method :load_without_new_constant_marking, :load
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2007-03-18 03:05:58 -04:00
|
|
|
def load(file, *extras) #:nodoc:
|
2008-03-30 01:02:25 -04:00
|
|
|
Dependencies.new_constants_in(Object) { super }
|
2006-08-30 23:16:28 -04:00
|
|
|
rescue Exception => exception # errors from loading file
|
2005-06-24 12:40:01 -04:00
|
|
|
exception.blame_file! file
|
|
|
|
raise
|
2005-02-20 05:51:10 -05:00
|
|
|
end
|
2005-02-20 08:47:57 -05:00
|
|
|
|
2007-03-18 03:05:58 -04:00
|
|
|
def require(file, *extras) #:nodoc:
|
2008-03-30 01:02:25 -04:00
|
|
|
Dependencies.new_constants_in(Object) { super }
|
2006-08-30 23:16:28 -04:00
|
|
|
rescue Exception => exception # errors from required file
|
2005-06-24 12:40:01 -04:00
|
|
|
exception.blame_file! file
|
|
|
|
raise
|
2005-02-20 05:51:10 -05:00
|
|
|
end
|
2006-10-15 19:32:31 -04:00
|
|
|
|
|
|
|
# Mark the given constant as unloadable. Unloadable constants are removed each
|
|
|
|
# time dependencies are cleared.
|
2008-03-30 01:02:25 -04:00
|
|
|
#
|
2006-10-15 19:32:31 -04:00
|
|
|
# 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.
|
2008-03-30 01:02:25 -04:00
|
|
|
#
|
2006-10-15 19:32:31 -04:00
|
|
|
# The provided constant descriptor may be a (non-anonymous) module or class,
|
|
|
|
# or a qualified constant name as a string or symbol.
|
2008-03-30 01:02:25 -04:00
|
|
|
#
|
2006-10-15 19:32:31 -04:00
|
|
|
# Returns true if the constant was not previously marked for unloading, false
|
|
|
|
# otherwise.
|
|
|
|
def unloadable(const_desc)
|
|
|
|
Dependencies.mark_for_unload const_desc
|
|
|
|
end
|
2005-02-20 05:51:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add file-blaming to exceptions
|
2005-07-06 06:43:42 -04:00
|
|
|
class Exception #:nodoc:
|
2005-02-20 05:51:10 -05:00
|
|
|
def blame_file!(file)
|
|
|
|
(@blamed_files ||= []).unshift file
|
|
|
|
end
|
2005-02-20 08:47:57 -05:00
|
|
|
|
2005-02-20 15:55:36 -05:00
|
|
|
def blamed_files
|
|
|
|
@blamed_files ||= []
|
|
|
|
end
|
2005-02-20 08:47:57 -05:00
|
|
|
|
2005-02-20 05:51:10 -05:00
|
|
|
def describe_blame
|
|
|
|
return nil if blamed_files.empty?
|
2006-07-10 15:41:59 -04:00
|
|
|
"This error occurred while loading the following files:\n #{blamed_files.join "\n "}"
|
2005-02-20 05:51:10 -05:00
|
|
|
end
|
2005-03-23 06:48:10 -05:00
|
|
|
|
|
|
|
def copy_blame!(exc)
|
|
|
|
@blamed_files = exc.blamed_files.clone
|
|
|
|
self
|
|
|
|
end
|
2006-07-10 15:41:59 -04:00
|
|
|
end
|