Class: Pact::Matchers::ExtractDiffMessages
- Inherits:
-
Object
- Object
- Pact::Matchers::ExtractDiffMessages
- Defined in:
- lib/pact/matchers/extract_diff_messages.rb
Instance Attribute Summary collapse
- #diff ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #diff_messages(obj, path = [], messages = []) ⇒ Object
- #handle_array(array, path, messages) ⇒ Object
- #handle_difference(difference, path, messages) ⇒ Object
- #handle_hash(hash, path, messages) ⇒ Object
-
#initialize(diff, options = {}) ⇒ ExtractDiffMessages
constructor
A new instance of ExtractDiffMessages.
- #parent_path_to_s(path) ⇒ Object
- #path_to_s(path) ⇒ Object
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
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, = {} @diff = diff end |
Instance Attribute Details
#diff ⇒ Object (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, = {} new(diff, ).call end |
Instance Method Details
#call ⇒ Object
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 obj, path = [], = [] case obj when Hash then handle_hash obj, path, when Array then handle_array obj, path, when BaseDifference then handle_difference obj, path, when NoDiffAtIndex then nil else raise "Invalid diff, expected Hash, Array, NoDiffAtIndex or BaseDifference, found #{obj.class}" end 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, array.each_with_index do | obj, index | obj, path + ["[#{index}]"], 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, if difference. = difference. = .gsub("<path>", path_to_s(path)) = .gsub("<parent_path>", parent_path_to_s(path)) << 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, hash.each_pair do | key, value | next_part = key =~ /\s/ ? key.inspect : key value, path + [".#{next_part}"], 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_a ⇒ Object
28 29 30 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 28 def to_a (diff) end |
#to_hash ⇒ Object
16 17 18 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 16 def to_hash to_a end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 24 def to_s (diff).join("\n") end |