Module: Ecoportal::API::Common::GraphQL::Model::Diffable::HashDiffNesting::InstanceMethods

Included in:
Ecoportal::API::Common::GraphQL::Model::Diffable::HashDiffNesting
Defined in:
lib/ecoportal/api/common/graphql/model/diffable/hash_diff_nesting.rb

Instance Method Summary collapse

Instance Method Details

#change_diff(a, b, ignore: []) ⇒ Hash

Note:
  • there should not be difference between null and "" (empty string)

The change data is built as follows:

  1. detect changes that have occurred translate into one api_operation of OP_TYPE:
* `:update`: meaning that the object has changed (existed and has not been removed)
* `:delete`:  the object has been removed
* `:create`:     the object is new
  1. at the level of the target object of the model, the object is opened for change with id and api_operation as follows:

    {
       "id":            "objectID",
       "api_operation": "OP_TYPE",
       "change_data" {
         "property":      "value",
          "...":          "..."
       }
    }
    
  2. the change_data property holds the specific changes of the object

  • the properties that have changed

Parameters:

  • a (Hash)

    current hash model

  • b (Hash)

    previous hash model

Returns:

  • (Hash)

    a change data



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ecoportal/api/common/graphql/model/diffable/hash_diff_nesting.rb', line 45

def change_diff(a, b, ignore: [])
  ignore = to_a(ignore)

  if b.is_a?(Hash) && !empty?(b) && empty?(a)
    patch_delete(b)
  elsif a.is_a?(Hash) && !empty?(a) && empty?(b)
    patch_new(a, ignore: ignore)
  elsif a.is_a?(Hash) && b.is_a?(Hash)
    patch_update(a, b, ignore: ignore)
  elsif any_array?(a, b)
    patch_data_array(a, b, ignore: ignore)
  else
    a
  end
end