Sha256: f671e15d5a56d58b748c0157ee813617c5bef1c9ed2f0b97dec90c4b5db4b6bd

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Ecoportal
  module API
    module Common
      module HashDiff
        ID_KEYS = %w[id]

        class << self

          def diff(a, b, ignore: [])
            case a
            when Hash
              {}.tap do |diffed|
                a.each do |key, a_value|
                  b_value    = b && b[key]
                  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
                if (diffed.keys - ID_KEYS).empty?
                  return {}
                end
              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
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecoportal-api-0.8.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.8.4 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.8.3 lib/ecoportal/api/common/hash_diff.rb