aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-09-27 10:13:50 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-09-27 10:13:50 +0000
commit935f8215371d68f36950c709061e5876ecadaa89 (patch)
treec6f55c027d3febebeb9704b364e0d7bacb9b0710 /activesupport
parentf3560d5a956f13df0571471c63f05369b85ba67d (diff)
downloadrails-935f8215371d68f36950c709061e5876ecadaa89.tar.gz
rails-935f8215371d68f36950c709061e5876ecadaa89.tar.bz2
rails-935f8215371d68f36950c709061e5876ecadaa89.zip
Object#copy_instance_variables_from Ruby 1.9 compat
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7654 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/object.rb3
-rw-r--r--activesupport/lib/active_support/core_ext/object/extending.rb16
-rw-r--r--activesupport/lib/active_support/core_ext/object/instance_variables.rb14
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb12
4 files changed, 23 insertions, 22 deletions
diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb
index 3dd6deaba1..57238f38f5 100644
--- a/activesupport/lib/active_support/core_ext/object.rb
+++ b/activesupport/lib/active_support/core_ext/object.rb
@@ -1,2 +1,3 @@
require File.dirname(__FILE__) + '/object/extending'
-require File.dirname(__FILE__) + '/object/misc' \ No newline at end of file
+require File.dirname(__FILE__) + '/object/instance_variables'
+require File.dirname(__FILE__) + '/object/misc'
diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb
index 19f42cabce..43a2be916e 100644
--- a/activesupport/lib/active_support/core_ext/object/extending.rb
+++ b/activesupport/lib/active_support/core_ext/object/extending.rb
@@ -24,25 +24,11 @@ class Object
ancestors = class << self; ancestors end
ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
end
-
- def copy_instance_variables_from(object, exclude = []) #:nodoc:
- 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) #:nodoc:
object.extended_by.each { |mod| extend mod }
end
- def instance_values #:nodoc:
- instance_variables.inject({}) do |values, name|
- values[name[1..-1]] = instance_variable_get(name)
- values
- end
- end
-
unless defined? instance_exec # 1.9
module InstanceExecMethods #:nodoc:
end
diff --git a/activesupport/lib/active_support/core_ext/object/instance_variables.rb b/activesupport/lib/active_support/core_ext/object/instance_variables.rb
index 72eaed70b6..e07eb76c1d 100644
--- a/activesupport/lib/active_support/core_ext/object/instance_variables.rb
+++ b/activesupport/lib/active_support/core_ext/object/instance_variables.rb
@@ -5,4 +5,18 @@ class Object
instance_variables.include?(variable.to_s)
end
end
+
+ def instance_values #:nodoc:
+ instance_variables.inject({}) do |values, name|
+ values[name.to_s[1..-1]] = instance_variable_get(name)
+ values
+ end
+ end
+
+ def copy_instance_variables_from(object, exclude = []) #:nodoc:
+ exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables
+
+ vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
+ vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
+ end
end
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 5e6144d64c..1987b70589 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -65,7 +65,7 @@ class ClassExtTest < Test::Unit::TestCase
def test_subclasses_of_should_not_return_removed_classes
# First create the removed class
- old_class = Nested.send :remove_const, :ClassL
+ old_class = Nested.class_eval { remove_const :ClassL }
new_class = Class.new(ClassK)
Nested.const_set :ClassL, new_class
assert_equal "Nested::ClassL", new_class.name # Sanity check
@@ -85,7 +85,7 @@ class ClassExtTest < Test::Unit::TestCase
assert !const_missing
assert_equal [ Nested::ClassL ], subclasses
- removed = Nested.send :remove_const, :ClassL # keep it in memory
+ removed = Nested.class_eval { remove_const :ClassL } # keep it in memory
subclasses = Object.subclasses_of ClassK
assert !const_missing
assert subclasses.empty?
@@ -188,7 +188,7 @@ class ObjectInstanceVariableTest < Test::Unit::TestCase
assert_equal [], @dest.instance_variables
@dest.copy_instance_variables_from(@source)
- assert_equal %w(@bar @baz), @dest.instance_variables.sort
+ assert_equal %w(@bar @baz), @dest.instance_variables.sort.map(&:to_s)
%w(@bar @baz).each do |name|
assert_equal @source.instance_variable_get(name).object_id,
@dest.instance_variable_get(name).object_id
@@ -197,7 +197,7 @@ class ObjectInstanceVariableTest < Test::Unit::TestCase
def test_copy_instance_variables_from_with_explicit_excludes
@dest.copy_instance_variables_from(@source, ['@baz'])
- assert !@dest.instance_variables.include?('@baz')
+ assert !@dest.instance_variable_defined?('@baz')
assert_equal 'bar', @dest.instance_variable_get('@bar')
end
@@ -210,8 +210,8 @@ class ObjectInstanceVariableTest < Test::Unit::TestCase
end
@dest.copy_instance_variables_from(@source)
- assert !@dest.instance_variables.include?('@bar')
- assert !@dest.instance_variables.include?('@quux')
+ assert !@dest.instance_variable_defined?('@bar')
+ assert !@dest.instance_variable_defined?('@quux')
assert_equal 'baz', @dest.instance_variable_get('@baz')
end