Class: Kennel::AttributeDiffer

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/attribute_differ.rb

Instance Method Summary collapse

Constructor Details

#initializeAttributeDiffer

Returns a new instance of AttributeDiffer.



7
8
9
10
11
# File 'lib/kennel/attribute_differ.rb', line 7

def initialize
  # min '2' because: -1 makes no sense, 0 does not work with * 2 math, 1 says '1 lines'
  @max_diff_lines = [Integer(ENV.fetch("MAX_DIFF_LINES", "50")), 2].max
  super
end

Instance Method Details

#format(type, field, old, new = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kennel/attribute_differ.rb', line 13

def format(type, field, old, new = nil)
  multiline = false
  if type == "+"
    temp = pretty_inspect(new)
    new = pretty_inspect(old)
    old = temp
  elsif old.is_a?(String) && new.is_a?(String) && (old.include?("\n") || new.include?("\n"))
    multiline = true
  else # ~ and -
    old = pretty_inspect(old)
    new = pretty_inspect(new)
  end

  message =
    if multiline
      "  #{type}#{field}\n" +
        multiline_diff(old, new).map { |l| "    #{l}" }.join("\n")
    elsif (old + new).size > 100
      "  #{type}#{field}\n" \
      "    #{old} ->\n" \
      "    #{new}"
    else
      "  #{type}#{field} #{old} -> #{new}"
    end

  truncate(message)
end