class Pact::Term

Attributes

generate[R]
matcher[R]

Public Class Methods

json_create(obj) click to toggle source
# File lib/pact/term.rb, line 13
def self.json_create(obj)
  new(generate: obj['data']['generate'], matcher: obj['data']['matcher'])
end
new(attributes = {}) click to toggle source
# File lib/pact/term.rb, line 27
def initialize(attributes = {})
  @generate = attributes[:generate]
  @matcher = attributes[:matcher]
  raise Pact::Error.new("Please specify a matcher for the Term") unless @matcher != nil
  raise Pact::Error.new("Please specify a value to generate for the Term") unless @generate != nil
  raise Pact::Error.new("Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect}") unless @generate =~ @matcher
end
unpack_regexps(source) click to toggle source
# File lib/pact/term.rb, line 17
def self.unpack_regexps source
  case source
  when Pact::Term then source.matcher
  when Array then unpack_regexps_from_array source
  when Hash then unpack_regexps_from_hash source
  else
    source
  end
end
unpack_regexps_from_array(source) click to toggle source
# File lib/pact/term.rb, line 74
def self.unpack_regexps_from_array source
  source.each_with_object([]) do | item, destination |
    destination << unpack_regexps(item)
  end
end
unpack_regexps_from_hash(source) click to toggle source
# File lib/pact/term.rb, line 80
def self.unpack_regexps_from_hash source
  source.keys.each_with_object({}) do | key, destination |
    destination[key] = unpack_regexps source[key]
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/pact/term.rb, line 52
def ==(other)
  return false unless other.respond_to?(:generate) && other.respond_to?(:matcher)
  generate == other.generate && matcher == other.matcher
end
as_json(options = {}) click to toggle source
# File lib/pact/term.rb, line 39
def as_json(options = {})
  to_hash
end
diff_with_actual(actual) click to toggle source
# File lib/pact/term.rb, line 61
def diff_with_actual(actual)
  match(actual) ? nil : {
    expected: self,
    actual: actual
  }
end
empty?() click to toggle source
# File lib/pact/term.rb, line 68
def empty?
  false
end
match(literal) click to toggle source
# File lib/pact/term.rb, line 48
def match(literal)
  literal.respond_to?(:to_s) ? matcher.match(literal.to_s) : nil
end
to_hash() click to toggle source
# File lib/pact/term.rb, line 35
def to_hash
  { json_class: self.class.name, data: { generate: generate, matcher: fix_regexp(matcher) } }
end
to_json(options = {}) click to toggle source
# File lib/pact/term.rb, line 44
def to_json(options = {})
  as_json.to_json(options)
end
to_s() click to toggle source
# File lib/pact/term.rb, line 57
def to_s
  "Pact::Term matcher: #{matcher.inspect}" + (generate.nil? ? "" : " generate: \"#{generate}\"")
end