Module: Gcp::Api::Discovery::Watcher::DiffHash

Defined in:
lib/gcp/api/discovery/watcher/diff_hash.rb

Class Method Summary collapse

Class Method Details

.diff_hash(a, b, port = $stdout, current_keys = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gcp/api/discovery/watcher/diff_hash.rb', line 16

def diff_hash(a, b, port=$stdout, current_keys=[])
  a = a.to_hash
  b = b.to_hash
  a_keys = a.keys.sort
  b_keys = b.keys.sort
  (a_keys - b_keys).each do |k|
    port << "\n--- '#{(current_keys+[k]).join(".")}':"
    port << a[k].pretty_inspect
  end
  (b_keys - a_keys).each do |k|
    port << "\n+++ '#{(current_keys+[k]).join(".")}':"
    port << b[k].pretty_inspect
  end
  (a_keys & b_keys).each do |k|
    unless a[k] == b[k]
      if a[k].is_a?(Hash) and b[k].is_a?(Hash)
        diff_hash(a[k], b[k], port, current_keys + [k])
      else
        port << "\n'#{(current_keys+[k]).join(".")}' is changed:\n"
        if a[k].is_a?(String) and b[k].is_a?(String)
          port << Diffy::Diff.new(a[k], b[k]).to_s
        else
          port << "--- #{a[k].to_json}\n"
          port << "+++ #{b[k].to_json}\n"
        end
      end
    end
  end
end