Class: SafeDb::Struct

Inherits:
Object
  • Object
show all
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

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

  1. a String - the incoming value is rejected and logged

  2. a Hash - the method is recalled with these nested Hashes as parameters

Parameters:

  • struct_1 (Hash)

    the current receiving hash data structure

  • struct_2 (Hash)

    the incoming hash data structure to merge



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 )

    reject_message( key, value_1, value_2 ) if not_the_same
    recursively_merge!( value_1, value_2 ) if is_mergeable
    value_1

  end


end