Module: Ecoportal::API::Common::GraphQL::Model::Diffable::HashDiff::InstanceMethods

Defined in:
lib/ecoportal/api/common/graphql/model/diffable/hash_diff.rb

Instance Method Summary collapse

Instance Method Details

#diff(a, b, ignore: []) ⇒ Object

Note:

the aim is to delegate hash_diff to the specific classes of the model... bulding the payload/input_base in a cascaded way (rather than in a one-off way from the top alone).

@todo: refactor to only reach the current level



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ecoportal/api/common/graphql/model/diffable/hash_diff.rb', line 22

def diff(a, b, ignore: [])
  case a
  when Hash
    {}.tap do |diffed|
      a.each do |key, a_value|
        b_value    = b[key] if b
        no_changes = (a_value == b_value) || ignore.include?(key)
        next if !ID_KEYS.include?(key) && no_changes

        diffed[key] = diff(a_value, b_value, ignore: ignore)
        diffed.delete(key) if diffed[key] == {}
      end

      # All keys are IDs, so it's actually blank

      return {} if (diffed.keys - ID_KEYS).empty?
    end
  when Array
    return a unless b.is_a?(Array) && a.length == b.length

    a.map.with_index do |a_value, idx|
      b_value = b[idx]
      diff(a_value, b_value, ignore: ignore)
    end.reject do |el|
      el == {}
    end
  else
    a
  end
end