Sha256: 5327709ff25cc824ccc6917809dab820316642df318b51a9cf951de5d61b6b6b

Contents?: true

Size: 1004 Bytes

Versions: 11

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

11 entries across 11 versions & 1 rubygems

Version Path
ecoportal-api-0.3.8 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.6 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.4 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.0 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.3.0.pre1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.2.2 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.2.1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.2.0 lib/ecoportal/api/common/hash_diff.rb