Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10470: const_missing_ruby_1.9_compat.diff

File const_missing_ruby_1.9_compat.diff, 2.3 kB (added by chuyeow, 10 months ago)
  • lib/active_support/dependencies.rb

    old new  
    216216  end 
    217217   
    218218  # Load the constant named +const_name+ which is missing from +from_mod+. If 
    219   # it is not possible to laod the constant into from_mod, try its parent module 
     219  # it is not possible to load the constant into from_mod, try its parent module 
    220220  # using const_missing. 
    221221  def load_missing_constant(from_mod, const_name) 
    222222    log_call from_mod, const_name 
     
    236236    unless qualified_const_defined?(from_mod.name) && from_mod.name.constantize.object_id == from_mod.object_id 
    237237      raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" 
    238238    end 
    239      
     239 
    240240    raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) 
    241241     
    242242    qualified_name = qualified_name_for from_mod, const_name 
     
    460460end 
    461461 
    462462class Class 
    463   def const_missing(class_id) 
     463  def const_missing(const_name) 
     464    # Bypass entire lookup process if we can get the constant from Object. 
     465    # This is useful for Ruby 1.9 where Module#const_defined? looks up the 
     466    # ancestors in the chain for the constant. 
     467    return ::Object.const_get(const_name) if ::Object.const_defined?(const_name) 
     468 
    464469    if [Object, Kernel].include?(self) || parent == self 
    465470      super 
    466471    else 
    467472      begin 
    468473        begin 
    469           Dependencies.load_missing_constant self, class_id 
     474          Dependencies.load_missing_constant self, const_name 
    470475        rescue NameError 
    471           parent.send :const_missing, class_id 
     476          parent.send :const_missing, const_name 
    472477        end 
    473478      rescue NameError => e 
    474479        # Make sure that the name we are missing is the one that caused the error 
    475         parent_qualified_name = Dependencies.qualified_name_for parent, class_id 
     480        parent_qualified_name = Dependencies.qualified_name_for parent, const_name 
    476481        raise unless e.missing_name? parent_qualified_name 
    477         qualified_name = Dependencies.qualified_name_for self, class_id 
     482        qualified_name = Dependencies.qualified_name_for self, const_name 
    478483        raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) 
    479484      end 
    480485    end