Module: ObjectPatch

Defined in:
lib/object_patch.rb,
lib/object_patch/pointer.rb,
lib/object_patch/version.rb,
lib/object_patch/generator.rb,
lib/object_patch/exceptions.rb,
lib/object_patch/operations.rb,
lib/object_patch/operation_factory.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Generator, OperationFactory, Operations, Pointer Classes: FailedTestException

Constant Summary collapse

VERSION =

The current gem version.

"0.8.1"
BaseException =

This is a parent exception for anything that is going to get raised by this gem, allowing users of this code to catch all the subclasses.

Class.new(StandardError)
InvalidIndexError =

Raised when the index value that was attempted to be accessed isn’t a numeric identifier or ‘-’ (the special index value defined in JSON Pointer).

Class.new(BaseException)
InvalidOperation =

An exception that gets raised when a patch contains an invalid operation.

Class.new(BaseException)
MissingTargetException =

An exception that gets raised when an operation takes place on a missing hash key, or a missing hash key is somewhere along the path.

Class.new(BaseException)
ObjectOperationOnArrayException =

Raised when a non-integer value is attempted to be used to access some part of an array.

Class.new(BaseException)
OutOfBoundsException =

When an integer value outside the available range of an array is used to access an array this will get raised.

Class.new(BaseException)
TraverseScalarException =

When the path provided attempts to cross a scalar value, this exception will be raised.

Class.new(BaseException)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply(source, patches) ⇒ Object

Applies a series of patches to a source document. It is important to note that this will return the changed document but will also modify the original document that got passed in, using dup also isn’t enough to prevent this modification as any subvalues within a hash or an array will not by duplicated. A deep duplication by marshalling the object, encoding and decoding as JSON, or using a recursive function to duplicate all of an object’s contents would prevent your source from being modified.

The JSON Patch RFC specifies that the original document shouldn’t be modified unless the whole series of patches is able to be applied successfully.

Parameters:

  • source (Object)

    The source document that will be modified.

  • patches (Array<Hash<String => String>>)

    An array of JSON patch operations that should be applied to the source document.

Returns:

  • (Object)

    The modified source document.



28
29
30
31
32
# File 'lib/object_patch.rb', line 28

def apply(source, patches)
  patches.inject(source) do |src, patch|
    OperationFactory.build(patch).apply(src)
  end
end

.generate(source, target) ⇒ Array<Hash>

Proxies the generation of a new set of patches to the Generator.

Parameters:

  • source (Object)

    The source document

  • target (Object)

    The target document we’ll generate the differences for.

Returns:

  • (Array<Hash>)

See Also:

  • [Generator[Generator#generate]


41
42
43
# File 'lib/object_patch.rb', line 41

def generate(source, target)
  Generator.generate(source, target)
end

Instance Method Details

#apply(source, patches) ⇒ Object (private)

Applies a series of patches to a source document. It is important to note that this will return the changed document but will also modify the original document that got passed in, using dup also isn’t enough to prevent this modification as any subvalues within a hash or an array will not by duplicated. A deep duplication by marshalling the object, encoding and decoding as JSON, or using a recursive function to duplicate all of an object’s contents would prevent your source from being modified.

The JSON Patch RFC specifies that the original document shouldn’t be modified unless the whole series of patches is able to be applied successfully.

Parameters:

  • source (Object)

    The source document that will be modified.

  • patches (Array<Hash<String => String>>)

    An array of JSON patch operations that should be applied to the source document.

Returns:

  • (Object)

    The modified source document.



28
29
30
31
32
# File 'lib/object_patch.rb', line 28

def apply(source, patches)
  patches.inject(source) do |src, patch|
    OperationFactory.build(patch).apply(src)
  end
end

#generate(source, target) ⇒ Array<Hash> (private)

Proxies the generation of a new set of patches to the Generator.

Parameters:

  • source (Object)

    The source document

  • target (Object)

    The target document we’ll generate the differences for.

Returns:

  • (Array<Hash>)

See Also:

  • ObjectPatch.[Generator[Generator#generate]


41
42
43
# File 'lib/object_patch.rb', line 41

def generate(source, target)
  Generator.generate(source, target)
end