class ObjectPatch::Operations::Test

An implementation of the JSON patch test operation.

Public Class Methods

new(patch_data) click to toggle source

Setup the test operation with any required arguments.

@param [Hash] patch_data Parameters necessary to build the operation. @option patch_data [String] path The location in the target document to

test.

@return [void]

# File lib/object_patch/operations/test.rb, line 26
def initialize(patch_data)
  @path = patch_data.fetch('path')
  @value = patch_data.fetch('value')
end

Public Instance Methods

apply(target_doc) click to toggle source

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.

@param [Object] target_doc @return [Object] Unmodified version of the document.

# 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_path() click to toggle source

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

@return [Array<String>] Expanded pointer path

# File lib/object_patch/operations/test.rb, line 34
def processed_path
  ObjectPatch::Pointer.parse(@path)
end
to_patch() click to toggle source

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

@return [Hash<String => String>] JSON patch test operation

# File lib/object_patch/operations/test.rb, line 42
def to_patch
  { 'op' => 'test', 'path' => @path, 'value' => @value }
end