aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorAlexey Gaziev <alex.gaziev@gmail.com>2012-04-24 13:59:55 +0400
committerPiotr Sarnacki <drogus@gmail.com>2012-05-06 10:28:35 -0700
commit657b4ff04ad43bf26ded31ebfc003075f46dad53 (patch)
tree8f7edd930e1568fa98b87607de7e1bbb2f5ebe44 /activesupport/lib
parented1703bcb206a740957c0a93df5f25177dedc0de (diff)
downloadrails-657b4ff04ad43bf26ded31ebfc003075f46dad53.tar.gz
rails-657b4ff04ad43bf26ded31ebfc003075f46dad53.tar.bz2
rails-657b4ff04ad43bf26ded31ebfc003075f46dad53.zip
Nice logic for deep_dup in rails
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/array.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/array/deep_dup.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/hash/deep_dup.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/object.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/object/deep_dup.rb6
5 files changed, 16 insertions, 4 deletions
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