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.

Author:

  • baweaver

Constant Summary collapse

IDENTITY =

Identity function that returns its argument directly

-> v { v }
NON_MATCH =

Definition of a non-match

[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.



18
19
20
21
22
# File 'lib/qo/matchers/guard_block_matcher.rb', line 18

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

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

Instance Method Details

#to_procProc

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



30
31
32
33
34
# File 'lib/qo/matchers/guard_block_matcher.rb', line 30

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