Sha256: 178dc4ee34480ae7b81ff652b42c093404441874f08f5f01616b5d1009c22009

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 KB

Contents

class Object
  def remove_subclasses_of(*superclasses) #:nodoc:
    Class.remove_class(*subclasses_of(*superclasses))
  end

  # 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
      end
    end

    subclasses
  end

  def extended_by #:nodoc:
    ancestors = class << self; ancestors end
    ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
  end

  def extend_with_included_modules_from(object) #:nodoc:
    object.extended_by.each { |mod| extend mod }
  end

  unless defined? instance_exec # 1.9
    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
    end
  end
end

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
masover-castronaut-0.4.4.4 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
masover-castronaut-0.4.4.5 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
masover-castronaut-0.5.0.1 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.4.1 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.4.2 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.4.3 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.4.4 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.4.5 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.4.6 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.5.0 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.5.1 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.5.2 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.5.3 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
relevance-castronaut-0.5.4 vendor/activesupport/lib/active_support/core_ext/object/extending.rb
activesupport-2.1.1 lib/active_support/core_ext/object/extending.rb