Class: Matchi::Satisfy
- Inherits:
-
Object
- Object
- Matchi::Satisfy
- Defined in:
- lib/matchi/satisfy.rb
Overview
Custom predicate matcher that validates values against an arbitrary condition.
This matcher provides a flexible way to test values against custom conditions defined in a block. Unlike specific matchers that test for predetermined conditions, Satisfy allows you to define any custom validation logic at runtime. This makes it particularly useful for complex or composite conditions that aren’t covered by other matchers.
Instance Method Summary collapse
-
#initialize {|Object| ... } ⇒ Satisfy
constructor
Initialize the matcher with a validation block.
-
#match? { ... } ⇒ Boolean
Checks if the yielded value satisfies the validation block.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize {|Object| ... } ⇒ Satisfy
Initialize the matcher with a validation block.
68 69 70 71 72 |
# File 'lib/matchi/satisfy.rb', line 68 def initialize(&block) raise ::ArgumentError, "a block must be provided" unless block_given? @expected = block end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the yielded value satisfies the validation block.
This method passes the value returned by the provided block to the validation block defined at initialization. The matcher succeeds if the validation block returns a truthy value.
98 99 100 101 102 |
# File 'lib/matchi/satisfy.rb', line 98 def match? raise ::ArgumentError, "a block must be provided" unless block_given? @expected.call(yield) end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
112 113 114 |
# File 'lib/matchi/satisfy.rb', line 112 def to_s "satisfy &block" end |