class PGExaminer::Base

Constants

ARRAY_REGEX

Public Instance Methods

==(other) click to toggle source
# File lib/pg_examiner/base.rb, line 70
def ==(other)
  self.class == other.class && diff(other) == {}
end
diff(other) click to toggle source
# 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_attrs() click to toggle source
# File lib/pg_examiner/base.rb, line 5
def diffable_attrs
  {}
end
diffable_lists() click to toggle source
# File lib/pg_examiner/base.rb, line 9
def diffable_lists
  []
end
diffable_methods() click to toggle source
# File lib/pg_examiner/base.rb, line 13
def diffable_methods
  []
end

Private Instance Methods

extract_array(value) click to toggle source
# File lib/pg_examiner/base.rb, line 77
def extract_array(value)
  without_brackets = value[ARRAY_REGEX, 1]
  raise "Invalid array value: #{value.inspect}" unless without_brackets
  without_brackets.split(',')
end