Class: DynportTools::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/dynport_tools/differ.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Differ

Returns a new instance of Differ.



6
7
8
9
# File 'lib/dynport_tools/differ.rb', line 6

def initialize(options = {})
  self.diff_all = options[:diff_all] != false
  self.use_return = options[:use_return] == true
end

Instance Attribute Details

#diff_allObject

Returns the value of attribute diff_all.



3
4
5
# File 'lib/dynport_tools/differ.rb', line 3

def diff_all
  @diff_all
end

#symbolize_keysObject

Returns the value of attribute symbolize_keys.



3
4
5
# File 'lib/dynport_tools/differ.rb', line 3

def symbolize_keys
  @symbolize_keys
end

#use_returnObject

Returns the value of attribute use_return.



3
4
5
# File 'lib/dynport_tools/differ.rb', line 3

def use_return
  @use_return
end

Instance Method Details

#diff(a, b) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/dynport_tools/differ.rb', line 11

def diff(a, b)
  if both?(a, b, Hash)
    diff_hash_values(a, b, a.keys + (self.diff_all ? b.keys : []))
  elsif both?(a, b, Array)
    diff_hash_values(a, b, all_array_indexes(a, b))
  else
    [a, b] if a != b
  end
end

#diff_strings(a, b) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dynport_tools/differ.rb', line 47

def diff_strings(a, b)
  chunks = []
  last = 0
  Diff::LCS.diff(a, b).each do |group|
    old_s = []
    new_s = []
    removed_elements(group).each_with_index do |c, i|
      chunks << a[last..(c.position - 1)] if i == 0
      old_s << c.element 
      last = c.position + 1
    end
    added_elements(group).each_with_index do |c, i|
      if i == 0 && removed_elements(group).empty?
        chunks << a[last..(c.position - 1)]
        last = c.position
      end
      new_s << c.element
    end
    if (old_s.join("").length > 0 || new_s.join("").length > 0)
      chunks << Term::ANSIColor.bold("<#{Term::ANSIColor.red(old_s.join(""))}|#{Term::ANSIColor.green(new_s.join(""))}>")
    end
  end
  chunks.join("")
end

#diff_to_message_lines(the_diff) ⇒ Object



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

def diff_to_message_lines(the_diff)
  diffs = []
  each_diff(the_diff) do |keys, old_value, new_value|
    if keys.nil?
      diffs << "expected #{expected_value(old_value)} to be #{expected_value(new_value)}"
    else
      diffs << "expected #{keys_to_s(keys)} to #{use_return ? "return" : "be"} #{expected_value(old_value)} but #{use_return ? "did" : "was"} #{expected_value(new_value)}"
    end
  end
  diffs
end

#each_diff(the_diff, prefix = nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dynport_tools/differ.rb', line 21

def each_diff(the_diff, prefix = nil, &block)
  if the_diff.is_a?(Array)
    yield(prefix, the_diff.first, the_diff.at(1))
  elsif the_diff.is_a?(Hash)
    the_diff.each do |key, diff|
      if diff.is_a?(Array)
        yield([prefix, key].flatten.compact, diff.first, diff.at(1))
      else
        each_diff(diff, [prefix, key], &block)
      end
    end
  end
end