Class: Qo::Matchers::GuardBlockMatcher

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

Overview

A GuardBlockMatcher is like a regular matcher, except in that if it "matches" it will provide its match target to its associated block.

It returns tuples of (status, result) in order to prevent masking of legitimate falsy or nil values returned.

Guard Block Matchers are best used with a PatternMatch, and chances are you're going to want to read that documentation first.

Examples:

Qo::Matchers::GuardBlockMatcher == Qo.matcher == Qo.m

guard_matcher = Qo.m(Integer) { |v| v * 2 }
guard_matcher.call(1)  # => [true, 2]
guard_matcher.call(:x) # => [false, false]

See Also:

Author:

  • baweaver

Since:

  • 0.1.5

Constant Summary

IDENTITY =

Identity function that returns its argument directly

Since:

  • 0.1.5

-> v { v }
NON_MATCH =

Definition of a non-match

Since:

  • 0.1.5

[false, false]

Instance Method Summary collapse

Methods inherited from BaseMatcher

#call

Constructor Details

#initialize(*array_matchers, **keyword_matchers, &fn) ⇒ GuardBlockMatcher

Returns a new instance of GuardBlockMatcher

Since:

  • 0.1.5



32
33
34
35
36
# File 'lib/qo/matchers/guard_block_matcher.rb', line 32

def initialize(*array_matchers, **keyword_matchers, &fn)
  @fn = fn || IDENTITY

  super('and', *array_matchers, **keyword_matchers)
end

Instance Method Details

#to_procProc[Any] - (Bool, Any)

Overrides the base matcher's #to_proc to wrap the value in a status and potentially call through to the associated block if a base matcher would have passed

Returns:

  • (Proc[Any] - (Bool, Any))

    (status, result) tuple

Since:

  • 0.1.5



44
45
46
47
48
# File 'lib/qo/matchers/guard_block_matcher.rb', line 44

def to_proc
  Proc.new { |target|
    super[target] ? [true, @fn.call(target)] : NON_MATCH
  }
end