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

Changeset 6426

Show
Ignore:
Timestamp:
03/14/07 23:27:28 (2 years ago)
Author:
ulysses
Message:

Update Dependencies to ignore constants inherited from ancestors. Closes #6951.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r6378 r6426  
    11*SVN* 
     2 
     3* Update Dependencies to ignore constants inherited from ancestors. Closes #6951. [Nicholas Seckar] 
    24 
    35* Array#to_query preserves its ordering.  #7756 [Greg Spurrier] 
  • trunk/activesupport/lib/active_support/core_ext/module/introspection.rb

    r4060 r6426  
    1919    parents 
    2020  end 
     21   
     22  # Return the constants that have been defined locally by this object and not 
     23  # in an ancestor. This method may miss some constants if their definition in 
     24  # the ancestor is identical to their definition in the receiver. 
     25  def local_constants 
     26    inherited = {} 
     27    ancestors.each do |anc| 
     28      next if anc == self 
     29      anc.constants.each { |const| inherited[const] = anc.const_get(const) } 
     30    end 
     31    constants.select do |const| 
     32      ! inherited.key?(const) || inherited[const].object_id != const_get(const).object_id 
     33    end 
     34  end 
    2135end 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r6083 r6426  
    319319      if desc.is_a? Module 
    320320        mod_name = desc.name 
    321         initial_constants = desc.constants 
     321        initial_constants = desc.local_constants 
    322322      elsif desc.is_a?(String) || desc.is_a?(Symbol) 
    323323        mod_name = desc.to_s 
     
    325325        # Handle the case where the module has yet to be defined. 
    326326        initial_constants = if qualified_const_defined?(mod_name) 
    327           mod_name.constantize.constants 
     327          mod_name.constantize.local_constants 
    328328        else 
    329329         [] 
     
    350350        mod = mod_name.constantize 
    351351        next [] unless mod.is_a? Module 
    352         new_constants = mod.constants - prior_constants 
     352        new_constants = mod.local_constants - prior_constants 
    353353         
    354354        # Make sure no other frames takes credit for these constants. 
  • trunk/activesupport/test/core_ext/module_test.rb

    r5113 r6426  
    22 
    33module One 
     4  Constant1 = "Hello World" 
     5  Constant2 = "What's up?" 
    46end 
    57 
    68class Ab 
    79  include One 
     10  Constant1 = "Hello World" # Will have different object id than One::Constant1 
     11  Constant3 = "Goodbye World" 
    812end 
    913 
     
    9195    assert_equal [Yz::Zy, Yz, Object], Yz::Zy::Cd.parents 
    9296    assert_equal [Yz, Object], Yz::Zy.parents 
     97  end 
     98   
     99  def test_local_constants 
     100    assert_equal %w(Constant1 Constant3), Ab.local_constants.sort 
    93101  end 
    94102 
  • trunk/activesupport/test/dependencies_test.rb

    r5816 r6426  
    88    name 
    99  end 
     10end 
     11 
     12module ModuleWithConstant 
     13  InheritedConstant = "Hello" 
    1014end 
    1115 
     
    573577  ensure 
    574578    Object.send :remove_const, :M rescue nil 
     579  end 
     580 
     581  def test_new_constants_in_with_inherited_constants 
     582    m = Dependencies.new_constants_in(:Object) do 
     583      Object.send :include, ModuleWithConstant 
     584    end 
     585    assert_equal [], m 
    575586  end 
    576587