Class: SafeDb::Struct
- Inherits:
-
Object
- Object
- SafeDb::Struct
- Defined in:
- lib/utils/store/struct.rb
Overview
Data structure class provides behaviour for holding managing curating and most important of all, merging, data.
How to Merge Data Structures
Recursively merge (deep merge) two Hash data structures. The core ruby Hash.merge() instance method only performs first level merges which is not ideal if you need to intelligently merge a deep tree.
Class Method Summary collapse
-
.recursively_merge!(struct_1, struct_2) ⇒ Object
Recursively merge (deep merge) two Hash data structures.
Class Method Details
.recursively_merge!(struct_1, struct_2) ⇒ Object
Recursively merge (deep merge) two Hash data structures. The core ruby Hash.merge() instance method only performs first level merges which is not ideal if you need to intelligently merge a deep tree.
Merge Behaviour
Currently this behaviour works only for Hash data structures that have string keys (only) and either string or Hash values. It is interesting only when duplicate keys are encountered at the same level. If the duplicate key’s value is
-
a String - the incoming value is rejected and logged
-
a Hash - the method is recalled with these nested Hashes as parameters
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/utils/store/struct.rb', line 31 def self.recursively_merge!( struct_1, struct_2 ) struct_1.merge!( struct_2 ) do | key, value_1, value_2 | is_mergeable = value_1.kind_of?( Hash ) && value_2.kind_of?( Hash ) are_both_str = value_1.kind_of?( String ) && value_2.kind_of?( String ) not_the_same = are_both_str && ( value_1 != value_2 ) ( key, value_1, value_2 ) if not_the_same recursively_merge!( value_1, value_2 ) if is_mergeable value_1 end end |