Module: Mongoid::Extensions::Hash::Conversions::InstanceMethods

Defined in:
lib/mongoid/extensions/hash/conversions.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#difference(other) ⇒ Object

Get the difference between 2 hashes. This will give back a new hash with the keys and pairs of [ old, new ] values.

Example:

first = { :field => "value" }
second = { :field => "new" }
first.difference(second) # => { :field => [ "value", "new" ] }

Returns:

A Hash of modifications.



22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/extensions/hash/conversions.rb', line 22

def difference(other)
  changes = {}
  each_pair do |key, value|
    if other.has_key?(key)
      new_value = other[key]
      changes[key] = [ value, new_value ] if new_value != value
    end
  end
  changes
end