Class: Qo::Matchers::ArrayMatcher

Inherits:
BaseMatcher show all
Defined in:
lib/qo/matchers/array_matcher.rb

Overview

An Array Matcher is a matcher that uses only varargs to define a sequence of matches to perform against either an object or another Array.

In the case of an Array matching against an Array it will compare via index.

In the case of an Array matching against an Object, it will match each provided matcher against the object.

All variants present in the BaseMatcher are present here, including ‘and’, ‘not’, and ‘or’.

Author:

  • baweaver

Instance Method Summary collapse

Methods inherited from BaseMatcher

#initialize

Constructor Details

This class inherits a constructor from Qo::Matchers::BaseMatcher

Instance Method Details

#call(target) ⇒ Boolean

Invocation for the match sequence. Will determine the target and applicable matchers to run against it.

Parameters:

  • target (Any)

Returns:

  • (Boolean)

    Match status



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/qo/matchers/array_matcher.rb', line 36

def call(target)
  return true if @array_matchers == target

  if target.is_a?(::Array)
    match_with(@array_matchers.each_with_index) { |matcher, i|
      match_value?(target[i], matcher)
    }
  else
    match_with(@array_matchers) { |matcher|
      match_value?(target, matcher)
    }
  end
end

#to_procProc[Any]

Used to match against a matcher made from an Array, like:

Qo['Foo', 'Bar']

Parameters:

  • matchers (Array[respond_to?(===)])

    indexed tuple to match the target object against

Returns:

  • (Proc[Any])

    Array -> Bool # Tuple match against targets index Object -> Bool # Boolean public send



26
27
28
# File 'lib/qo/matchers/array_matcher.rb', line 26

def to_proc
  Proc.new { |target| self.call(target) }
end