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.
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
-
#initialize(*array_matchers, **keyword_matchers, &fn) ⇒ GuardBlockMatcher
constructor
A new instance of GuardBlockMatcher.
-
#to_proc ⇒ Proc
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.
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_proc ⇒ Proc
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
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 |