Method: HashDiff.unpatch!

Defined in:
lib/hashdiff/patch.rb

.unpatch!(obj, changes) ⇒ Object

Unpatch an object

Parameters:

  • obj (Hash, Array)

    the object to be unpatchted, can be an Array of a Hash

  • changes (Array)

    e.g. [[ ‘+’, ‘a.b’, ‘45’ ], [ ‘-’, ‘a.c’, ‘5’ ], [ ‘~’, ‘a.x’, ‘45’, ‘63’]]

Returns:

  • the object after unpatch

Since:

  • 0.0.1



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hashdiff/patch.rb', line 49

def self.unpatch!(obj, changes)
  changes.reverse_each do |change|
    parts = decode_property_path(change[1])
    last_part = parts.last

    parent_node = node(obj, parts[0, parts.size-1])

    if change[0] == '+'
      if last_part.is_a?(Fixnum)
        parent_node.delete_at(last_part)
      else
        parent_node.delete(last_part)
      end
    elsif 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] == '~'
      parent_node[last_part] = change[2]
    end
  end

  obj
end