class Pact::MatchingRules::V3::Extract

Attributes

matchable[R]
rules[R]

Public Class Methods

call(matchable) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 9
def self.call matchable
  new(matchable).call
end
new(matchable) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 13
def initialize matchable
  @matchable = matchable
  @rules = Hash.new
end

Public Instance Methods

call() click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 18
def call
  recurse matchable, "$", nil
  rules
end

Private Instance Methods

handle_array_like(array_like, path, match_type) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 56
def handle_array_like array_like, path, match_type
  record_rule "#{path}", 'min' => array_like.min
  record_match_type_rule "#{path}[*].*", 'type'
  recurse array_like.contents, "#{path}[*]", :array_like
end
handle_something_like(something_like, path, match_type) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 51
def handle_something_like something_like, path, match_type
  record_match_type_rule path, "type"
  recurse something_like.contents, path, "type"
end
next_path_part(key) click to toggle source

Beth: there’s a potential bug if the key contains a dot and a single quote. Not sure what to do then.

# File lib/pact/matching_rules/v3/extract.rb, line 85
def next_path_part key
  if key.to_s.include?('.')
    "['#{key}']"
  else
    ".#{key}"
  end
end
record_match_type_rule(path, match_type) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 75
def record_match_type_rule path, match_type
  unless match_type == :array_like || match_type.nil?
    rules[path] ||= {}
    rules[path]['matchers'] ||= []
    rules[path]['matchers'] << { 'match' => match_type }
  end
end
record_regex_rule(term, path) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 68
def record_regex_rule term, path
  rules[path] ||= {}
  rules[path]['matchers'] ||= []
  rule = { 'match' => 'regex', 'regex' => term.matcher.inspect[1..-2]}
  rules[path]['matchers'] << rule
end
record_rule(path, rule) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 62
def record_rule path, rule
  rules[path] ||= {}
  rules[path]['matchers'] ||= []
  rules[path]['matchers'] << rule
end
recurse(object, path, match_type) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 27
def recurse object, path, match_type
  case object
  when Hash then recurse_hash(object, path, match_type)
  when Array then recurse_array(object, path, match_type)
  when Pact::SomethingLike then handle_something_like(object, path, match_type)
  when Pact::ArrayLike then handle_array_like(object, path, match_type)
  when Pact::Term then record_regex_rule object, path
  when Pact::QueryString then recurse(object.query, path, match_type)
  when Pact::QueryHash then recurse_hash(object.query, path, match_type)
  end
end
recurse_array(new_array, path, match_type) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 45
def recurse_array new_array, path, match_type
  new_array.each_with_index do | value, index |
    recurse value, "#{path}[#{index}]", match_type
  end
end
recurse_hash(hash, path, match_type) click to toggle source
# File lib/pact/matching_rules/v3/extract.rb, line 39
def recurse_hash hash, path, match_type
  hash.each do | (key, value) |
    recurse value, "#{path}#{next_path_part(key)}", match_type
  end
end