Sha256: 486bacfef691a6aeb7b6a36de155c3064181023edcbec07cc43b41544d4c6de7

Contents?: true

Size: 1.06 KB

Versions: 18

Compression:

Stored size: 1.06 KB

Contents

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

        class << self
          def 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
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ecoportal-api-0.8.2 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.7.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.7.4 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.7.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.7.2 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.7.1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.7.0 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.6.0 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.8 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.7 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.6 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.5.0 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.4.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.4.2 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.4.1 lib/ecoportal/api/common/hash_diff.rb