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
Returns the value of attribute diff.
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_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(diff, options = {}) ⇒ ExtractDiffMessages
Returns a new instance of ExtractDiffMessages.
7 8 9 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 7 def initialize diff, = {} @diff = diff end |
Instance Attribute Details
#diff ⇒ Object (readonly)
Returns the value of attribute diff.
5 6 7 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 5 def diff @diff end |
Class Method Details
.call(diff, options = {}) ⇒ Object
11 12 13 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 11 def self.call diff, = {} new(diff, ).call end |
Instance Method Details
#call ⇒ Object
19 20 21 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 19 def call to_s end |
#diff_messages(obj, path = [], messages = []) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 27 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
46 47 48 49 50 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 46 def handle_array array, path, array.each_with_index do | obj, index | obj, path + ["[#{index}]"], end end |
#handle_difference(difference, path, messages) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 52 def handle_difference difference, path, if difference. = "* #{difference.message}" = .gsub("<path>", path_to_s(path)) = .gsub("<parent_path>", parent_path_to_s(path)) << end end |
#handle_hash(hash, path, messages) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 39 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
65 66 67 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 65 def parent_path_to_s path path_to_s(path[0..-2]) end |
#path_to_s(path) ⇒ Object
61 62 63 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 61 def path_to_s path "$" + path.join end |
#to_hash ⇒ Object
15 16 17 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 15 def to_hash diff end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/pact/matchers/extract_diff_messages.rb', line 23 def to_s (diff).join("\n") end |