Sha256: fe90ddb034f40687a00bade323487cc7dfb7c47baf4a70c2f9c3003c69bb4bee

Contents?: true

Size: 1004 Bytes

Versions: 5

Compression:

Stored size: 1004 Bytes

Contents

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

        def self.diff(a, b)
          return a if a.class != b.class
          case a
          when Hash
            {}.tap do |diffed|
              a.each do |key, a_value|
                b_value = b[key]
                next if a_value == b_value && !ID_KEYS.include?(key)
                diffed[key] = diff(a_value, b_value)
                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 a.length == b.length
            a.map.with_index do |a_value, idx|
              b_value = b[idx]
              diff(a_value, b_value)
            end.reject do |el|
              el == {}
            end
          else
            a
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ecoportal-api-0.1.12 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.11 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.10 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.9 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.8 lib/ecoportal/api/common/hash_diff.rb