Method: HashDiff.patch!
- Defined in:
- lib/hashdiff/patch.rb
.patch!(obj, changes, delimiter = '.') ⇒ Object
Apply patch to object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hashdiff/patch.rb', line 15 def self.patch!(obj, changes, delimiter='.') changes.each do |change| parts = decode_property_path(change[1], delimiter) last_part = parts.last parent_node = node(obj, parts[0, parts.size-1]) if change[0] == '+' if last_part.is_a?(Fixnum) parent_node.insert(last_part, change[2]) else parent_node[last_part] = change[2] end elsif change[0] == '-' if last_part.is_a?(Fixnum) parent_node.delete_at(last_part) else parent_node.delete(last_part) end elsif change[0] == '~' parent_node[last_part] = change[3] end end obj end |