Class: JSONP3::Patch::OpCopy
Overview
The JSON Patch copy operation.
Instance Method Summary collapse
- #apply!(value, index) ⇒ Object
-
#initialize(from, pointer) ⇒ OpCopy
constructor
A new instance of OpCopy.
- #name ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(from, pointer) ⇒ OpCopy
Returns a new instance of OpCopy.
9 10 11 12 13 |
# File 'lib/json_p3/patch/op_copy.rb', line 9 def initialize(from, pointer) super() @from = from @pointer = pointer end |
Instance Method Details
#apply!(value, index) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/json_p3/patch/op_copy.rb', line 19 def apply!(value, index) # Grab the source value. _source_parent, source_obj = @from.resolve_with_parent(value) if source_obj == JSONP3::Pointer::UNDEFINED raise JSONP3::Patch::Error, "source object does not exist (#{name}:#{index})" end # Find the parent of the destination pointer. dest_parent, _dest_obj = @pointer.resolve_with_parent(value) return deep_copy(source_obj) if dest_parent == JSONP3::Pointer::UNDEFINED dest_target = @pointer.tokens.last if dest_target == JSONP3::Pointer::UNDEFINED raise JSONP3::Patch::Error, "unexpected operation (#{name}:#{index})" end # Write the source value to the destination. if dest_parent.is_a?(Array) if dest_target == "-" dest_parent << source_obj else dest_parent.insert(dest_target.to_i, deep_copy(source_obj)) end elsif dest_parent.is_a?(Hash) dest_parent[dest_target] = deep_copy(source_obj) else raise JSONP3::Patch::Error, "unexpected operation on #{dest_parent.class} (#{name}:#{index})" end value end |
#name ⇒ Object
15 16 17 |
# File 'lib/json_p3/patch/op_copy.rb', line 15 def name "copy" end |
#to_h ⇒ Object
53 54 55 |
# File 'lib/json_p3/patch/op_copy.rb', line 53 def to_h { "op" => name, "from" => @from.to_s, "path" => @pointer.to_s } end |