136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/json_p3/patch.rb', line 136
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 target == JSONP3::JSONPointer::UNDEFINED
raise JSONPatchError,
"unexpected operation (#{name}:#{index})"
end
if parent.is_a?(Array)
raise JSONPatchError, "no item to replace (#{name}:#{index})" if obj == JSONP3::JSONPointer::UNDEFINED
parent[target.to_i] = @value
elsif parent.is_a?(Hash)
raise JSONPatchError, "no property to replace (#{name}:#{index})" if obj == JSONP3::JSONPointer::UNDEFINED
parent[target] = @value
else
raise JSONPatchError, "unexpected operation on #{parent.class} (#{name}:#{index})"
end
value
end
|