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.

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.



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.



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.



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

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