Module: Droonga::Differ

Defined in:
lib/droonga/differ.rb

Class Method Summary collapse

Class Method Details

.diff(a, b) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/droonga/differ.rb', line 19

def diff(a, b)
  unless a.class == b.class
    return "#{a.inspect} <=> #{b.inspect}"
  end
  case a
  when Hash
    diff_hashes(a, b)
  when Array
    diff_arrays(a, b)
  else
    if a == b
      nil
    else
      "#{a.inspect} <=> #{b.inspect}"
    end
  end
end

.diff_arrays(a, b) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/droonga/differ.rb', line 47

def diff_arrays(a, b)
  difference = {}
  [a.size, b.size].max.times do |index|
    unless a[index] == b[index]
      difference[index] = diff(a[index], b[index])
    end
  end
  difference
end

.diff_hashes(a, b) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/droonga/differ.rb', line 37

def diff_hashes(a, b)
  difference = {}
  (a.keys + b.keys).uniq.each do |key|
    unless a[key] == b[key]
      difference[key] = diff(a[key], b[key])
    end
  end
  difference
end