Class: Qo::Matchers::GuardBlockMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- Qo::Matchers::GuardBlockMatcher
- 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.
Constant Summary
- IDENTITY =
Identity function that returns its argument directly
-> v { v }
- NON_MATCH =
Definition of a non-match
[false, false]
Instance Method Summary collapse
-
#initialize(*array_matchers, **keyword_matchers, &fn) ⇒ GuardBlockMatcher
constructor
A new instance of GuardBlockMatcher.
-
#to_proc ⇒ Proc[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.
Methods inherited from BaseMatcher
Constructor Details
#initialize(*array_matchers, **keyword_matchers, &fn) ⇒ GuardBlockMatcher
Returns a new instance of GuardBlockMatcher
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_proc ⇒ Proc[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
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 |