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

Changeset 7590

Show
Ignore:
Timestamp:
09/23/07 01:09:20 (10 months ago)
Author:
bitsweat
Message:

Object.subclasses_of includes anonymous subclasses.

Files:

Legend:

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

    r7569 r7590  
    11*SVN* 
     2 
     3* Object.subclasses_of includes anonymous subclasses.  [Jeremy Kemper] 
    24 
    35* Fixed that pluralizing an empty string should return the same empty string, not "s" #7720 [josh] 
  • trunk/activesupport/lib/active_support/core_ext/object/extending.rb

    r6443 r7590  
    66  def subclasses_of(*superclasses) #:nodoc: 
    77    subclasses = [] 
     8 
     9    # Exclude this class unless it's a subclass of our supers and is defined. 
     10    # We check defined? in case we find a removed class that has yet to be 
     11    # garbage collected. This also fails for anonymous classes -- please 
     12    # submit a patch if you have a workaround. 
    813    ObjectSpace.each_object(Class) do |k| 
    9       next unless # Exclude this class unless 
    10         superclasses.any? { |superclass| k < superclass } &&        # It *is* a subclass of our supers 
    11         eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id") # It *is* defined 
    12           # Note that we check defined? in case we find a removed class that has 
    13           # yet to be garbage collected. 
    14       subclasses << k 
     14      if superclasses.any? { |superclass| k < superclass } && 
     15        (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) 
     16        subclasses << k 
     17      end 
    1518    end 
     19 
    1620    subclasses 
    1721  end 
    18    
     22 
    1923  def extended_by #:nodoc: 
    2024    ancestors = class << self; ancestors end 
  • trunk/activesupport/test/abstract_unit.rb

    r7486 r7590  
    77def uses_mocha(test_name) 
    88  require 'rubygems' 
     9  gem 'mocha', '>= 0.5.5' 
    910  require 'mocha' 
    1011  yield 
  • trunk/activesupport/test/core_ext/object_and_class_ext_test.rb

    r6036 r7590  
    9898    assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort 
    9999  end 
    100    
     100 
     101  def test_subclasses_of_doesnt_find_anonymous_classes 
     102    assert_equal [], Object.subclasses_of(Foo) 
     103    bar = Class.new(Foo) 
     104    assert_nothing_raised do 
     105      assert_equal [bar], Object.subclasses_of(Foo) 
     106    end 
     107  end 
    101108end 
    102109