Module: Philiprehberger::JsonMerge::JsonPatch

Defined in:
lib/philiprehberger/json_merge/json_patch.rb

Overview

RFC 6902 JSON Patch implementation

Supports add, remove, replace, move, copy, and test operations. Paths use JSON Pointer (RFC 6901) syntax.

Class Method Summary collapse

Class Method Details

.call(target, operations) ⇒ Hash, Array

Apply an array of RFC 6902 operations to a target document

Parameters:

  • target (Hash, Array)

    the target document

  • operations (Array<Hash>)

    array of patch operations

Returns:

  • (Hash, Array)

    the patched document

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/philiprehberger/json_merge/json_patch.rb', line 16

def self.call(target, operations)
  result = deep_clone(target)

  operations.each_with_index do |operation, index|
    op = (operation['op'] || operation[:op]).to_s

    result = case op
             when 'add'    then apply_add(result, operation, index)
             when 'remove' then apply_remove(result, operation, index)
             when 'replace' then apply_replace(result, operation, index)
             when 'move'   then apply_move(result, operation, index)
             when 'copy'   then apply_copy(result, operation, index)
             when 'test'   then apply_test(result, operation, index)
             else
               raise Error, "Unknown operation '#{op}' at index #{index}"
             end
  end

  result
end