17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/pg_examiner/base.rb', line 17
def diff(other)
raise "Can't diff a #{self.class} and a #{other.class}" unless self.class == other.class
r = {}
diffable_attrs.each do |attr, description|
this = @row.fetch(attr)
that = other.row.fetch(attr)
unless this == that
r[description] = {this => that}
end
end
diffable_methods.each do |attr, description|
this = send(attr)
that = other.send(attr)
unless this == that
r[description] = {this => that}
end
end
diffable_lists.each do |attr, description|
these = send(attr)
those = other.send(attr)
these_names = these.map(&:name)
those_names = those.map(&:name)
if these_names == those_names
result = these.zip(those).each_with_object({}) do |(this, that), hash|
if (result = this.diff(that)).any?
hash[this.name] = result
end
end
if result.any?
r[description] = result
end
else
added = those_names - these_names
removed = these_names - those_names
h = {}
h['added'] = added.sort if added.any?
h['removed'] = removed.sort if removed.any?
r[description] = h
end
end
r
end
|