~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/core_ext/class/removal.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
class Class #:nodoc:
2
 
  
3
 
  # Unassociates the class with its subclasses and removes the subclasses
4
 
  # themselves.
5
 
  #
6
 
  #   Integer.remove_subclasses # => [Bignum, Fixnum]
7
 
  #   Fixnum                    # => NameError: uninitialized constant Fixnum
8
 
  def remove_subclasses
9
 
    Object.remove_subclasses_of(self)
10
 
  end
11
 
 
12
 
  # Returns an array with the names of the subclasses of +self+ as strings.
13
 
  #
14
 
  #   Integer.subclasses # => ["Bignum", "Fixnum"]
15
 
  def subclasses
16
 
    Object.subclasses_of(self).map { |o| o.to_s }
17
 
  end
18
 
 
19
 
  # Removes the classes in +klasses+ from their parent module.
20
 
  #
21
 
  # Ordinary classes belong to some module via a constant. This method computes
22
 
  # that constant name from the class name and removes it from the module it
23
 
  # belongs to.
24
 
  #
25
 
  #   Object.remove_class(Integer) # => [Integer]
26
 
  #   Integer                      # => NameError: uninitialized constant Integer
27
 
  #
28
 
  # Take into account that in general the class object could be still stored
29
 
  # somewhere else.
30
 
  #
31
 
  #   i = Integer                  # => Integer
32
 
  #   Object.remove_class(Integer) # => [Integer]
33
 
  #   Integer                      # => NameError: uninitialized constant Integer
34
 
  #   i.subclasses                 # => ["Bignum", "Fixnum"]
35
 
  #   Fixnum.superclass            # => Integer
36
 
  def remove_class(*klasses)
37
 
    klasses.flatten.each do |klass|
38
 
      # Skip this class if there is nothing bound to this name
39
 
      next unless defined?(klass.name)
40
 
      
41
 
      basename = klass.to_s.split("::").last
42
 
      parent = klass.parent
43
 
      
44
 
      # Skip this class if it does not match the current one bound to this name
45
 
      next unless parent.const_defined?(basename) && klass = parent.const_get(basename)
46
 
 
47
 
      parent.instance_eval { remove_const basename } unless parent == klass
48
 
    end
49
 
  end
50
 
end