Class: JSONP3::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.
244 245 246 247 248 |
# File 'lib/json_p3/patch.rb', line 244 def initialize(from, pointer) super() @from = from @pointer = pointer end |
Instance Method Details
#apply(value, index) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/json_p3/patch.rb', line 254 def apply(value, index) # Grab the source value. _source_parent, source_obj = @from.resolve_with_parent(value) if source_obj == JSONP3::JSONPointer::UNDEFINED raise JSONPatchError, "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::JSONPointer::UNDEFINED dest_target = @pointer.tokens.last if dest_target == JSONP3::JSONPointer::UNDEFINED raise JSONPatchError, "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 JSONPatchError, "unexpected operation on #{dest_parent.class} (#{name}:#{index})" end value end |
#name ⇒ Object
250 251 252 |
# File 'lib/json_p3/patch.rb', line 250 def name "copy" end |
#to_h ⇒ Object
288 289 290 |
# File 'lib/json_p3/patch.rb', line 288 def to_h { "op" => name, "from" => @from.to_s, "path" => @pointer.to_s } end |