Class: ObjectPatch::Operations::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/object_patch/operations/test.rb

Overview

An implementation of the JSON patch test operation.

Instance Method Summary collapse

Constructor Details

#initialize(patch_data) ⇒ void

Setup the test operation with any required arguments.

Parameters:

  • patch_data (Hash)

    Parameters necessary to build the operation.

Options Hash (patch_data):

  • path (String)

    The location in the target document to test.



26
27
28
29
# File 'lib/object_patch/operations/test.rb', line 26

def initialize(patch_data)
  @path = patch_data.fetch('path')
  @value = patch_data.fetch('value')
end

Instance Method Details

#apply(target_doc) ⇒ Object

A simple test to validate the value at the expected location matches the value in the patch information. Will raise an error if the test fails.

Parameters:

  • target_doc (Object)

Returns:

  • (Object)

    Unmodified version of the document.



12
13
14
15
16
17
18
# File 'lib/object_patch/operations/test.rb', line 12

def apply(target_doc)
  unless @value == ObjectPatch::Pointer.eval(processed_path, target_doc)
    raise ObjectPatch::FailedTestException.new(@value, @path)
  end

  target_doc
end

#processed_pathArray<String>

Returns the path after being expanded by the JSON pointer semantics.

Returns:

  • (Array<String>)

    Expanded pointer path



34
35
36
# File 'lib/object_patch/operations/test.rb', line 34

def processed_path
  ObjectPatch::Pointer.parse(@path)
end

#to_patchHash<String => String>

Covert this operation to a format that can be built into a full on JSON patch.

Returns:

  • (Hash<String => String>)

    JSON patch test operation



42
43
44
# File 'lib/object_patch/operations/test.rb', line 42

def to_patch
  { 'op' => 'test', 'path' => @path, 'value' => @value }
end