2009-03-21 07:00:38 -04:00
|
|
|
require 'set'
|
2009-04-22 19:41:06 -04:00
|
|
|
require 'thread'
|
2012-12-13 08:47:33 -05:00
|
|
|
require 'thread_safe'
|
2010-02-14 13:47:50 -05:00
|
|
|
require 'pathname'
|
2009-03-29 02:50:31 -04:00
|
|
|
require 'active_support/core_ext/module/aliasing'
|
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
|
|
require 'active_support/core_ext/module/introspection'
|
2010-02-14 14:18:34 -05:00
|
|
|
require 'active_support/core_ext/module/anonymous'
|
2011-10-29 21:03:35 -04:00
|
|
|
require 'active_support/core_ext/module/qualified_const'
|
2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2013-10-15 07:00:06 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/load_error'
|
|
|
|
require 'active_support/core_ext/name_error'
|
|
|
|
require 'active_support/core_ext/string/starts_ends_with'
|
|
|
|
require 'active_support/inflector'
|
2009-03-21 07:00:38 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
module ActiveSupport #:nodoc:
|
|
|
|
module Dependencies #:nodoc:
|
|
|
|
extend self
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# All files ever loaded.
|
|
|
|
mattr_accessor :history
|
|
|
|
self.history = Set.new
|
|
|
|
|
|
|
|
# All files currently loaded.
|
|
|
|
mattr_accessor :loaded
|
|
|
|
self.loaded = Set.new
|
|
|
|
|
|
|
|
# Should we load files or require them?
|
|
|
|
mattr_accessor :mechanism
|
2009-03-26 22:10:13 -04:00
|
|
|
self.mechanism = ENV['NO_RELOAD'] ? :require : :load
|
2008-06-03 14:32:53 -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,
|
2010-06-22 17:17:20 -04:00
|
|
|
# unless the directory also appears in autoload_once_paths.
|
|
|
|
mattr_accessor :autoload_paths
|
|
|
|
self.autoload_paths = []
|
2008-06-03 14:32:53 -04:00
|
|
|
|
|
|
|
# The set of directories from which automatically loaded constants are loaded
|
2010-06-22 17:17:20 -04:00
|
|
|
# only once. All directories in this set must also be present in +autoload_paths+.
|
|
|
|
mattr_accessor :autoload_once_paths
|
|
|
|
self.autoload_once_paths = []
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# An array of qualified constant names that have been loaded. Adding a name
|
2012-12-05 01:11:54 -05:00
|
|
|
# to this array will cause it to be unloaded the next time Dependencies are
|
2012-09-17 01:22:18 -04:00
|
|
|
# cleared.
|
2008-06-03 14:32:53 -04:00
|
|
|
mattr_accessor :autoloaded_constants
|
|
|
|
self.autoloaded_constants = []
|
|
|
|
|
|
|
|
# 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 = []
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# The logger is used for generating information on the action run-time
|
|
|
|
# (including benchmarking) if available. Can be set to nil for no logging.
|
|
|
|
# Compatible with both Ruby's own Logger and Log4r loggers.
|
2008-08-18 23:38:58 -04:00
|
|
|
mattr_accessor :logger
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Set to +true+ to enable logging of const_missing and file loads.
|
2008-06-03 14:32:53 -04:00
|
|
|
mattr_accessor :log_activity
|
|
|
|
self.log_activity = false
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# The WatchStack keeps a stack of the modules being watched as files are
|
|
|
|
# loaded. If a file in the process of being loaded (parent.rb) triggers the
|
|
|
|
# load of another file (child.rb) the stack will ensure that child.rb
|
|
|
|
# handles the new constants.
|
2010-08-02 16:55:55 -04:00
|
|
|
#
|
|
|
|
# If child.rb is being autoloaded, its constants will be added to
|
|
|
|
# autoloaded_constants. If it was being `require`d, they will be discarded.
|
|
|
|
#
|
|
|
|
# This is handled by walking back up the watch stack and adding the constants
|
2012-09-17 01:22:18 -04:00
|
|
|
# found by child.rb to the list of original constants in parent.rb.
|
2011-11-30 13:14:01 -05:00
|
|
|
class WatchStack
|
|
|
|
include Enumerable
|
|
|
|
|
2010-08-02 16:55:55 -04:00
|
|
|
# @watching is a stack of lists of constants being watched. For instance,
|
2012-09-17 01:22:18 -04:00
|
|
|
# if parent.rb is autoloaded, the stack will look like [[Object]]. If
|
|
|
|
# parent.rb then requires namespace/child.rb, the stack will look like
|
|
|
|
# [[Object], [Namespace]].
|
2010-08-02 16:55:55 -04:00
|
|
|
|
2010-02-14 13:47:50 -05:00
|
|
|
def initialize
|
2010-08-02 16:55:55 -04:00
|
|
|
@watching = []
|
2011-11-30 13:14:01 -05:00
|
|
|
@stack = Hash.new { |h,k| h[k] = [] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
@stack.each(&block)
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
|
|
|
|
2011-12-06 06:32:34 -05:00
|
|
|
def watching?
|
|
|
|
!@watching.empty?
|
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Returns a list of new constants found since the last call to
|
|
|
|
# <tt>watch_namespaces</tt>.
|
2010-08-02 16:55:55 -04:00
|
|
|
def new_constants
|
|
|
|
constants = []
|
2010-02-14 13:47:50 -05:00
|
|
|
|
2010-08-02 16:55:55 -04:00
|
|
|
# Grab the list of namespaces that we're looking for new constants under
|
|
|
|
@watching.last.each do |namespace|
|
2010-09-13 16:53:39 -04:00
|
|
|
# Retrieve the constants that were present under the namespace when watch_namespaces
|
2010-08-02 16:55:55 -04:00
|
|
|
# was originally called
|
2011-11-30 13:14:01 -05:00
|
|
|
original_constants = @stack[namespace].last
|
2010-02-14 13:47:50 -05:00
|
|
|
|
2010-08-02 16:55:55 -04:00
|
|
|
mod = Inflector.constantize(namespace) if Dependencies.qualified_const_defined?(namespace)
|
2010-02-14 13:47:50 -05:00
|
|
|
next unless mod.is_a?(Module)
|
|
|
|
|
2010-08-02 16:55:55 -04:00
|
|
|
# Get a list of the constants that were added
|
2012-01-12 15:17:24 -05:00
|
|
|
new_constants = mod.local_constants - original_constants
|
2010-08-02 16:55:55 -04:00
|
|
|
|
|
|
|
# self[namespace] returns an Array of the constants that are being evaluated
|
|
|
|
# for that namespace. For instance, if parent.rb requires child.rb, the first
|
|
|
|
# element of self[Object] will be an Array of the constants that were present
|
|
|
|
# before parent.rb was required. The second element will be an Array of the
|
|
|
|
# constants that were present before child.rb was required.
|
2011-11-30 13:14:01 -05:00
|
|
|
@stack[namespace].each do |namespace_constants|
|
2010-08-17 19:50:19 -04:00
|
|
|
namespace_constants.concat(new_constants)
|
2010-07-26 03:59:54 -04:00
|
|
|
end
|
2010-02-14 13:47:50 -05:00
|
|
|
|
2010-08-02 16:55:55 -04:00
|
|
|
# Normalize the list of new constants, and add them to the list we will return
|
2010-02-19 13:10:45 -05:00
|
|
|
new_constants.each do |suffix|
|
2010-08-02 16:55:55 -04:00
|
|
|
constants << ([namespace, suffix] - ["Object"]).join("::")
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
2010-02-19 13:10:45 -05:00
|
|
|
end
|
|
|
|
constants
|
2010-08-02 16:55:55 -04:00
|
|
|
ensure
|
2010-09-13 16:53:39 -04:00
|
|
|
# A call to new_constants is always called after a call to watch_namespaces
|
2010-08-02 16:55:55 -04:00
|
|
|
pop_modules(@watching.pop)
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Add a set of modules to the watch stack, remembering the initial
|
|
|
|
# constants.
|
2010-08-02 16:55:55 -04:00
|
|
|
def watch_namespaces(namespaces)
|
2012-03-15 23:54:39 -04:00
|
|
|
@watching << namespaces.map do |namespace|
|
2010-08-02 16:55:55 -04:00
|
|
|
module_name = Dependencies.to_constant_name(namespace)
|
|
|
|
original_constants = Dependencies.qualified_const_defined?(module_name) ?
|
2012-01-12 15:17:24 -05:00
|
|
|
Inflector.constantize(module_name).local_constants : []
|
2010-08-02 16:55:55 -04:00
|
|
|
|
2011-11-30 13:14:01 -05:00
|
|
|
@stack[module_name] << original_constants
|
2012-03-15 23:54:39 -04:00
|
|
|
module_name
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-30 13:14:01 -05:00
|
|
|
private
|
2010-08-02 16:55:55 -04:00
|
|
|
def pop_modules(modules)
|
2011-11-30 13:14:01 -05:00
|
|
|
modules.each { |mod| @stack[mod].pop }
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# An internal stack used to record which constants are loaded by any block.
|
|
|
|
mattr_accessor :constant_watch_stack
|
2010-02-14 13:47:50 -05:00
|
|
|
self.constant_watch_stack = WatchStack.new
|
2009-01-29 01:31:07 -05:00
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Module includes this module.
|
2008-07-19 18:32:22 -04:00
|
|
|
module ModuleConstMissing #:nodoc:
|
2010-02-14 13:47:50 -05:00
|
|
|
def self.append_features(base)
|
2008-07-19 18:32:22 -04:00
|
|
|
base.class_eval do
|
2010-02-14 13:47:50 -05:00
|
|
|
# Emulate #exclude via an ivar
|
2010-02-19 14:28:20 -05:00
|
|
|
return if defined?(@_const_missing) && @_const_missing
|
2010-02-14 14:35:05 -05:00
|
|
|
@_const_missing = instance_method(:const_missing)
|
2010-02-14 13:47:50 -05:00
|
|
|
remove_method(:const_missing)
|
2008-07-24 12:58:26 -04:00
|
|
|
end
|
2010-02-14 13:47:50 -05:00
|
|
|
super
|
2008-07-24 12:58:26 -04:00
|
|
|
end
|
2008-07-19 18:32:22 -04:00
|
|
|
|
2010-02-14 13:47:50 -05:00
|
|
|
def self.exclude_from(base)
|
2008-07-24 12:58:26 -04:00
|
|
|
base.class_eval do
|
2010-02-14 13:47:50 -05:00
|
|
|
define_method :const_missing, @_const_missing
|
|
|
|
@_const_missing = nil
|
2008-07-19 18:32:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-19 17:50:34 -04:00
|
|
|
def const_missing(const_name)
|
2013-12-06 13:13:51 -05:00
|
|
|
from_mod = anonymous? ? guess_for_anonymous(const_name) : self
|
2012-09-06 08:00:45 -04:00
|
|
|
Dependencies.load_missing_constant(from_mod, const_name)
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
|
|
|
|
2013-12-06 13:13:51 -05:00
|
|
|
# Dependencies assumes the name of the module reflects the nesting (unless
|
|
|
|
# it can be proven that is not the case), and the path to the file that
|
|
|
|
# defines the constant. Anonymous modules cannot follow these conventions
|
|
|
|
# and we assume therefore the user wants to refer to a top-level constant.
|
|
|
|
def guess_for_anonymous(const_name)
|
|
|
|
if Object.const_defined?(const_name)
|
|
|
|
raise NameError, "#{const_name} cannot be autoloaded from an anonymous class or module"
|
|
|
|
else
|
|
|
|
Object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-14 13:47:50 -05:00
|
|
|
def unloadable(const_desc = self)
|
|
|
|
super(const_desc)
|
2008-07-19 18:32:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Object includes this module.
|
2008-07-19 18:32:22 -04:00
|
|
|
module Loadable #:nodoc:
|
2010-02-14 13:47:50 -05:00
|
|
|
def self.exclude_from(base)
|
|
|
|
base.class_eval { define_method(:load, Kernel.instance_method(:load)) }
|
2008-07-24 12:58:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def require_or_load(file_name)
|
|
|
|
Dependencies.require_or_load(file_name)
|
|
|
|
end
|
|
|
|
|
2013-10-01 04:10:53 -04:00
|
|
|
# Interprets a file using <tt>mechanism</tt> and marks its defined
|
|
|
|
# constants as autoloaded. <tt>file_name</tt> can be either a string or
|
|
|
|
# respond to <tt>to_path</tt>.
|
|
|
|
#
|
|
|
|
# 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.
|
2009-06-10 15:12:15 -04:00
|
|
|
def require_dependency(file_name, message = "No such file to load -- %s")
|
2013-09-30 16:20:28 -04:00
|
|
|
file_name = file_name.to_path if file_name.respond_to?(:to_path)
|
2010-02-14 15:13:37 -05:00
|
|
|
unless file_name.is_a?(String)
|
2013-09-30 16:20:28 -04:00
|
|
|
raise ArgumentError, "the file name must either be a String or implement #to_path -- you passed #{file_name.inspect}"
|
2010-02-14 15:13:37 -05:00
|
|
|
end
|
|
|
|
|
2012-06-09 18:35:18 -04:00
|
|
|
Dependencies.depend_on(file_name, message)
|
2008-07-24 12:58:26 -04:00
|
|
|
end
|
|
|
|
|
2010-02-14 13:47:50 -05:00
|
|
|
def load_dependency(file)
|
2011-12-06 06:32:34 -05:00
|
|
|
if Dependencies.load? && ActiveSupport::Dependencies.constant_watch_stack.watching?
|
2011-12-05 21:36:08 -05:00
|
|
|
Dependencies.new_constants_in(Object) { yield }
|
2008-11-11 00:39:05 -05:00
|
|
|
else
|
2010-02-14 13:47:50 -05:00
|
|
|
yield
|
2008-11-11 00:39:05 -05:00
|
|
|
end
|
2008-07-19 18:32:22 -04:00
|
|
|
rescue Exception => exception # errors from loading file
|
2013-03-03 08:50:59 -05:00
|
|
|
exception.blame_file! file if exception.respond_to? :blame_file!
|
2008-07-19 18:32:22 -04:00
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
2012-08-20 17:58:08 -04:00
|
|
|
def load(file, wrap = false)
|
2011-08-23 20:04:05 -04:00
|
|
|
result = false
|
|
|
|
load_dependency(file) { result = super }
|
|
|
|
result
|
2010-02-14 13:47:50 -05:00
|
|
|
end
|
|
|
|
|
2011-12-05 21:36:27 -05:00
|
|
|
def require(file)
|
2011-08-23 19:55:11 -04:00
|
|
|
result = false
|
|
|
|
load_dependency(file) { result = super }
|
|
|
|
result
|
2008-07-19 18:32:22 -04:00
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Mark the given constant as unloadable. Unloadable constants are removed
|
|
|
|
# each time dependencies are cleared.
|
2008-07-19 18:32:22 -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;
|
2012-09-17 01:22:18 -04:00
|
|
|
# each constant will be removed for every subsequent clear, as opposed to
|
|
|
|
# for the first clear.
|
2008-07-19 18:32:22 -04:00
|
|
|
#
|
|
|
|
# The provided constant descriptor may be a (non-anonymous) module or class,
|
|
|
|
# or a qualified constant name as a string or symbol.
|
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Returns +true+ if the constant was not previously marked for unloading,
|
|
|
|
# +false+ otherwise.
|
2008-07-19 18:32:22 -04:00
|
|
|
def unloadable(const_desc)
|
|
|
|
Dependencies.mark_for_unload const_desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Exception file-blaming.
|
2008-07-19 18:32:22 -04:00
|
|
|
module Blamable #:nodoc:
|
|
|
|
def blame_file!(file)
|
|
|
|
(@blamed_files ||= []).unshift file
|
|
|
|
end
|
|
|
|
|
|
|
|
def blamed_files
|
|
|
|
@blamed_files ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def describe_blame
|
|
|
|
return nil if blamed_files.empty?
|
|
|
|
"This error occurred while loading the following files:\n #{blamed_files.join "\n "}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_blame!(exc)
|
|
|
|
@blamed_files = exc.blamed_files.clone
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-24 12:58:26 -04:00
|
|
|
def hook!
|
2010-02-14 13:47:50 -05:00
|
|
|
Object.class_eval { include Loadable }
|
|
|
|
Module.class_eval { include ModuleConstMissing }
|
|
|
|
Exception.class_eval { include Blamable }
|
2008-07-24 12:58:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def unhook!
|
2010-02-14 13:47:50 -05:00
|
|
|
ModuleConstMissing.exclude_from(Module)
|
|
|
|
Loadable.exclude_from(Object)
|
2008-07-19 18:32:22 -04:00
|
|
|
end
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def load?
|
|
|
|
mechanism == :load
|
|
|
|
end
|
2005-11-23 16:31:51 -05:00
|
|
|
|
2012-06-09 18:35:18 -04:00
|
|
|
def depend_on(file_name, message = "No such file to load -- %s.rb")
|
2010-08-10 10:18:58 -04:00
|
|
|
path = search_for_file(file_name)
|
|
|
|
require_or_load(path || file_name)
|
|
|
|
rescue LoadError => load_error
|
2012-06-09 18:35:18 -04:00
|
|
|
if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1]
|
2012-06-12 19:19:51 -04:00
|
|
|
load_error.message.replace(message % file_name)
|
|
|
|
load_error.copy_blame!(load_error)
|
2009-06-10 15:12:15 -04:00
|
|
|
end
|
2012-06-09 18:35:18 -04:00
|
|
|
raise
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2005-11-23 16:31:51 -05:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def clear
|
|
|
|
log_call
|
|
|
|
loaded.clear
|
|
|
|
remove_unloadable_constants!
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def require_or_load(file_name, const_path = nil)
|
|
|
|
log_call file_name, const_path
|
2012-08-23 19:16:27 -04:00
|
|
|
file_name = $` if file_name =~ /\.rb\z/
|
2008-06-03 14:32:53 -04:00
|
|
|
expanded = File.expand_path(file_name)
|
|
|
|
return if loaded.include?(expanded)
|
2008-05-31 16:33:38 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Record that we've seen this file *before* loading it to avoid an
|
|
|
|
# infinite loop with mutual dependencies.
|
|
|
|
loaded << expanded
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
begin
|
|
|
|
if load?
|
|
|
|
log "loading #{file_name}"
|
|
|
|
|
2010-12-02 21:29:02 -05:00
|
|
|
# Enable warnings if this file has not been loaded before and
|
2008-06-03 14:32:53 -04:00
|
|
|
# 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?(expanded)
|
|
|
|
result = load_file(*load_args)
|
|
|
|
else
|
|
|
|
enable_warnings { result = load_file(*load_args) }
|
|
|
|
end
|
2005-11-24 00:43:27 -05:00
|
|
|
else
|
2008-06-03 14:32:53 -04:00
|
|
|
log "requiring #{file_name}"
|
|
|
|
result = require file_name
|
2005-11-24 00:43:27 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
rescue Exception
|
|
|
|
loaded.delete expanded
|
|
|
|
raise
|
2005-11-23 16:31:51 -05:00
|
|
|
end
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Record history *after* loading so first load gets warnings.
|
|
|
|
history << expanded
|
2012-03-15 23:54:39 -04:00
|
|
|
result
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Is the provided constant path defined?
|
2011-12-20 12:31:13 -05:00
|
|
|
def qualified_const_defined?(path)
|
|
|
|
Object.qualified_const_defined?(path.sub(/^::/, ''), false)
|
2006-08-16 13:50:52 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2012-09-17 01:22:18 -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
|
2012-12-05 01:11:54 -05:00
|
|
|
# file.
|
2010-06-22 17:17:20 -04:00
|
|
|
def loadable_constants_for_path(path, bases = autoload_paths)
|
2012-08-24 21:10:38 -04:00
|
|
|
path = $` if path =~ /\.rb\z/
|
2010-06-23 19:04:41 -04:00
|
|
|
expanded_path = File.expand_path(path)
|
|
|
|
paths = []
|
|
|
|
|
|
|
|
bases.each do |root|
|
|
|
|
expanded_root = File.expand_path(root)
|
|
|
|
next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path
|
|
|
|
|
|
|
|
nesting = expanded_path[(expanded_root.size)..-1]
|
|
|
|
nesting = nesting[1..-1] if nesting && nesting[0] == ?/
|
|
|
|
next if nesting.blank?
|
2010-02-14 13:47:50 -05:00
|
|
|
|
|
|
|
paths << nesting.camelize
|
2010-06-23 19:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
paths.uniq!
|
|
|
|
paths
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2010-06-22 17:17:20 -04:00
|
|
|
# Search for a file in autoload_paths matching the provided suffix.
|
2010-08-04 13:17:00 -04:00
|
|
|
def search_for_file(path_suffix)
|
|
|
|
path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb")
|
2010-02-14 13:47:50 -05:00
|
|
|
|
2010-06-22 17:17:20 -04:00
|
|
|
autoload_paths.each do |root|
|
2008-06-03 14:32:53 -04:00
|
|
|
path = File.join(root, path_suffix)
|
|
|
|
return path if File.file? path
|
|
|
|
end
|
|
|
|
nil # Gee, I sure wish we had first_match ;-)
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Does the provided path_suffix correspond to an autoloadable module?
|
2012-09-17 01:22:18 -04:00
|
|
|
# Instead of returning a boolean, the autoload base for this module is
|
|
|
|
# returned.
|
2008-06-03 14:32:53 -04:00
|
|
|
def autoloadable_module?(path_suffix)
|
2010-06-22 17:17:20 -04:00
|
|
|
autoload_paths.each do |load_path|
|
2008-06-03 14:32:53 -04:00
|
|
|
return load_path if File.directory? File.join(load_path, path_suffix)
|
|
|
|
end
|
|
|
|
nil
|
2006-11-01 18:21:13 -05:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def load_once_path?(path)
|
2011-08-24 12:30:50 -04:00
|
|
|
# to_s works around a ruby1.9 issue where #starts_with?(Pathname) will always return false
|
|
|
|
autoload_once_paths.any? { |base| path.starts_with? base.to_s }
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Attempt to autoload the provided module name by searching for a directory
|
2012-09-17 01:22:18 -04:00
|
|
|
# matching the expected path suffix. If found, the module is created and
|
|
|
|
# assigned to +into+'s constants with the name +const_name+. Provided that
|
2012-12-05 01:11:54 -05:00
|
|
|
# the directory was loaded from a reloadable base path, it is added to the
|
2012-09-17 01:22:18 -04:00
|
|
|
# set of constants that are to be unloaded.
|
2008-06-03 14:32:53 -04:00
|
|
|
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
|
2010-06-22 17:17:20 -04:00
|
|
|
autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
|
2012-03-15 23:54:39 -04:00
|
|
|
mod
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -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.
|
|
|
|
#
|
2012-09-17 01:22:18 -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
|
2008-06-03 14:32:53 -04:00
|
|
|
# +loadable_constants_for_path+ for more details.
|
|
|
|
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
|
2013-06-17 20:49:11 -04:00
|
|
|
parent_paths = const_paths.collect { |const_path| const_path[/.*(?=::)/] || ::Object }
|
2008-06-03 14:32:53 -04:00
|
|
|
|
|
|
|
result = nil
|
|
|
|
newly_defined_paths = new_constants_in(*parent_paths) do
|
2010-02-14 13:47:50 -05:00
|
|
|
result = Kernel.load path
|
2006-09-05 18:36:28 -04:00
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
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?
|
2012-03-15 23:54:39 -04:00
|
|
|
result
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Returns the constant path for the provided parent and constant name.
|
2008-06-03 14:32:53 -04:00
|
|
|
def qualified_name_for(mod, name)
|
|
|
|
mod_name = to_constant_name mod
|
2010-02-14 13:47:50 -05:00
|
|
|
mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}"
|
2006-08-16 23:35:36 -04:00
|
|
|
end
|
2007-12-14 21:12:39 -05:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Load the constant named +const_name+ which is missing from +from_mod+. If
|
2012-09-17 01:22:18 -04:00
|
|
|
# it is not possible to load the constant into from_mod, try its parent
|
|
|
|
# module using +const_missing+.
|
2008-06-03 14:32:53 -04:00
|
|
|
def load_missing_constant(from_mod, const_name)
|
|
|
|
log_call from_mod, const_name
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2010-02-14 13:47:50 -05:00
|
|
|
unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod)
|
2008-06-03 14:32:53 -04:00
|
|
|
raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
|
|
|
|
end
|
|
|
|
|
|
|
|
qualified_name = qualified_name_for from_mod, const_name
|
|
|
|
path_suffix = qualified_name.underscore
|
2010-02-27 14:51:19 -05:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
file_path = search_for_file(path_suffix)
|
2010-02-14 13:47:50 -05:00
|
|
|
|
2012-08-28 14:27:59 -04:00
|
|
|
if file_path
|
|
|
|
expanded = File.expand_path(file_path)
|
2012-08-29 11:42:24 -04:00
|
|
|
expanded.sub!(/\.rb\z/, '')
|
2012-08-28 14:27:59 -04:00
|
|
|
|
|
|
|
if loaded.include?(expanded)
|
|
|
|
raise "Circular dependency detected while autoloading constant #{qualified_name}"
|
|
|
|
else
|
2013-07-26 12:37:00 -04:00
|
|
|
require_or_load(expanded, qualified_name)
|
2012-09-03 19:17:38 -04:00
|
|
|
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
|
2012-08-28 14:27:59 -04:00
|
|
|
return from_mod.const_get(const_name)
|
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
|
|
|
|
return mod
|
|
|
|
elsif (parent = from_mod.parent) && parent != from_mod &&
|
2012-01-12 16:53:50 -05:00
|
|
|
! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
|
2008-06-03 14:32:53 -04:00
|
|
|
# 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
|
2012-09-06 07:43:49 -04:00
|
|
|
# Since Ruby does not pass the nesting at the point the unknown
|
|
|
|
# constant triggered the callback we cannot fully emulate constant
|
|
|
|
# name lookup and need to make a trade-off: we are going to assume
|
|
|
|
# that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
|
|
|
|
# though it might not be. Counterexamples are
|
|
|
|
#
|
|
|
|
# class Foo::Bar
|
|
|
|
# Module.nesting # => [Foo::Bar]
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
|
|
|
# module M::N
|
|
|
|
# module S::T
|
|
|
|
# Module.nesting # => [S::T, M::N]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# for example.
|
2008-06-03 14:32:53 -04:00
|
|
|
return parent.const_missing(const_name)
|
|
|
|
rescue NameError => e
|
|
|
|
raise unless e.missing_name? qualified_name_for(parent, const_name)
|
|
|
|
end
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2011-07-29 17:38:57 -04:00
|
|
|
|
|
|
|
raise NameError,
|
|
|
|
"uninitialized constant #{qualified_name}",
|
2012-03-15 23:54:39 -04:00
|
|
|
caller.reject { |l| l.starts_with? __FILE__ }
|
2006-08-08 17:21:04 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Remove the constants that have been autoloaded, and those that have been
|
2010-09-01 09:12:06 -04:00
|
|
|
# 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.
|
2011-03-03 23:14:18 -05:00
|
|
|
# as the environment will be in an inconsistent state, e.g. other constants
|
2010-09-01 09:12:06 -04:00
|
|
|
# may have already been unloaded and not accessible.
|
2008-06-03 14:32:53 -04:00
|
|
|
def remove_unloadable_constants!
|
|
|
|
autoloaded_constants.each { |const| remove_constant const }
|
|
|
|
autoloaded_constants.clear
|
2010-06-06 01:02:49 -04:00
|
|
|
Reference.clear!
|
2008-06-03 14:32:53 -04:00
|
|
|
explicitly_unloadable_constants.each { |const| remove_constant const }
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2011-03-01 20:20:10 -05:00
|
|
|
class ClassCache
|
|
|
|
def initialize
|
2012-12-13 08:47:33 -05:00
|
|
|
@store = ThreadSafe::Cache.new
|
2011-03-01 20:20:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@store.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def key?(key)
|
|
|
|
@store.key?(key)
|
|
|
|
end
|
2010-06-06 01:02:49 -04:00
|
|
|
|
2011-11-23 16:43:03 -05:00
|
|
|
def get(key)
|
|
|
|
key = key.name if key.respond_to?(:name)
|
|
|
|
@store[key] ||= Inflector.constantize(key)
|
2011-03-01 20:20:10 -05:00
|
|
|
end
|
2011-11-23 18:45:27 -05:00
|
|
|
alias :[] :get
|
2011-03-01 20:20:10 -05:00
|
|
|
|
2011-11-23 16:43:03 -05:00
|
|
|
def safe_get(key)
|
2011-03-01 20:20:10 -05:00
|
|
|
key = key.name if key.respond_to?(:name)
|
2012-03-15 23:54:39 -04:00
|
|
|
@store[key] ||= Inflector.safe_constantize(key)
|
2010-06-04 20:28:04 -04:00
|
|
|
end
|
|
|
|
|
2011-11-23 16:43:03 -05:00
|
|
|
def store(klass)
|
|
|
|
return self unless klass.respond_to?(:name)
|
|
|
|
raise(ArgumentError, 'anonymous classes cannot be cached') if klass.name.empty?
|
|
|
|
@store[klass.name] = klass
|
2011-03-02 12:31:40 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-03-01 20:20:10 -05:00
|
|
|
def clear!
|
|
|
|
@store.clear
|
2010-06-04 20:28:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-01 20:20:10 -05:00
|
|
|
Reference = ClassCache.new
|
|
|
|
|
2011-03-02 12:31:40 -05:00
|
|
|
# Store a reference to a class +klass+.
|
|
|
|
def reference(klass)
|
|
|
|
Reference.store klass
|
|
|
|
end
|
2010-06-05 23:33:53 -04:00
|
|
|
|
2011-03-02 12:31:40 -05:00
|
|
|
# Get the reference for class named +name+.
|
2011-11-23 16:43:03 -05:00
|
|
|
# Raises an exception if referenced class does not exist.
|
2010-06-05 23:33:53 -04:00
|
|
|
def constantize(name)
|
2011-03-01 20:43:45 -05:00
|
|
|
Reference.get(name)
|
2010-06-04 20:28:04 -04:00
|
|
|
end
|
|
|
|
|
2011-11-23 16:43:03 -05:00
|
|
|
# Get the reference for class named +name+ if one exists.
|
2012-09-17 01:22:18 -04:00
|
|
|
# Otherwise returns +nil+.
|
2011-11-23 16:43:03 -05:00
|
|
|
def safe_constantize(name)
|
|
|
|
Reference.safe_get(name)
|
|
|
|
end
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Determine if the given constant has been automatically loaded.
|
|
|
|
def autoloaded?(desc)
|
2010-02-12 13:50:00 -05:00
|
|
|
return false if desc.is_a?(Module) && desc.anonymous?
|
2008-06-03 14:32:53 -04:00
|
|
|
name = to_constant_name desc
|
|
|
|
return false unless qualified_const_defined? name
|
|
|
|
return autoloaded_constants.include?(name)
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Will the provided constant descriptor be unloaded?
|
|
|
|
def will_unload?(const_desc)
|
2008-06-27 20:19:24 -04:00
|
|
|
autoloaded?(const_desc) ||
|
2008-06-03 14:32:53 -04:00
|
|
|
explicitly_unloadable_constants.include?(to_constant_name(const_desc))
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -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
|
2012-03-15 23:54:39 -04:00
|
|
|
false
|
2008-06-03 14:32:53 -04:00
|
|
|
else
|
|
|
|
explicitly_unloadable_constants << name
|
2012-03-15 23:54:39 -04:00
|
|
|
true
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2006-10-15 19:32:31 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04: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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
def new_constants_in(*descs)
|
|
|
|
log_call(*descs)
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2010-08-02 16:55:55 -04:00
|
|
|
constant_watch_stack.watch_namespaces(descs)
|
2008-06-03 14:32:53 -04:00
|
|
|
aborting = true
|
2010-08-02 16:55:55 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
begin
|
|
|
|
yield # Now yield to the code that is to define new constants.
|
|
|
|
aborting = false
|
|
|
|
ensure
|
2010-08-02 16:55:55 -04:00
|
|
|
new_constants = constant_watch_stack.new_constants
|
2008-06-03 14:32:53 -04:00
|
|
|
|
|
|
|
log "New constants: #{new_constants * ', '}"
|
2010-02-14 13:47:50 -05:00
|
|
|
return new_constants unless aborting
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2010-02-14 13:47:50 -05:00
|
|
|
log "Error during loading, removing partially loaded constants "
|
2012-03-15 23:54:39 -04:00
|
|
|
new_constants.each { |c| remove_constant(c) }.clear
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2012-03-15 23:54:39 -04:00
|
|
|
[]
|
2006-11-07 15:45:36 -05:00
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
# Convert the provided const desc to a qualified constant name (as a string).
|
|
|
|
# A module, class, symbol, or string may be provided.
|
|
|
|
def to_constant_name(desc) #:nodoc:
|
2010-07-22 23:36:34 -04:00
|
|
|
case desc
|
2010-02-14 13:47:50 -05:00
|
|
|
when String then desc.sub(/^::/, '')
|
2008-06-03 14:32:53 -04:00
|
|
|
when Symbol then desc.to_s
|
|
|
|
when Module
|
2013-06-17 20:50:11 -04:00
|
|
|
desc.name ||
|
2010-02-14 13:47:50 -05:00
|
|
|
raise(ArgumentError, "Anonymous modules have no name to be referenced by")
|
2008-06-03 14:32:53 -04:00
|
|
|
else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
|
2006-04-02 23:06:23 -04:00
|
|
|
end
|
|
|
|
end
|
2006-08-08 17:21:04 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def remove_constant(const) #:nodoc:
|
2012-11-14 22:33:17 -05:00
|
|
|
# Normalize ::Foo, ::Object::Foo, Object::Foo, Object::Object::Foo, etc. as Foo.
|
|
|
|
normalized = const.to_s.sub(/\A::/, '')
|
|
|
|
normalized.sub!(/\A(Object::)+/, '')
|
|
|
|
|
2012-11-27 18:13:14 -05:00
|
|
|
constants = normalized.split('::')
|
|
|
|
to_remove = constants.pop
|
2012-11-14 22:33:17 -05:00
|
|
|
|
2012-11-27 18:13:14 -05:00
|
|
|
if constants.empty?
|
|
|
|
parent = Object
|
|
|
|
else
|
|
|
|
# This method is robust to non-reachable constants.
|
2012-11-15 05:06:35 -05:00
|
|
|
#
|
2012-11-27 18:13:14 -05:00
|
|
|
# Non-reachable constants may be passed if some of the parents were
|
|
|
|
# autoloaded and already removed. It is easier to do a sanity check
|
|
|
|
# here than require the caller to be clever. We check the parent
|
|
|
|
# rather than the very const argument because we do not want to
|
|
|
|
# trigger Kernel#autoloads, see the comment below.
|
|
|
|
parent_name = constants.join('::')
|
|
|
|
return unless qualified_const_defined?(parent_name)
|
|
|
|
parent = constantize(parent_name)
|
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2012-11-27 18:13:14 -05:00
|
|
|
log "removing constant #{const}"
|
|
|
|
|
|
|
|
# In an autoloaded user.rb like this
|
|
|
|
#
|
|
|
|
# autoload :Foo, 'foo'
|
|
|
|
#
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# we correctly register "Foo" as being autoloaded. But if the app does
|
|
|
|
# not use the "Foo" constant we need to be careful not to trigger
|
|
|
|
# loading "foo.rb" ourselves. While #const_defined? and #const_get? do
|
|
|
|
# require the file, #autoload? and #remove_const don't.
|
|
|
|
#
|
|
|
|
# We are going to remove the constant nonetheless ---which exists as
|
|
|
|
# far as Ruby is concerned--- because if the user removes the macro
|
|
|
|
# call from a class or module that were not autoloaded, as in the
|
|
|
|
# example above with Object, accessing to that constant must err.
|
|
|
|
unless parent.autoload?(to_remove)
|
2012-11-14 22:33:17 -05:00
|
|
|
begin
|
2012-11-27 18:13:14 -05:00
|
|
|
constantized = parent.const_get(to_remove, false)
|
2012-11-14 22:33:17 -05:00
|
|
|
rescue NameError
|
|
|
|
log "the constant #{const} is not reachable anymore, skipping"
|
2012-11-27 18:13:14 -05:00
|
|
|
return
|
|
|
|
else
|
|
|
|
constantized.before_remove_const if constantized.respond_to?(:before_remove_const)
|
2012-11-14 22:33:17 -05:00
|
|
|
end
|
|
|
|
end
|
2012-11-27 18:13:14 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
parent.instance_eval { remove_const to_remove }
|
|
|
|
rescue NameError
|
|
|
|
log "the constant #{const} is not reachable anymore, skipping"
|
|
|
|
end
|
2006-10-15 19:32:31 -04:00
|
|
|
end
|
2007-09-27 07:01:20 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
protected
|
|
|
|
def log_call(*args)
|
2010-09-05 10:27:16 -04:00
|
|
|
if log_activity?
|
2008-06-25 22:21:53 -04:00
|
|
|
arg_str = args.collect { |arg| arg.inspect } * ', '
|
2008-06-03 14:32:53 -04:00
|
|
|
/in `([a-z_\?\!]+)'/ =~ caller(1).first
|
|
|
|
selector = $1 || '<unknown>'
|
|
|
|
log "called #{selector}(#{arg_str})"
|
|
|
|
end
|
|
|
|
end
|
2008-03-30 01:02:25 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def log(msg)
|
2010-09-05 10:27:16 -04:00
|
|
|
logger.debug "Dependencies: #{msg}" if log_activity?
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_activity?
|
|
|
|
logger && log_activity
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2006-08-12 23:29:04 -04:00
|
|
|
end
|
2004-12-29 16:03:21 -05:00
|
|
|
end
|
|
|
|
|
2008-07-24 12:58:26 -04:00
|
|
|
ActiveSupport::Dependencies.hook!
|