Class: Pact::Matchers::V4::EachValue

Inherits:
Base show all
Defined in:
lib/pact/matchers/v4/each_value.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(value_matchers, template) ⇒ EachValue

Returns a new instance of EachValue.



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

def initialize(value_matchers, template)
  # raise MatcherInitializationError, "#{self.class}: #{template} should be a Hash" unless template.is_a?(Hash)
  unless value_matchers.is_a?(Array)
    raise MatcherInitializationError,
          "#{self.class}: #{value_matchers} should be an Array"
  end
  unless value_matchers.all?(Pact::Matchers::Base)
    raise MatcherInitializationError,
          "#{self.class}: #{value_matchers} should be instances of Pact::Matchers::Base"
  end
  unless value_matchers.size > 0
    raise MatcherInitializationError,
          "#{self.class}: #{value_matchers} size should be greater than 0"
  end

  super(spec_version: Pact::Matchers::PACT_SPEC_V4, kind: 'each-value', template: template, opts: { rules: value_matchers }) # rubocop:disable Layout/LineLength
end

Instance Method Details

#as_pluginObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pact/matchers/v4/each_value.rb', line 25

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

  @opts[:rules].map do |matcher|
    "eachValue(#{matcher.as_plugin})"
  end.join(', ')
end