From 657b4ff04ad43bf26ded31ebfc003075f46dad53 Mon Sep 17 00:00:00 2001 From: Alexey Gaziev Date: Tue, 24 Apr 2012 13:59:55 +0400 Subject: Nice logic for deep_dup in rails --- activesupport/lib/active_support/core_ext/array.rb | 1 + activesupport/lib/active_support/core_ext/array/deep_dup.rb | 6 ++++++ activesupport/lib/active_support/core_ext/hash/deep_dup.rb | 6 ++---- activesupport/lib/active_support/core_ext/object.rb | 1 + activesupport/lib/active_support/core_ext/object/deep_dup.rb | 6 ++++++ 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/array/deep_dup.rb create mode 100644 activesupport/lib/active_support/core_ext/object/deep_dup.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index 79ba79192a..d6e1398a27 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -1,6 +1,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/array/access' require 'active_support/core_ext/array/uniq_by' +require 'active_support/core_ext/array/deep_dup' require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' diff --git a/activesupport/lib/active_support/core_ext/array/deep_dup.rb b/activesupport/lib/active_support/core_ext/array/deep_dup.rb new file mode 100644 index 0000000000..82f9805236 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/deep_dup.rb @@ -0,0 +1,6 @@ +class Array + # Returns a deep copy of array. + def deep_dup + map { |it| it.deep_dup } + end +end diff --git a/activesupport/lib/active_support/core_ext/hash/deep_dup.rb b/activesupport/lib/active_support/core_ext/hash/deep_dup.rb index 9ab179c566..882f27623e 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_dup.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_dup.rb @@ -1,10 +1,8 @@ class Hash # Returns a deep copy of hash. def deep_dup - duplicate = self.dup - duplicate.each_pair do |k,v| - duplicate[k] = v.is_a?(Hash) ? v.deep_dup : v + each_with_object(dup) do |(key, value), hash| + hash[key.deep_dup] = value.deep_dup end - duplicate end end diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 9ad1e12699..ec2157221f 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -1,6 +1,7 @@ require 'active_support/core_ext/object/acts_like' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/duplicable' +require 'active_support/core_ext/object/deep_dup' require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/inclusion' diff --git a/activesupport/lib/active_support/core_ext/object/deep_dup.rb b/activesupport/lib/active_support/core_ext/object/deep_dup.rb new file mode 100644 index 0000000000..daae98dde1 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/deep_dup.rb @@ -0,0 +1,6 @@ +class Object + # Returns a deep copy of object if it's duplicable. + def deep_dup + duplicable? ? dup : self + end +end -- cgit v1.2.3