Class: HashHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/hash_helper.rb

Class Method Summary collapse

Class Method Details

.compare(expected, actual) ⇒ Object

Compare two hashes. Returns a hash with keys :ok, :missing, :unexpected, :changed.



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

def self.compare(expected, actual)
  result = {
      :missing => {},
      :unexpected => {},
      :changed => {},
      :ok => {},
  }
  expected.each_pair do |key_expected, value_expected|
    if actual.include?(key_expected)
      value_actual = actual[key_expected]
      if value_actual == value_expected
        result[:ok][key_expected] = value_expected
      else
        result[:changed][key_expected] = {:expected => value_expected, :actual => value_actual}
      end
    else
      result[:missing][key_expected] = value_expected
    end
  end
  actual.each_pair do |key_actual, value_actual|
    next if expected.include?(key_actual)
    result[:unexpected][key_actual] = value_actual
  end
  result
end