Class: JSONP3::Patch::OpReplace
- Defined in:
- lib/json_p3/patch/op_replace.rb
Overview
The JSON Patch replace operation.
Instance Method Summary collapse
- #apply!(value, index) ⇒ Object
-
#initialize(pointer, value) ⇒ OpReplace
constructor
A new instance of OpReplace.
- #name ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(pointer, value) ⇒ OpReplace
Returns a new instance of OpReplace.
9 10 11 12 13 |
# File 'lib/json_p3/patch/op_replace.rb', line 9 def initialize(pointer, value) super() @pointer = pointer @value = value 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 |
# File 'lib/json_p3/patch/op_replace.rb', line 19 def apply!(value, index) parent, obj = @pointer.resolve_with_parent(value) return @value if parent == JSONP3::Pointer::UNDEFINED && @pointer.tokens.empty? if parent == JSONP3::Pointer::UNDEFINED raise JSONP3::Patch::Error, "no such property or item '#{@pointer.parent}' (#{name}:#{index})" end target = @pointer.tokens.last if target == JSONP3::Pointer::UNDEFINED raise JSONP3::Patch::Error, "unexpected operation (#{name}:#{index})" end if parent.is_a?(Array) raise JSONP3::Patch::Error, "no item to replace (#{name}:#{index})" if obj == JSONP3::Pointer::UNDEFINED parent[target.to_i] = @value elsif parent.is_a?(Hash) raise JSONP3::Patch::Error, "no property to replace (#{name}:#{index})" if obj == JSONP3::Pointer::UNDEFINED parent[target] = @value else raise JSONP3::Patch::Error, "unexpected operation on #{parent.class} (#{name}:#{index})" end value end |
#name ⇒ Object
15 16 17 |
# File 'lib/json_p3/patch/op_replace.rb', line 15 def name "replace" end |
#to_h ⇒ Object
49 50 51 |
# File 'lib/json_p3/patch/op_replace.rb', line 49 def to_h { "op" => name, "path" => @pointer.to_s, "value" => @value } end |