Sha256: 4cf4318ac8ec1160bdb5a222ae7dce91f8339392852f01828f34e0ece9e6ac32

Contents?: true

Size: 1001 Bytes

Versions: 8

Compression:

Stored size: 1001 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

8 entries across 8 versions & 1 rubygems

Version Path
ecoportal-api-0.1.7 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.6 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.4 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.2 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.1.0 lib/ecoportal/api/common/hash_diff.rb