Class: Draisine::HashDiff

Inherits:
Struct
  • Object
show all
Defined in:
lib/draisine/util/hash_diff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#addedObject

Returns the value of attribute added

Returns:

  • (Object)

    the current value of added



2
3
4
# File 'lib/draisine/util/hash_diff.rb', line 2

def added
  @added
end

#changedObject

Returns the value of attribute changed

Returns:

  • (Object)

    the current value of changed



2
3
4
# File 'lib/draisine/util/hash_diff.rb', line 2

def changed
  @changed
end

#removedObject

Returns the value of attribute removed

Returns:

  • (Object)

    the current value of removed



2
3
4
# File 'lib/draisine/util/hash_diff.rb', line 2

def removed
  @removed
end

#unchangedObject

Returns the value of attribute unchanged

Returns:

  • (Object)

    the current value of unchanged



2
3
4
# File 'lib/draisine/util/hash_diff.rb', line 2

def unchanged
  @unchanged
end

Class Method Details

.diff(hash1, hash2, equality = -> (a, b) { a == b }) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/draisine/util/hash_diff.rb', line 3

def self.diff(hash1, hash2, equality = -> (a, b) { a == b })
  unless hash1.respond_to?(:key?) && hash2.respond_to?(:key?)
    fail ArgumentError, "both arguments should be hashes"
  end

  added = []
  removed = []
  changed = []
  unchanged = []

  (hash1.keys | hash2.keys).each do |key|
    if hash1.key?(key) && hash2.key?(key)
      if equality.call(hash1[key], hash2[key])
        unchanged << key
      else
        changed << key
      end
    elsif hash1.key?(key)
      removed << key
    else
      added << key
    end
  end

  new(added, removed, changed, unchanged)
end

.sf_diff(hash1, hash2) ⇒ Object



30
31
32
# File 'lib/draisine/util/hash_diff.rb', line 30

def self.sf_diff(hash1, hash2)
  diff(hash1, hash2, SalesforceComparisons.method(:salesforce_equals?))
end

Instance Method Details

#diff_keysObject



34
35
36
# File 'lib/draisine/util/hash_diff.rb', line 34

def diff_keys
  changed | added | removed
end