From 78aa23b579985a35478d9e2ab723831588024b3a Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 11 Jul 2009 01:59:34 +0200 Subject: [PATCH] deprecate duplicable? and remove its usage from the framework --- activerecord/lib/active_record/base.rb | 2 +- .../connection_adapters/abstract/query_cache.rb | 2 +- .../lib/active_support/cache/memory_store.rb | 2 +- .../active_support/cache/strategy/local_cache.rb | 4 ++-- .../core_ext/class/inheritable_attributes.rb | 2 +- .../active_support/core_ext/object/duplicable.rb | 9 +++++++++ activesupport/test/core_ext/duplicable_test.rb | 10 +++++++--- 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index deab56e..280de4f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -3164,7 +3164,7 @@ module ActiveRecord #:nodoc: def clone_attribute_value(reader_method, attribute_name) value = send(reader_method, attribute_name) - value.duplicable? ? value.clone : value + value.clone rescue value # rescues in case value is not cloneable rescue TypeError, NoMethodError value end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb index 00c7109..8797b44 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb @@ -84,7 +84,7 @@ module ActiveRecord if Array === result result.collect { |row| row.dup } else - result.duplicable? ? result.dup : result + result.dup rescue result # rescues in case result is not duplicable end rescue TypeError result diff --git a/activesupport/lib/active_support/cache/memory_store.rb b/activesupport/lib/active_support/cache/memory_store.rb index 21ba79c..809a92b 100644 --- a/activesupport/lib/active_support/cache/memory_store.rb +++ b/activesupport/lib/active_support/cache/memory_store.rb @@ -26,7 +26,7 @@ module ActiveSupport def write(name, value, options = nil) super - @data[name] = (value.duplicable? ? value.dup : value).freeze + @data[name] = (value.dup rescue value).freeze # rescues in case value is not duplicable end def delete(name, options = nil) diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 3b5fccc..fdfd28d 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -45,10 +45,10 @@ module ActiveSupport elsif value.nil? value = super local_cache.write(key, value || NULL) if local_cache - value.duplicable? ? value.dup : value + value.dup rescue value # rescues in case value is not duplicable else # forcing the value to be immutable - value.duplicable? ? value.dup : value + value.dup rescue value # rescues in case value is not duplicable end end diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb index 8bac2df..3372cb3 100644 --- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb @@ -130,7 +130,7 @@ class Class # :nodoc: new_inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES else new_inheritable_attributes = inheritable_attributes.inject({}) do |memo, (key, value)| - memo.update(key => value.duplicable? ? value.dup : value) + memo.update(key => (value.dup rescue value)) # rescues in case value is not duplicable end end diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index 8f49ddf..3c9c8e4 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -1,43 +1,52 @@ +require 'active_support/core_ext/module/deprecation' + class Object # Can you safely .dup this object? # False for nil, false, true, symbols, and numbers; true otherwise. def duplicable? true end + deprecate :duplicable? end class NilClass #:nodoc: def duplicable? false end + deprecate :duplicable? end class FalseClass #:nodoc: def duplicable? false end + deprecate :duplicable? end class TrueClass #:nodoc: def duplicable? false end + deprecate :duplicable? end class Symbol #:nodoc: def duplicable? false end + deprecate :duplicable? end class Numeric #:nodoc: def duplicable? false end + deprecate :duplicable? end class Class #:nodoc: def duplicable? false end + deprecate :duplicable? end diff --git a/activesupport/test/core_ext/duplicable_test.rb b/activesupport/test/core_ext/duplicable_test.rb index 6e1f876..127c2cd 100644 --- a/activesupport/test/core_ext/duplicable_test.rb +++ b/activesupport/test/core_ext/duplicable_test.rb @@ -2,13 +2,15 @@ require 'abstract_unit' require 'bigdecimal' require 'active_support/core_ext/object/duplicable' -class DuplicableTest < Test::Unit::TestCase +class DuplicableTest < ActiveSupport::TestCase NO = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56'), Class.new] YES = ['1', Object.new, /foo/, [], {}, Time.now] def test_duplicable NO.each do |v| - assert !v.duplicable? + assert_deprecated do + assert !v.duplicable? + end begin v.dup fail @@ -17,7 +19,9 @@ class DuplicableTest < Test::Unit::TestCase end YES.each do |v| - assert v.duplicable? + assert_deprecated do + assert v.duplicable? + end assert_nothing_raised { v.dup } end end -- 1.6.3.2