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

Extended by:
DocHelpers
Defined in:
lib/ecoportal/api/common/content/hash_diff_patch.rb

Constant Summary collapse

ID_KEYS =
%w[id]
META_KEYS =
%w[id patch_ver]
NO_CHANGES =
"%not-changed!%"

Class Method Summary collapse

Methods included from DocHelpers

array_id_index, array_id_item, array_ids, get_body, get_id

Class 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)
    • delete: the object has been removed
    • new: the object is new
  2. 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",
         "...":       "..."
      }
   }
  1. 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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ecoportal/api/common/content/hash_diff_patch.rb', line 39

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