Class: Qo::PatternMatchers::ResultPatternMatch

Inherits:
PatternMatch
  • Object
show all
Defined in:
lib/qo/pattern_matchers/result_pattern_match.rb

Overview

Unlike the normal pattern matcher, this one works on tuple arrays containing a status and a result like [:ok, result] and [:err, message].

Note that each of these can still take conditionals much like a where branch for more fine grained control over what we're looking for.

Examples:

```ruby
matcher = Qo.result_match { |m|
  m.success(String) { |v| "Hello #{v}"}
  m.success         { |v| v + 10 }
  m.failure         { |v| "Error: #{v}" }
}

matcher.call([:ok, 'there'])
# => "Hello there"

matcher.call([:ok, 4])
# => 14

matcher.call([:err, 'OH NO'])
# => "Error: OH NO"
```

Author:

  • baweaver

Since:

  • 1.0.0

Instance Method Summary collapse

Methods inherited from PatternMatch

#call, create, mixin, #to_proc

Methods included from Branching

included

Constructor Details

#initialize(destructure: false) ⇒ type

Creates a new result matcher

Parameters:

  • destructure: (defaults to: false)

    false [Boolean] Whether or not to destructure the value before yielding to the first matched block

Since:

  • 1.0.0



40
41
42
# File 'lib/qo/pattern_matchers/result_pattern_match.rb', line 40

def initialize(destructure: false)
  super(destructure: destructure)
end