Module: Philiprehberger::JsonMerge::Diff

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

Overview

Generate RFC 6902 JSON Patch operations from two documents

Class Method Summary collapse

Class Method Details

.call(source, target, path = '') ⇒ Array<Hash>

Generate an array of RFC 6902 operations that transform source into target

Parameters:

  • source (Hash, Array, Object)

    the source document

  • target (Hash, Array, Object)

    the target document

  • path (String) (defaults to: '')

    the current JSON Pointer path prefix

Returns:

  • (Array<Hash>)

    array of patch operations



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/philiprehberger/json_merge/diff.rb', line 13

def self.call(source, target, path = '')
  return [] if source == target

  operations = []

  if source.is_a?(Hash) && target.is_a?(Hash)
    diff_hashes(source, target, path, operations)
  elsif source.is_a?(Array) && target.is_a?(Array)
    diff_arrays(source, target, path, operations)
  else
    operations << { 'op' => 'replace', 'path' => path, 'value' => target }
  end

  operations
end