Class: Pact::Generators

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/generators.rb

Class Method Summary collapse

Class Method Details

.add_generator(generator) ⇒ Object



18
19
20
# File 'lib/pact/generators.rb', line 18

def self.add_generator(generator)
  generators.unshift(generator)
end

.apply_generators(expected_request, component, example_value, state_params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pact/generators.rb', line 34

def self.apply_generators(expected_request, component, example_value, state_params)
  # Latest pact-support is required to have generators exposed
  if expected_request.methods.include?(:generators) && expected_request.generators[component]
    # Some component will have single generator without selectors, i.e. path
    generators = expected_request.generators[component]
    if generators.is_a?(Hash) && generators.key?('type')
      return execute_generators(generators, state_params, example_value)
    end

    generators.each do |selector, generator|
      val = JsonPath.new(selector).on(example_value)
      replace = execute_generators(generator, state_params, val)
      example_value = JsonPath.for(example_value).gsub(selector) { |_v| replace }.to_hash
    end
  end
  example_value
end

.execute_generators(object, state_params = nil, example_value = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/pact/generators.rb', line 26

def self.execute_generators(object, state_params = nil, example_value = nil)
  generators.each do |parser|
    return parser.call(object, state_params, example_value) if parser.can_generate?(object)
  end

  raise Pact::UnrecognizePactFormatError, "This document does not use a recognised Pact generator: #{object}"
end

.generatorsObject



22
23
24
# File 'lib/pact/generators.rb', line 22

def self.generators
  @generators ||= []
end