Class: Pact::Term

Inherits:
Object
  • Object
show all
Includes:
ActiveSupportSupport
Defined in:
lib/pact/term.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupportSupport

#fix_all_the_things, #fix_json_formatting, #fix_regexp, #remove_unicode, #warn_about_regexp

Constructor Details

#initialize(attributes = {}) ⇒ Term

Returns a new instance of Term.

Raises:



27
28
29
30
31
32
33
34
35
36
37
# 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
  if @generate.is_a?(Float) || @generate.is_a?(Integer)
    raise Pact::Error.new("#{@generate.is_a?(Float) ? "Float" : "Integer"} Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect} when converted to string") unless @generate.to_s =~ @matcher
  else
    raise Pact::Error.new("Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect}") unless @generate =~ @matcher
  end
end

Instance Attribute Details

#generateObject (readonly)

Returns the value of attribute generate.



11
12
13
# File 'lib/pact/term.rb', line 11

def generate
  @generate
end

#matcherObject (readonly)

Returns the value of attribute matcher.



11
12
13
# File 'lib/pact/term.rb', line 11

def matcher
  @matcher
end

Class Method Details

.json_create(obj) ⇒ Object



13
14
15
# File 'lib/pact/term.rb', line 13

def self.json_create(obj)
  new(generate: obj['data']['generate'], matcher: obj['data']['matcher'])
end

.unpack_regexps(source) ⇒ Object



17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#==(other) ⇒ Object



56
57
58
59
# File 'lib/pact/term.rb', line 56

def ==(other)
  return false unless other.respond_to?(:generate) && other.respond_to?(:matcher)
  generate == other.generate && matcher == other.matcher
end

#as_json(options = {}) ⇒ Object



43
44
45
# File 'lib/pact/term.rb', line 43

def as_json(options = {})
  to_hash
end

#diff_with_actual(actual) ⇒ Object



65
66
67
68
69
70
# File 'lib/pact/term.rb', line 65

def diff_with_actual(actual)
  match(actual) ? nil : {
    expected: self,
    actual: actual
  }
end

#empty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/pact/term.rb', line 72

def empty?
  false
end

#match(literal) ⇒ Object



52
53
54
# File 'lib/pact/term.rb', line 52

def match(literal)
  literal.respond_to?(:to_s) ? matcher.match(literal.to_s) : nil
end

#to_hashObject



39
40
41
# File 'lib/pact/term.rb', line 39

def to_hash
  { json_class: self.class.name, data: { generate: generate, matcher: fix_regexp(matcher) } }
end

#to_json(options = {}) ⇒ Object



48
49
50
# File 'lib/pact/term.rb', line 48

def to_json(options = {})
  as_json.to_json(options)
end

#to_sObject



61
62
63
# File 'lib/pact/term.rb', line 61

def to_s
  "Pact::Term matcher: #{matcher.inspect}" + (generate.nil? ? "" : " generate: \"#{generate}\"")
end