Sha256: 9b1be5544a1a39fc886aa476a0f92ae0d904d10386c468d0a4ecda0d497d3c15
Contents?: true
Size: 1.24 KB
Versions: 20
Compression:
Stored size: 1.24 KB
Contents
module HybridPlatformsConductor # Provide an easy way to safe-merge hashes module SafeMerge # Safe-merge 2 hashes. # Safe-merging is done by: # * Merging values that are hashes. # * Reporting errors when values conflict. # When values are conflicting, the initial hash won't modify those conflicting values and will stop the merge. # # Parameters:: # * *hash* (Hash): Hash to be modified merging hash_to_merge # * *hash_to_merge* (Hash): Hash to be merged into hash # Result:: # * nil or Array<Object>: nil in case of success, or the keys path leading to a conflicting value in case of error def safe_merge(hash, hash_to_merge) conflicting_path = nil hash_to_merge.each do |key, value_to_merge| if hash.key?(key) if hash[key].is_a?(Hash) && value_to_merge.is_a?(Hash) sub_conflicting_path = safe_merge(hash[key], value_to_merge) conflicting_path = [key] + sub_conflicting_path unless sub_conflicting_path.nil? elsif hash[key] != value_to_merge conflicting_path = [key] end else hash[key] = value_to_merge end break unless conflicting_path.nil? end conflicting_path end end end
Version data entries
20 entries across 20 versions & 1 rubygems