Class: Pact::Matchers::V3::Each

Inherits:
Base show all
Defined in:
lib/pact/matchers/v3/each.rb

Instance Attribute Summary

Attributes inherited from Base

#kind, #opts, #spec_version, #template

Instance Method Summary collapse

Methods inherited from Base

#as_basic

Constructor Details

#initialize(template, min) ⇒ Each

Returns a new instance of Each.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pact/matchers/v3/each.rb', line 7

def initialize(template, min)
  if min.present? && min < 1
    raise MatcherInitializationError,
          "#{self.class}: #{min} should be greater than 0"
  end

  min_array_size = min.presence || 1
  val = template.is_a?(Array) ? template : [template] * min_array_size

  if min_array_size != val.size
    raise MatcherInitializationError,
          "#{self.class}: #{min} is invalid: template size is #{val.size}"
  end

  super(
    spec_version: Pact::Matchers::PACT_SPEC_V3,
    kind: 'type',
    template: val,
    opts: { min: min_array_size })
end

Instance Method Details

#as_pluginObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pact/matchers/v3/each.rb', line 28

def as_plugin
  if @template.first.is_a?(Hash)
    return {
      'pact:match' => "eachValue(matching($'SAMPLE'))",
      'SAMPLE' => serialize!(@template.first.deep_dup, :plugin)
    }
  end

  params = @opts.except(:min).values.map { |v| format_primitive(v) }.join(',')
  value = format_primitive(@template.first)

  return "eachValue(matching(#{@kind}, #{params}, #{value}))" if params.present?

  "eachValue(matching(#{@kind}, #{value}))"
end