Class: Comparator

Inherits:
Object show all
Defined in:
lib/comparator.rb

Class Method Summary collapse

Class Method Details

.array_diff(obj1, obj2) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/comparator.rb', line 37

def self.array_diff(obj1, obj2)
  {}.tap do |ret|
    # handle VariableArray by extracting the first element
    # of non-VariableArrays and the 2nd element of VariableArrays
    if obj1.is_a?(VariableArray) || obj2.is_a?(VariableArray)
      obj1 = obj1.is_a?(VariableArray) ? [obj1[1]] : [obj1[0]]
      obj2 = obj2.is_a?(VariableArray) ? [obj2[1]] : [obj2[0]]
    end
    bigger_arr = obj1.size >= obj2.size ? obj1 : obj2
    bigger_arr.each_index do |k|
      ret[k] = self.diff(obj1[k], obj2[k]) unless obj1[k] == obj2[k]
    end   
  end
end

.diff(obj1, obj2) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/comparator.rb', line 25

def self.diff(obj1, obj2)
  return {} if obj1 == obj2
  if obj1.is_a?(Array) && obj2.is_a?(Array)
    return self.array_diff(obj1, obj2) 
  elsif obj1.is_a?(Hash) && obj2.is_a?(Hash)
    return self.hash_diff(obj1, obj2) 
  end
  return [formatted_value(obj1), "KEY MISSING"] if obj2.nil?
  return ["KEY MISSING", formatted_value(obj2)] if obj1.nil?
  return [formatted_value(obj1), formatted_value(obj2)]
end

.formatted_value(val) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/comparator.rb', line 5

def self.formatted_value(val)
  if val.nil?
    return "NilClass::nil"
  elsif val.is_a?(PathString)
    return val.gsub(":","")
  elsif val == ""
    return "''"
  else
    return val
  end
end

.hash_diff(obj1, obj2) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/comparator.rb', line 52

def self.hash_diff(obj1, obj2)
  obj1, obj2 = self.stringify_keys(obj1), self.stringify_keys(obj2)
  (obj1.keys + obj2.keys).uniq.inject({}) do |memo, key|
    if !obj1.keys.include?(key) 
      memo[key] = ["KEY MISSING", formatted_value(obj2[key])]
    elsif !obj2.keys.include?(key) 
      memo[key] = [formatted_value(obj1[key]), "KEY MISSING"]
    elsif obj1[key] != obj2[key]
      memo[key] = self.diff(obj1[key], obj2[key])
    end
    memo
  end
end

.normalize_value(val) ⇒ Object


17
18
19
20
21
22
23
# File 'lib/comparator.rb', line 17

def self.normalize_value(val)
  return val if val.is_a?(TimeDateMatcher)
  val = ActiveSupport::JSON.decode(val) if val.is_a?(String)
  val = self.stringify_keys(val) if val.is_a?(Hash)
  val = val.collect{|v| v.is_a?(Hash) || v.is_a?(Array) ? self.normalize_value(v) : v} if val.is_a?(Array)
  val
end

.stringify_keys(hash_or_array) ⇒ Object


66
67
68
69
70
71
72
73
# File 'lib/comparator.rb', line 66

def self.stringify_keys(hash_or_array)
  return hash_or_array.collect{|v| v.is_a?(Hash) || v.is_a?(Array) ? self.stringify_keys(v) : v} if hash_or_array.is_a?(Array)
  {}.tap do |ret|
    hash_or_array.each_pair.each do |k, v|
      ret[k.to_s] = v.is_a?(Hash) || v.is_a?(Array) ? self.stringify_keys(v) : v
    end
  end
end