Method: DataTools::Hash#compare

Defined in:
lib/data_tools/hash.rb

#compare(hash2) ⇒ Object

HASH OF HASHES compare to another hash-of-hashes (aka changes, deltas, diffs) report the changes between a current state and a future state (hash2) each of the four sections (new elements, lost elements, unchanged elements, changes) is another hash-of-hashes



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/data_tools/hash.rb', line 109

def compare(hash2)
  newkeys = hash2.keys - self.keys
  lostkeys = self.keys - hash2.keys
  commonkeys = self.keys & hash2.keys

  unchanged = []
  changes = {}
  commonkeys.each do |k|
    if (diffs = hash2[k].diff(self[k])).any?
      changes[k] = diffs
    else
      unchanged << k
    end
  end

  {
    :new => hash2.slice(*newkeys),
    :lost => self.slice(*lostkeys),
    :unchanged => self.slice(*unchanged),
    :changes => changes
  }
end