Class: ObjectPatch::Operations::Replace
- Inherits:
-
Object
- Object
- ObjectPatch::Operations::Replace
- Defined in:
- lib/object_patch/operations/replace.rb
Overview
A representation of a JSON pointer replace 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_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.
38 39 40 41 |
# File 'lib/object_patch/operations/replace.rb', line 38 def initialize(patch_data) @path = patch_data.fetch('path') @value = patch_data.fetch('value') 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 26 27 28 |
# File 'lib/object_patch/operations/replace.rb', line 14 def apply(target_doc) return @value if processed_path.empty? key = processed_path.last inner_obj = ObjectPatch::Pointer.eval(processed_path[0...-1], target_doc) if inner_obj.is_a?(Array) raise ObjectPatch::InvalidIndexError unless key =~ /\A\d+\Z/ inner_obj[key.to_i] = @value else inner_obj[key] = @value end target_doc end |
#processed_path ⇒ Array<String>
Returns the path after being expanded by the JSON pointer semantics.
46 47 48 |
# File 'lib/object_patch/operations/replace.rb', line 46 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.
54 55 56 |
# File 'lib/object_patch/operations/replace.rb', line 54 def to_patch { 'op' => 'replace', 'path' => @path, 'value' => @value } end |