From 39045fc2ab2de88f6e0daa143f44d27df2d461ce Mon Sep 17 00:00:00 2001 From: Dawid Marcin Grzesiak Date: Sat, 2 Apr 2011 23:44:13 +0200 Subject: [PATCH] active support class to path conventions for autoload --- activesupport/lib/active_support/dependencies.rb | 32 +++++++++++++++------ 1 files changed, 23 insertions(+), 9 deletions(-) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index dc10f78..5c128d9 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -477,21 +477,35 @@ module ActiveSupport #:nodoc: raise ArgumentError, "#{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 trace = caller.reject {|l| l =~ %r{#{Regexp.escape(__FILE__)}}} name_error = NameError.new("uninitialized constant #{qualified_name}") name_error.set_backtrace(trace) + + class_to_path_conventions = [] + class_to_path_conventions << lambda { |c| c.underscore } + class_to_path_conventions << lambda { |c| c.gsub('::', '/') } + + class_to_path_conventions.each do |conv| + path_suffix = conv.call(qualified_name) + file_path = search_for_file(path_suffix) + + if file_path && ! loaded.include?(File.expand_path(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) + end + end - file_path = search_for_file(path_suffix) + class_to_path_conventions.each do |conv| + path_suffix = conv.call(qualified_name) + + if mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) + return mod + end + end - if file_path && ! loaded.include?(File.expand_path(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 && + if (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 -- 1.7.1