Class: JSONP3::OpAdd
Overview
The JSON Patch add operation.
Instance Method Summary collapse
- #apply(value, index) ⇒ Object
-
#initialize(pointer, value) ⇒ OpAdd
constructor
A new instance of OpAdd.
- #name ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(pointer, value) ⇒ OpAdd
Returns a new instance of OpAdd.
29 30 31 32 33 |
# File 'lib/json_p3/patch.rb', line 29 def initialize(pointer, value) super() @pointer = pointer @value = value end |
Instance Method Details
#apply(value, index) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/json_p3/patch.rb', line 39 def apply(value, index) parent, obj = @pointer.resolve_with_parent(value) return @value if parent == JSONP3::JSONPointer::UNDEFINED && @pointer.tokens.empty? if parent == JSONP3::JSONPointer::UNDEFINED raise JSONPatchError, "no such property or item '#{@pointer.parent}' (#{name}:#{index})" end target = @pointer.tokens.last if parent.is_a?(Array) if obj == JSONP3::JSONPointer::UNDEFINED raise JSONPatchError, "index out of range (#{name}:#{index})" unless target == "-" parent << @value else parent.insert(target.to_i, @value) end elsif parent.is_a?(Hash) parent[target] = @value else raise JSONPatchError, "unexpected operation on #{parent.class} (#{name}:#{index})" end value end |
#name ⇒ Object
35 36 37 |
# File 'lib/json_p3/patch.rb', line 35 def name "add" end |
#to_h ⇒ Object
66 67 68 |
# File 'lib/json_p3/patch.rb', line 66 def to_h { "op" => name, "path" => @pointer.to_s, "value" => @value } end |