Module: ObjectPatch::Operations
- Defined in:
- lib/object_patch/operations.rb,
lib/object_patch/operations/add.rb,
lib/object_patch/operations/copy.rb,
lib/object_patch/operations/move.rb,
lib/object_patch/operations/test.rb,
lib/object_patch/operations/remove.rb,
lib/object_patch/operations/replace.rb
Overview
These operations take advantage of the fact that Pointer#eval returns the same object (obj.object_id match) and thus any changes made to the extracted object will be reflected in the original deeply nested object.
Defined Under Namespace
Classes: Add, Copy, Move, Remove, Replace, Test
Class Method Summary collapse
-
.add_op(target_obj, key, new_value) ⇒ Object
Add a value at the provided key within the provided object.
-
.check_array_index(index, array_size) ⇒ Fixnum
Validates that the array index provided falls within the acceptable range or in the event we have received the special ‘-’ index defined in the JSON Pointer RFC we treat it as the last element.
-
.rm_op(target_obj, key) ⇒ Object
Remove a hash key or index from the provided object.
Instance Method Summary collapse
-
#add_op(target_obj, key, new_value) ⇒ Object
private
Add a value at the provided key within the provided object.
-
#check_array_index(index, array_size) ⇒ Fixnum
private
Validates that the array index provided falls within the acceptable range or in the event we have received the special ‘-’ index defined in the JSON Pointer RFC we treat it as the last element.
-
#rm_op(target_obj, key) ⇒ Object
private
Remove a hash key or index from the provided object.
Class Method Details
.add_op(target_obj, key, new_value) ⇒ Object
Add a value at the provided key within the provided object. This will behave differently depending on whether we’re processing a hash or an array as the target destination.
It is important to note that this behaves by adjusting the state of the provided object. It does not return the new object itself!
28 29 30 31 32 33 34 |
# File 'lib/object_patch/operations.rb', line 28 def add_op(target_obj, key, new_value) if target_obj.is_a?(Array) target_obj.insert(check_array_index(key, target_obj.size), new_value) else target_obj[key] = new_value end end |
.check_array_index(index, array_size) ⇒ Fixnum
Validates that the array index provided falls within the acceptable range or in the event we have received the special ‘-’ index defined in the JSON Pointer RFC we treat it as the last element.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/object_patch/operations.rb', line 44 def check_array_index(index, array_size) return -1 if index == "-" raise ObjectOperationOnArrayException unless index =~ /\A-?\d+\Z/ index = index.to_i # There is a bug in the IETF tests that require us to allow patches to # set a value at the end of the array. The final '<=' should actually be # a '<'. raise OutOfBoundsException unless (0 <= index && index <= array_size) index end |
.rm_op(target_obj, key) ⇒ Object
Remove a hash key or index from the provided object.
It is important to note that this behaves by adjusting the state of the provided object. It does not return the new object itself!
66 67 68 69 70 71 72 73 74 |
# File 'lib/object_patch/operations.rb', line 66 def rm_op(target_obj, key) if target_obj.is_a?(Array) raise InvalidIndexError unless key =~ /\A\d+\Z/ target_obj.delete_at(check_array_index(key, target_obj.size)) else raise(MissingTargetException, key) unless target_obj.has_key?(key) target_obj.delete(key) end end |
Instance Method Details
#add_op(target_obj, key, new_value) ⇒ Object (private)
Add a value at the provided key within the provided object. This will behave differently depending on whether we’re processing a hash or an array as the target destination.
It is important to note that this behaves by adjusting the state of the provided object. It does not return the new object itself!
28 29 30 31 32 33 34 |
# File 'lib/object_patch/operations.rb', line 28 def add_op(target_obj, key, new_value) if target_obj.is_a?(Array) target_obj.insert(check_array_index(key, target_obj.size), new_value) else target_obj[key] = new_value end end |
#check_array_index(index, array_size) ⇒ Fixnum (private)
Validates that the array index provided falls within the acceptable range or in the event we have received the special ‘-’ index defined in the JSON Pointer RFC we treat it as the last element.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/object_patch/operations.rb', line 44 def check_array_index(index, array_size) return -1 if index == "-" raise ObjectOperationOnArrayException unless index =~ /\A-?\d+\Z/ index = index.to_i # There is a bug in the IETF tests that require us to allow patches to # set a value at the end of the array. The final '<=' should actually be # a '<'. raise OutOfBoundsException unless (0 <= index && index <= array_size) index end |
#rm_op(target_obj, key) ⇒ Object (private)
Remove a hash key or index from the provided object.
It is important to note that this behaves by adjusting the state of the provided object. It does not return the new object itself!
66 67 68 69 70 71 72 73 74 |
# File 'lib/object_patch/operations.rb', line 66 def rm_op(target_obj, key) if target_obj.is_a?(Array) raise InvalidIndexError unless key =~ /\A\d+\Z/ target_obj.delete_at(check_array_index(key, target_obj.size)) else raise(MissingTargetException, key) unless target_obj.has_key?(key) target_obj.delete(key) end end |