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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5eb4297991e401679fa80b27d5317ad1e7ed2454879b1717c920bd862f7de53f
4
- data.tar.gz: beb646818a282748bb694d117533ea339d2a702ca7c024746846894e190ab05d
3
+ metadata.gz: 28cebce97247546c4f1db4d7f7fda1d307e24cecd2cd8a235782f2c8765092e2
4
+ data.tar.gz: cc3fca8a245ad2dd48d0bd3d1653ef09c2aa371fe3509304a0c04795bc3e2597
5
5
  SHA512:
6
- metadata.gz: 8332df937c26fc60091db0fb821c4b585029b60976a703d49439adef13f1df03af721b31bb68c1965413ed02bcbbf6ccb005f280ea44731a8b4c5af923610b59
7
- data.tar.gz: d18ed3d408b262fcc9f982b4cc1bbbaf5795d1f701be3d4aa3ebd80c532156a88a6380e1c9c657d13b050c79ad77275b9a8792d502bdc5e3eb60fc7b306d0a8d
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
- Marshal.load(Marshal.dump(self))
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
- return self unless hash.is_a?(Hash)
296
- base = self
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
@@ -16,6 +16,7 @@ require_relative "nice/hash/set_nested"
16
16
  require_relative "nice/hash/set_values"
17
17
  require_relative "nice/hash/transtring"
18
18
  require_relative "nice/hash/validate"
19
+ require_relative "nice/hash/deep_clone"
19
20
 
20
21
 
21
22
  require "string_pattern"
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.1
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