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_regexp

Constructor Details

#initialize(attributes = {}) ⇒ Term

Returns a new instance of Term.



15
16
17
18
19
20
21
# File 'lib/pact/term.rb', line 15

def initialize(attributes = {})
  @generate = attributes[:generate]
  @matcher = attributes[:matcher]
  raise "Please specify a matcher for the Term" unless @matcher != nil
  raise "Please specify a value to generate for the Term" unless @generate != nil
  raise "Value to generate \"#{@generate}\" does not match regular expression #{@matcher}" unless @generate =~ @matcher
end

Instance Attribute Details

#generateObject (readonly)

Returns the value of attribute generate.



9
10
11
# File 'lib/pact/term.rb', line 9

def generate
  @generate
end

#matcherObject (readonly)

Returns the value of attribute matcher.



9
10
11
# File 'lib/pact/term.rb', line 9

def matcher
  @matcher
end

Class Method Details

.json_create(obj) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#as_json(options = {}) ⇒ Object



27
28
29
# File 'lib/pact/term.rb', line 27

def as_json(options = {})
  to_hash
end

#diff_with_actual(actual) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  false
end

#match(literal) ⇒ Object



36
37
38
# File 'lib/pact/term.rb', line 36

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

#to_hashObject



23
24
25
# File 'lib/pact/term.rb', line 23

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

#to_json(options = {}) ⇒ Object



32
33
34
# File 'lib/pact/term.rb', line 32

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

#to_sObject



45
46
47
# File 'lib/pact/term.rb', line 45

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