Class: Pact::MatchingRules::Merge
- Inherits:
-
Object
- Object
- Pact::MatchingRules::Merge
- Defined in:
- lib/pact/matching_rules/merge.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #handle_match_type(object, path, rules) ⇒ Object
- #handle_regex(object, path, rules) ⇒ Object
-
#initialize(expected, matching_rules, root_path) ⇒ Merge
constructor
A new instance of Merge.
- #log_ignored_rules(path, rules, used_rules) ⇒ Object
- #recurse(expected, path) ⇒ Object
- #recurse_array(array, path) ⇒ Object
- #recurse_hash(hash, path) ⇒ Object
- #warn_when_not_one_example_item(array, path) ⇒ Object
-
#wrap(object, path) ⇒ Object
def handle_array_like.
Constructor Details
#initialize(expected, matching_rules, root_path) ⇒ Merge
Returns a new instance of Merge.
11 12 13 14 15 |
# File 'lib/pact/matching_rules/merge.rb', line 11 def initialize expected, matching_rules, root_path @expected = expected @matching_rules = matching_rules @root_path = root_path end |
Class Method Details
.call(expected, matching_rules, root_path = '$') ⇒ Object
7 8 9 |
# File 'lib/pact/matching_rules/merge.rb', line 7 def self.call expected, matching_rules, root_path = '$' new(expected, matching_rules, root_path).call end |
Instance Method Details
#call ⇒ Object
17 18 19 20 |
# File 'lib/pact/matching_rules/merge.rb', line 17 def call return @expected if @matching_rules.nil? || @matching_rules.empty? recurse @expected, @root_path end |
#handle_match_type(object, path, rules) ⇒ Object
82 83 84 85 |
# File 'lib/pact/matching_rules/merge.rb', line 82 def handle_match_type object, path, rules log_ignored_rules(path, rules, {'match' => 'type'}) Pact::SomethingLike.new(object) end |
#handle_regex(object, path, rules) ⇒ Object
87 88 89 90 |
# File 'lib/pact/matching_rules/merge.rb', line 87 def handle_regex object, path, rules log_ignored_rules(path, rules, {'match' => 'regex', 'regex' => rules['regex']}) Pact::Term.new(generate: object, matcher: Regexp.new(rules['regex'])) end |
#log_ignored_rules(path, rules, used_rules) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/pact/matching_rules/merge.rb', line 92 def log_ignored_rules path, rules, used_rules dup_rules = rules.dup used_rules.each_pair do | used_key, used_value | dup_rules.delete(used_key) if dup_rules[used_key] == used_value end if dup_rules.any? $stderr.puts "WARN: Ignoring unsupported matching rules #{dup_rules} for path #{path}" end end |
#recurse(expected, path) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/pact/matching_rules/merge.rb', line 22 def recurse expected, path case expected when Hash then recurse_hash(expected, path) when Array then recurse_array(expected, path) else expected end end |
#recurse_array(array, path) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pact/matching_rules/merge.rb', line 38 def recurse_array array, path array_like_path = "#{path}[*].*" array_match_type = @matching_rules[array_like_path] && @matching_rules[array_like_path]['match'] if array_match_type == 'type' warn_when_not_one_example_item(array, path) min = @matching_rules[path]['min'] # log_ignored_rules(path, @matching_rules[path], {'min' => min}) Pact::ArrayLike.new(recurse(array.first, "#{path}[*]"), min: min) else new_array = [] array.each_with_index do | item, index | new_path = path + "[#{index}]" new_array << recurse(wrap(item, new_path), new_path) end new_array end end |
#recurse_hash(hash, path) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/pact/matching_rules/merge.rb', line 31 def recurse_hash hash, path hash.each_with_object({}) do | (k, v), new_hash | new_path = path + ".#{k.to_s}" new_hash[k] = recurse(wrap(v, new_path), new_path) end end |
#warn_when_not_one_example_item(array, path) ⇒ Object
57 58 59 60 61 |
# File 'lib/pact/matching_rules/merge.rb', line 57 def warn_when_not_one_example_item array, path unless array.size == 1 Pact.configuration.error_stream.puts "WARN: Only the first item will be used to match the items in the array at #{path}" end end |
#wrap(object, path) ⇒ Object
def handle_array_like
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pact/matching_rules/merge.rb', line 65 def wrap object, path rules = @matching_rules[path] array_rules = @matching_rules["#{path}[*].*"] return object unless rules || array_rules if rules['match'] == 'type' handle_match_type(object, path, rules) elsif rules['regex'] handle_regex(object, path, rules) # elsif array_rules['match'] == 'type' # handle_array_like(object, path, rules) else log_ignored_rules(path, rules, {}) object end end |