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
- #wrap(object, path) ⇒ Object
Constructor Details
#initialize(expected, matching_rules, root_path) ⇒ Merge
Returns a new instance of Merge.
9 10 11 12 13 |
# File 'lib/pact/matching_rules/merge.rb', line 9 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
5 6 7 |
# File 'lib/pact/matching_rules/merge.rb', line 5 def self.call expected, matching_rules, root_path = '$' new(expected, matching_rules, root_path).call end |
Instance Method Details
#call ⇒ Object
15 16 17 18 |
# File 'lib/pact/matching_rules/merge.rb', line 15 def call return @expected if @matching_rules.nil? || @matching_rules.empty? recurse @expected, @root_path end |
#handle_match_type(object, path, rules) ⇒ Object
59 60 61 62 |
# File 'lib/pact/matching_rules/merge.rb', line 59 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
64 65 66 67 |
# File 'lib/pact/matching_rules/merge.rb', line 64 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/pact/matching_rules/merge.rb', line 69 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
20 21 22 23 24 25 26 27 |
# File 'lib/pact/matching_rules/merge.rb', line 20 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
36 37 38 39 40 41 42 43 |
# File 'lib/pact/matching_rules/merge.rb', line 36 def recurse_array array, path 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 |
#recurse_hash(hash, path) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/pact/matching_rules/merge.rb', line 29 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 |
#wrap(object, path) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pact/matching_rules/merge.rb', line 45 def wrap object, path rules = @matching_rules[path] return object unless rules if rules['match'] == 'type' handle_match_type(object, path, rules) elsif rules['regex'] handle_regex(object, path, rules) else log_ignored_rules(path, rules, {}) object end end |