Class: ObjectPatch::Operations::Move
- Inherits:
-
Object
- Object
- ObjectPatch::Operations::Move
- Defined in:
- lib/object_patch/operations/move.rb
Overview
A representation of a JSON pointer move operation.
Instance Method Summary collapse
-
#apply(target_doc) ⇒ Object
Apply this operation to the provided document and return the updated document.
-
#initialize(patch_data) ⇒ void
constructor
Setup the replace operation with any required arguments.
-
#processed_from ⇒ Array<String>
Returns the from field after being expanded by the JSON pointer semantics.
-
#processed_path ⇒ Array<String>
Returns the path after being expanded by the JSON pointer semantics.
-
#to_patch ⇒ Hash<String => String>
Covert this operation to a format that can be built into a full on JSON patch.
Constructor Details
#initialize(patch_data) ⇒ void
Setup the replace operation with any required arguments.
35 36 37 38 |
# File 'lib/object_patch/operations/move.rb', line 35 def initialize(patch_data) @from = patch_data.fetch('from') @path = patch_data.fetch('path') end |
Instance Method Details
#apply(target_doc) ⇒ Object
Apply this operation to the provided document and return the updated document. Please note that the changes will be reflected not only in the returned value but the original document that was passed in as well.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/object_patch/operations/move.rb', line 14 def apply(target_doc) src_key = processed_from.last dst_key = processed_path.last src_obj = ObjectPatch::Pointer.eval(processed_from[0...-1], target_doc) dst_obj = ObjectPatch::Pointer.eval(processed_path[0...-1], target_doc) moved_value = ObjectPatch::Operations.rm_op(src_obj, src_key) ObjectPatch::Operations.add_op(dst_obj, dst_key, moved_value) target_doc end |
#processed_from ⇒ Array<String>
Returns the from field after being expanded by the JSON pointer semantics.
43 44 45 |
# File 'lib/object_patch/operations/move.rb', line 43 def processed_from ObjectPatch::Pointer.parse(@from) end |
#processed_path ⇒ Array<String>
Returns the path after being expanded by the JSON pointer semantics.
50 51 52 |
# File 'lib/object_patch/operations/move.rb', line 50 def processed_path ObjectPatch::Pointer.parse(@path) end |
#to_patch ⇒ Hash<String => String>
Covert this operation to a format that can be built into a full on JSON patch.
58 59 60 |
# File 'lib/object_patch/operations/move.rb', line 58 def to_patch { 'op' => 'move', 'from' => @from, 'path' => @path } end |