From 01b1a8772928fbf31180341356981b95d0581413 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 29 Jan 2006 00:37:39 +0000 Subject: Added reusable reloading support through the inclusion of the Relodable module that all subclasses of ActiveRecord::Base, ActiveRecord::Observer, ActiveController::Base, and ActionMailer::Base automatically gets [DHH]. Added auto-loading support for classes in modules, so Conductor::Migration will look for conductor/migration.rb and Conductor::Database::Settings will look for conductor/database/settings.rb [Nicholas Seckar]. Refactored extensions to module, class, and object in active support [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3493 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/core_ext/object/extending.rb | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/object/extending.rb (limited to 'activesupport/lib/active_support/core_ext/object/extending.rb') diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb new file mode 100644 index 0000000000..8c638f83c2 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -0,0 +1,45 @@ +class Object #:nodoc: + def remove_subclasses_of(*superclasses) + subclasses_of(*superclasses).each do |subclass| + Object.send(:remove_const, subclass.to_s) rescue nil + end + end + + def subclasses_of(*superclasses) + subclasses = [] + ObjectSpace.each_object(Class) do |k| + next if (k.ancestors & superclasses).empty? || superclasses.include?(k) || k.to_s.include?("::") || subclasses.include?(k) + subclasses << k + end + subclasses + end + + def extended_by + ancestors = class << self; ancestors end + ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ] + end + + def copy_instance_variables_from(object, exclude = []) + exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables + + instance_variables = object.instance_variables - exclude.map { |name| name.to_s } + instance_variables.each { |name| instance_variable_set(name, object.instance_variable_get(name)) } + end + + def extend_with_included_modules_from(object) + object.extended_by.each { |mod| extend mod } + end + + def instance_values + instance_variables.inject({}) do |values, name| + values[name[1..-1]] = instance_variable_get(name) + values + end + end + + unless defined? instance_exec # 1.9 + def instance_exec(*arguments, &block) + block.bind(self)[*arguments] + end + end +end \ No newline at end of file -- cgit v1.2.3