Class: Pact::Matchers::ExtractDiffMessages

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/matchers/extract_diff_messages.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff, options = {}) ⇒ ExtractDiffMessages

Returns a new instance of ExtractDiffMessages.



8
9
10
# File 'lib/pact/matchers/extract_diff_messages.rb', line 8

def initialize diff, options = {}
  @diff = diff
end

Instance Attribute Details

#diffObject (readonly)



6
7
8
# File 'lib/pact/matchers/extract_diff_messages.rb', line 6

def diff
  @diff
end

Class Method Details

.call(diff, options = {}) ⇒ Object



12
13
14
# File 'lib/pact/matchers/extract_diff_messages.rb', line 12

def self.call diff, options = {}
  new(diff, options).call
end

Instance Method Details

#callObject



20
21
22
# File 'lib/pact/matchers/extract_diff_messages.rb', line 20

def call
  to_a
end

#diff_messages(obj, path = [], messages = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pact/matchers/extract_diff_messages.rb', line 32

def diff_messages obj, path = [], messages = []
  case obj
  when Hash then handle_hash obj, path, messages
  when Array then handle_array obj, path, messages
  when BaseDifference then handle_difference obj, path, messages
  when NoDiffAtIndex then nil
  else
    raise "Invalid diff, expected Hash, Array, NoDiffAtIndex or BaseDifference, found #{obj.class}"
  end
  messages
end

#handle_array(array, path, messages) ⇒ Object



51
52
53
54
55
# File 'lib/pact/matchers/extract_diff_messages.rb', line 51

def handle_array array, path, messages
  array.each_with_index do | obj, index |
    diff_messages obj, path + ["[#{index}]"], messages
  end
end

#handle_difference(difference, path, messages) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/pact/matchers/extract_diff_messages.rb', line 57

def handle_difference difference, path, messages
  if difference.message
    message = difference.message
    message = message.gsub("<path>", path_to_s(path))
    message = message.gsub("<parent_path>", parent_path_to_s(path))
    messages << message
  end
end

#handle_hash(hash, path, messages) ⇒ Object



44
45
46
47
48
49
# File 'lib/pact/matchers/extract_diff_messages.rb', line 44

def handle_hash hash, path, messages
  hash.each_pair do | key, value |
    next_part = key =~ /\s/ ? key.inspect : key
    diff_messages value, path + [".#{next_part}"], messages
  end
end

#parent_path_to_s(path) ⇒ Object



70
71
72
# File 'lib/pact/matchers/extract_diff_messages.rb', line 70

def parent_path_to_s path
  path_to_s(path[0..-2])
end

#path_to_s(path) ⇒ Object



66
67
68
# File 'lib/pact/matchers/extract_diff_messages.rb', line 66

def path_to_s path
  "$" + path.join
end

#to_aObject



28
29
30
# File 'lib/pact/matchers/extract_diff_messages.rb', line 28

def to_a
  diff_messages(diff)
end

#to_hashObject



16
17
18
# File 'lib/pact/matchers/extract_diff_messages.rb', line 16

def to_hash
  to_a
end

#to_sObject



24
25
26
# File 'lib/pact/matchers/extract_diff_messages.rb', line 24

def to_s
  diff_messages(diff).join("\n")
end