### Use dry-matcher for Success Scenarios Source: https://github.com/dry-rb/dry-matcher/blob/main/docsite/source/index.html.md This Ruby example demonstrates how to use a previously built `Dry::Matcher` instance to process a successful result. It illustrates the block syntax for `m.success` and `m.failure` clauses, showing how the `success` block is executed when the input matches the success case. ```ruby my_success = [:ok, "success!"] result = matcher.(my_success) do |m| m.success do |v| "Yay: #{v}" end # :not_found and :lost are patterns m.failure :not_found, :lost do |v| "Oops, not found: #{v}" end m.failure do |v| "Boo: #{v}" end end result # => "Yay: success!" ``` -------------------------------- ### Use dry-matcher for Failure Scenarios with Patterns Source: https://github.com/dry-rb/dry-matcher/blob/main/docsite/source/index.html.md This Ruby example showcases the `Dry::Matcher` handling a failure result, specifically demonstrating pattern matching within `m.failure` clauses. It highlights how the matcher evaluates cases in order and executes the first matching failure block, including those with specific patterns like `:not_found`. ```ruby my_failure = [:err, :not_found, "missing!"] result = matcher.(my_failure) do |m| m.success do |v| "Yay: #{v}" end m.failure :not_found do |v| "Oops, not found: #{v}" end m.failure do |v| "Boo: #{v}" end end result # => "Oops, not found: missing!" ``` -------------------------------- ### Define Custom dry-matcher Cases Source: https://github.com/dry-rb/dry-matcher/blob/main/docsite/source/index.html.md This Ruby code defines custom `Dry::Matcher::Case` objects for handling success (`:ok`) and failure (`:err`) conditions. It shows how to check the input code and value, returning the value on match or `Dry::Matcher::Undefined` to signal no match. Finally, it demonstrates building a `Dry::Matcher` instance with these defined cases. ```ruby require "dry-matcher" # Match `[:ok, some_value]` for success success_case = Dry::Matcher::Case.new do |(code, value), _| if code.equal?(:ok) value else # this is a constant from dry/core/constants Dry::Matcher::Undefined end end # Match `[:err, some_error_code, some_value]` for failure failure_case = Dry::Matcher::Case.new do |(code, value), patterns| if code.equal?(:err) && (patterns.empty? || patterns.include?(value)) value else Dry::Matcher::Undefined end end # Build the matcher matcher = Dry::Matcher.new(success: success_case, failure: failure_case) ``` -------------------------------- ### Using MaybeMatcher with dry-monads Source: https://github.com/dry-rb/dry-matcher/blob/main/docsite/source/maybe-matcher.html.md Demonstrates how to use `Dry::Matcher::MaybeMatcher` to pattern match on `Dry::Monads::Maybe` values, handling `some` (with and without type matching) and `none` cases. It shows how the matcher evaluates the block and returns the result based on the matched case. ```ruby require "dry/monads" require "dry/matcher/maybe_matcher" value = Dry::Monads::Maybe("success!") result = Dry::Matcher::MaybeMatcher.(value) do |m| m.some(Integer) do |i| "Got int: #{i}" end m.some do |v| "Yay: #{v}" end m.none do "Boo: none" end end result # => "Yay: success!" ``` -------------------------------- ### Matching on Dry::Monads::Success with ResultMatcher Source: https://github.com/dry-rb/dry-matcher/blob/main/docsite/source/result-matcher.html.md Demonstrates how to use `Dry::Matcher::ResultMatcher` to pattern match on a `Dry::Monads::Success` value. It shows different match clauses for success and failure, including type-specific matching for `Integer` and symbol-based failure matching, illustrating how the matcher selects the most appropriate block. ```ruby require "dry/monads" require "dry/matcher/result_matcher" value = Dry::Monads::Success("success!") result = Dry::Matcher::ResultMatcher.(value) do |m| m.success(Integer) do |i| "Got int: #{i}" end m.success do |v| "Yay: #{v}" end m.failure :not_found do |_err, reason| "Nope: #{reason}" end m.failure do |v| "Boo: #{v}" end end result # => "Yay: success!" ``` -------------------------------- ### Integrate Dry::Matcher into a Ruby Class Method Source: https://github.com/dry-rb/dry-matcher/blob/main/docsite/source/class-enhancement.html.md This snippet demonstrates how to integrate `Dry::Matcher` into a custom Ruby class using `Dry::Matcher.for`. It shows the process of defining a matcher, including it in a class's method (e.g., `call`), and then utilizing the exposed matcher block API on an instance of that class. ```ruby require "dry-matcher" # First, build a matcher or use an existing one (like dry-matcher's ResultMatcher) MyMatcher = Dry::Matcher.new(...) # Offer it from your class with `Dry::Matcher.for` class MyOperation include Dry::Matcher.for(:call, with: MyMatcher) def call # return a value here end end # And now `MyOperation#call` offers the matcher block API operation = MyOperation.new operation.() do |m| # Use the matcher's API here end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.