Sha256: 438050e86dcbdb3c0226f4b92ad1dc09a098f9d5b9eb6801b7cec57ce81f1b94

Contents?: true

Size: 1.2 KB

Versions: 15

Compression:

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

15 entries across 15 versions & 1 rubygems

Version Path
ecoportal-api-0.10.8 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.7 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.6 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.4 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.2 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.1 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.10.0 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.9.7 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.9.6 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.9.5 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.9.4 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.9.3 lib/ecoportal/api/common/hash_diff.rb
ecoportal-api-0.9.2 lib/ecoportal/api/common/hash_diff.rb