Class: JSON::MergePatch

Inherits:
Object
  • Object
show all
Defined in:
lib/json/merge_patch.rb,
lib/json/merge_patch/railtie.rb,
lib/json/merge_patch/version.rb

Overview

This class represents a merge patch.

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =

The current version of json-merge_patch

"1.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(orig, patch) ⇒ MergePatch

Sets up a MergePatch.

Parameters:

  • orig (Object)

    the original document

  • patch (Object)

    the patch document



30
31
32
33
# File 'lib/json/merge_patch.rb', line 30

def initialize(orig, patch)
  @orig = orig
  @patch = patch
end

Instance Method Details

#callObject

Applies the patch the original object.

Returns:

  • (Object)

    the document after applying the patch.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/json/merge_patch.rb', line 38

def call
  if @patch.nil?
    return @orig
  elsif @patch.kind_of?(Array) || @orig.kind_of?(Array)
    @orig = purge_nils(@patch)
  elsif is_primitive?(@patch) || is_primitive?(@orig)
    @orig = @patch
  elsif @patch.kind_of?(Hash)
    @patch.each_key do |m|
      if @orig.has_key?(m)
        if @patch[m].nil?
          @orig.delete(m)
        else
          if is_primitive?(@patch[m])
            @orig[m] = @patch[m]
          else
            if @orig[m].kind_of?(Array)
              @orig[m] = purge_nils(@patch[m])
            else
              @orig[m] = self.class.new(@orig[m], @patch[m]).call
            end
          end
        end
      elsif !(@patch[m].nil?)
        @orig[m] = purge_nils(@patch[m])
      end
    end
  end
  @orig
end