Class: JSONP3::Patch
- Inherits:
-
Object
show all
- Defined in:
- lib/json_p3/patch.rb,
lib/json_p3/errors.rb,
lib/json_p3/patch/op.rb,
lib/json_p3/patch/op_add.rb,
lib/json_p3/patch/op_copy.rb,
lib/json_p3/patch/op_move.rb,
lib/json_p3/patch/op_test.rb,
lib/json_p3/patch/op_remove.rb,
lib/json_p3/patch/op_replace.rb
Overview
A JSON Patch containing zero or more patch operations.
Defined Under Namespace
Classes: Error, Op, OpAdd, OpCopy, OpMove, OpRemove, OpReplace, OpTest, TestFailure
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(ops = nil) ⇒ Patch
Returns a new instance of Patch.
13
14
15
16
|
# File 'lib/json_p3/patch.rb', line 13
def initialize(ops = nil)
@ops = []
build(ops) unless ops.nil?
end
|
Class Method Details
.apply!(ops, value) ⇒ Object
8
9
10
|
# File 'lib/json_p3/patch.rb', line 8
def self.apply!(ops, value)
new(ops).apply!(value)
end
|
Instance Method Details
#add(pointer, value) ⇒ self
21
22
23
24
|
# File 'lib/json_p3/patch.rb', line 21
def add(pointer, value)
@ops.push(OpAdd.new(ensure_pointer(pointer, :add, @ops.length), value))
self
end
|
#apply!(value) ⇒ Object
Apply this patch to JSON-like value value.
72
73
74
75
|
# File 'lib/json_p3/patch.rb', line 72
def apply!(value)
@ops.each_with_index { |op, i| value = op.apply!(value, i) }
value
end
|
#copy(from, pointer) ⇒ self
55
56
57
58
59
60
61
|
# File 'lib/json_p3/patch.rb', line 55
def copy(from, pointer)
@ops.push(OpCopy.new(
ensure_pointer(from, :copy, @ops.length),
ensure_pointer(pointer, :copy, @ops.length)
))
self
end
|
#move(from, pointer) ⇒ self
44
45
46
47
48
49
50
|
# File 'lib/json_p3/patch.rb', line 44
def move(from, pointer)
@ops.push(OpMove.new(
ensure_pointer(from, :move, @ops.length),
ensure_pointer(pointer, :move, @ops.length)
))
self
end
|
#remove(pointer) ⇒ self
28
29
30
31
|
# File 'lib/json_p3/patch.rb', line 28
def remove(pointer)
@ops.push(OpRemove.new(ensure_pointer(pointer, :remove, @ops.length)))
self
end
|
#replace(pointer, value) ⇒ self
36
37
38
39
|
# File 'lib/json_p3/patch.rb', line 36
def replace(pointer, value)
@ops.push(OpReplace.new(ensure_pointer(pointer, :replace, @ops.length), value))
self
end
|
#test(pointer, value) ⇒ self
66
67
68
69
|
# File 'lib/json_p3/patch.rb', line 66
def test(pointer, value)
@ops.push(OpTest.new(ensure_pointer(pointer, :test, @ops.length), value))
self
end
|
#to_a ⇒ Object
77
78
79
|
# File 'lib/json_p3/patch.rb', line 77
def to_a
@ops.map(&:to_h)
end
|