Module: Ecoportal::API::Common::Content::HashDiffPatch::InstanceMethods

Includes:
DocHelpers
Included in:
Ecoportal::API::Common::Content::HashDiffPatch
Defined in:
lib/ecoportal/api/common/content/hash_diff_patch.rb

Instance Method Summary collapse

Methods included from DocHelpers

#array_id_index, #array_id_item, #array_ids, #get_body, #get_id

Instance Method Details

#patch_diff(a, b) ⇒ Hash

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

The patch data is built as follows:

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

    {
       "id": "objectID",
       "operation": "OP_TYPE",
       "data": {
          "patch_ver": "prev_patch_ver_+1",
          "property":  "value",
          "...":       "..."
       }
    }
    
  2. the data property holds the specific changes of the object

  • the patch_ver (compulsory) is incremental (for data integrity)
  • the properties that have changed

Parameters:

  • a (Hash)

    current hash model

  • b (Hash)

    previous hash model

Returns:

  • (Hash)

    a patch data



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ecoportal/api/common/content/hash_diff_patch.rb', line 48

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