2007-12-21 08:48:59 +01:00
|
|
|
class Object
|
|
|
|
def remove_subclasses_of(*superclasses) #:nodoc:
|
2007-01-22 14:43:50 +01:00
|
|
|
Class.remove_class(*subclasses_of(*superclasses))
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
begin
|
|
|
|
ObjectSpace.each_object(Class.new) {}
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
# Exclude this class unless it's a subclass of our supers and is defined.
|
|
|
|
# We check defined? in case we find a removed class that has yet to be
|
|
|
|
# garbage collected. This also fails for anonymous classes -- please
|
|
|
|
# submit a patch if you have a workaround.
|
|
|
|
def subclasses_of(*superclasses) #:nodoc:
|
|
|
|
subclasses = []
|
|
|
|
|
|
|
|
superclasses.each do |sup|
|
|
|
|
ObjectSpace.each_object(class << sup; self; end) do |k|
|
|
|
|
if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
|
|
|
|
subclasses << k
|
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
|
|
|
subclasses
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
rescue RuntimeError
|
|
|
|
# JRuby and any implementations which cannot handle the objectspace traversal
|
|
|
|
# above fall back to this implementation
|
|
|
|
def subclasses_of(*superclasses) #:nodoc:
|
|
|
|
subclasses = []
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
superclasses.each do |sup|
|
|
|
|
ObjectSpace.each_object(Class) do |k|
|
|
|
|
if superclasses.any? { |superclass| k < superclass } &&
|
|
|
|
(k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
|
|
|
|
subclasses << k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
subclasses.uniq!
|
|
|
|
end
|
|
|
|
subclasses
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def extended_by #:nodoc:
|
2007-01-22 14:43:50 +01:00
|
|
|
ancestors = class << self; ancestors end
|
|
|
|
ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def extend_with_included_modules_from(object) #:nodoc:
|
2007-01-22 14:43:50 +01:00
|
|
|
object.extended_by.each { |mod| extend mod }
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? instance_exec # 1.9
|
2007-12-21 08:48:59 +01:00
|
|
|
module InstanceExecMethods #:nodoc:
|
|
|
|
end
|
|
|
|
include InstanceExecMethods
|
|
|
|
|
|
|
|
# Evaluate the block with the given arguments within the context of
|
|
|
|
# this object, so self is set to the method receiver.
|
|
|
|
#
|
|
|
|
# From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
|
|
|
|
def instance_exec(*args, &block)
|
|
|
|
begin
|
|
|
|
old_critical, Thread.critical = Thread.critical, true
|
|
|
|
n = 0
|
|
|
|
n += 1 while respond_to?(method_name = "__instance_exec#{n}")
|
|
|
|
InstanceExecMethods.module_eval { define_method(method_name, &block) }
|
|
|
|
ensure
|
|
|
|
Thread.critical = old_critical
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
send(method_name, *args)
|
|
|
|
ensure
|
|
|
|
InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|