Class: PGExaminer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_examiner/base.rb

Direct Known Subclasses

Result, Result::Item

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/pg_examiner/base.rb', line 70

def ==(other)
  self.class == other.class && diff(other) == {}
end

#diff(other) ⇒ Object



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

#diffable_attrsObject



5
6
7
# File 'lib/pg_examiner/base.rb', line 5

def diffable_attrs
  {}
end

#diffable_listsObject



9
10
11
# File 'lib/pg_examiner/base.rb', line 9

def diffable_lists
  []
end

#diffable_methodsObject



13
14
15
# File 'lib/pg_examiner/base.rb', line 13

def diffable_methods
  []
end