nice_hash 1.18.1 → 1.18.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nice/hash/add_to_ruby.rb +13 -6
- data/lib/nice/hash/deep_clone.rb +30 -0
- data/lib/nice_hash.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28cebce97247546c4f1db4d7f7fda1d307e24cecd2cd8a235782f2c8765092e2
|
4
|
+
data.tar.gz: cc3fca8a245ad2dd48d0bd3d1653ef09c2aa371fe3509304a0c04795bc3e2597
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 196b9c9e068ef89415b6b2883deaec846a1b7b5ccb3b0c835ed2d50e02f409344ecb4bbfd5a4c87cacf46181765f7bf52b0ee945dff5eec81180a3b5c20e93b6
|
7
|
+
data.tar.gz: '09ce814e6899f7c163a5c5c009e1617f33262809b7401ee6d3e8c4e73c1a18c4377cf21d80e1d0dc4d6f2a8da0266f0df5aefe599f4adbc77318c41f0f9a0fa3'
|
@@ -198,8 +198,8 @@ class Hash
|
|
198
198
|
# returns a clean copy of the hash
|
199
199
|
###########################################################################
|
200
200
|
def deep_copy
|
201
|
-
|
202
|
-
end
|
201
|
+
NiceHash.deep_clone(self)
|
202
|
+
end
|
203
203
|
|
204
204
|
###########################################################################
|
205
205
|
# It will filter the hash by the key specified on select_hash_key.
|
@@ -291,18 +291,25 @@ class Hash
|
|
291
291
|
###########################################################################
|
292
292
|
# Merging multi-dimensional hashes
|
293
293
|
###########################################################################
|
294
|
-
def nice_merge(hash = nil)
|
295
|
-
|
296
|
-
|
294
|
+
def nice_merge(hash = nil, return_self = false)
|
295
|
+
if return_self
|
296
|
+
base = self
|
297
|
+
else
|
298
|
+
base = self.deep_copy
|
299
|
+
end
|
300
|
+
return base unless hash.is_a?(Hash)
|
297
301
|
hash.each do |key, v|
|
298
302
|
if base[key].is_a?(Hash) && hash[key].is_a?(Hash)
|
299
|
-
base[key].nice_merge(hash[key])
|
303
|
+
base[key].nice_merge!(hash[key])
|
300
304
|
else
|
301
305
|
base[key]= hash[key]
|
302
306
|
end
|
303
307
|
end
|
304
308
|
base
|
305
309
|
end
|
310
|
+
def nice_merge!(hash = nil)
|
311
|
+
return nice_merge(hash, true)
|
312
|
+
end
|
306
313
|
|
307
314
|
alias gen generate
|
308
315
|
alias val validate
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class NiceHash
|
2
|
+
##################################################
|
3
|
+
# Deep clones the supplied object
|
4
|
+
#
|
5
|
+
# @param obj [Object] The object we want to deep clone
|
6
|
+
#
|
7
|
+
# @return [Object]
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# my_hash = { user: {
|
11
|
+
# address: {
|
12
|
+
# city: 'Madrid',
|
13
|
+
# country: 'Spain'
|
14
|
+
# },
|
15
|
+
# name: 'Peter',
|
16
|
+
# age: 33
|
17
|
+
# },
|
18
|
+
# customer: true
|
19
|
+
# }
|
20
|
+
# NiceHash.deep_clone(my_hash)
|
21
|
+
# #>{:user=>{:address=>{:city=>"Madrid", :country=>"Spain"}, :name=>"Peter", :age=>33}, :customer=>true}
|
22
|
+
##################################################
|
23
|
+
def self.deep_clone(obj)
|
24
|
+
obj.clone.tap do |new_obj|
|
25
|
+
new_obj.each do |key, val|
|
26
|
+
new_obj[key] = deep_clone(val) if val.is_a?(Hash)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/nice_hash.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nice_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.18.
|
4
|
+
version: 1.18.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Ruiz
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- lib/nice/hash/add_to_ruby.rb
|
70
70
|
- lib/nice/hash/change_one_by_one.rb
|
71
71
|
- lib/nice/hash/compare_structure.rb
|
72
|
+
- lib/nice/hash/deep_clone.rb
|
72
73
|
- lib/nice/hash/delete_nested.rb
|
73
74
|
- lib/nice/hash/generate.rb
|
74
75
|
- lib/nice/hash/get_all_keys.rb
|