Sha256: de5c9869eb3851963834075deef997bd9e07368071fab62b19f223d00eff4699

Contents?: true

Size: 1.77 KB

Versions: 192

Compression:

Stored size: 1.77 KB

Contents

class Class #:nodoc:
  
  # Unassociates the class with its subclasses and removes the subclasses
  # themselves.
  #
  #   Integer.remove_subclasses # => [Bignum, Fixnum]
  #   Fixnum                    # => NameError: uninitialized constant Fixnum
  def remove_subclasses
    Object.remove_subclasses_of(self)
  end

  # Returns an array with the names of the subclasses of +self+ as strings.
  #
  #   Integer.subclasses # => ["Bignum", "Fixnum"]
  def subclasses
    Object.subclasses_of(self).map { |o| o.to_s }
  end

  # Removes the classes in +klasses+ from their parent module.
  #
  # Ordinary classes belong to some module via a constant. This method computes
  # that constant name from the class name and removes it from the module it
  # belongs to.
  #
  #   Object.remove_class(Integer) # => [Integer]
  #   Integer                      # => NameError: uninitialized constant Integer
  #
  # Take into account that in general the class object could be still stored
  # somewhere else.
  #
  #   i = Integer                  # => Integer
  #   Object.remove_class(Integer) # => [Integer]
  #   Integer                      # => NameError: uninitialized constant Integer
  #   i.subclasses                 # => ["Bignum", "Fixnum"]
  #   Fixnum.superclass            # => Integer
  def remove_class(*klasses)
    klasses.flatten.each do |klass|
      # Skip this class if there is nothing bound to this name
      next unless defined?(klass.name)
      
      basename = klass.to_s.split("::").last
      parent = klass.parent
      
      # Skip this class if it does not match the current one bound to this name
      next unless parent.const_defined?(basename) && klass = parent.const_get(basename)

      parent.instance_eval { remove_const basename } unless parent == klass
    end
  end
end

Version data entries

192 entries across 159 versions & 26 rubygems

Version Path
activesupport-2.3.17 lib/active_support/core_ext/class/removal.rb
depengine-0.0.1 etc/isolate/jruby-1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
depengine-0.0.1 etc/isolate/ruby-1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
activesupport_csi-2.3.5.p7 lib/active_support/core_ext/class/removal.rb
activesupport_csi-2.3.5.p6 lib/active_support/core_ext/class/removal.rb
activesupport-2.3.16 lib/active_support/core_ext/class/removal.rb
activesupport-2.3.15 lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.6.4 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.6.3 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.6.2 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.6.1 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.6.0 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.5.9 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/core_ext/class/removal.rb
abiquo-etk-0.5.8 vendor/activesupport-2.3.8/lib/active_support/core_ext/class/removal.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/activesupport-2.3.12/lib/active_support/core_ext/class/removal.rb
activesupport-2.3.14 lib/active_support/core_ext/class/removal.rb
kajam-1.0.3.rc2 vendor/rails/activesupport/lib/active_support/core_ext/class/removal.rb
activesupport-2.3.12 lib/active_support/core_ext/class/removal.rb
radiant-1.0.0.rc2 vendor/rails/activesupport/lib/active_support/core_ext/class/removal.rb