Sha256: 356ef3f1204f9235149b593b21973886efcceeb1dfbeb95a29aec01298fc841d

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "dry/matcher"
require "dry/matcher/match"

module Dry
  class Matcher
    # Built-in {Matcher} ready to use with `Maybe` monad from
    # [dry-monads](/gems/dry-monads) or any other compatible gems.
    #
    # Provides {Case}s for two matchers:
    # * `:some` matches `Dry::Monads::Maybe::Some`
    # * `:none` matches `Dry::Monads::Maybe::None`
    #
    # @return [Dry::Matcher]
    #
    # @example Usage with `dry-monads`
    #   require 'dry/monads'
    #   require 'dry/matcher/maybe_matcher'
    #
    #   value = Dry::Monads::Maybe.new('there is a value!')
    #
    #   Dry::Matcher::MaybeMatcher.(value) do |m|
    #     m.some do |v|
    #       "Yay: #{v}"
    #     end
    #
    #     m.none do
    #       "Boo: none"
    #     end
    #   end #=> "Yay: there is a value!"
    #
    #
    # @example Usage with specific types
    #   value = Dry::Monads::Maybe.new([200, :ok])
    #
    #   Dry::Matcher::MaybeMatcher.(value) do |m|
    #     m.some(200, :ok) do |code, value|
    #       "Yay: #{value}"
    #     end
    #
    #     m.none do
    #       "Boo: none"
    #     end
    #   end #=> "Yay: :ok"
    #
    MaybeMatcher = Dry::Matcher.new(
      some: Case.new { |maybe, patterns|
        if maybe.none?
          Undefined
        else
          Dry::Matcher::PatternMatch.(maybe.value!, patterns)
        end
      },
      none: Case.new { |maybe|
        if maybe.some?
          Undefined
        end
      }
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-matcher-1.0.0 lib/dry/matcher/maybe_matcher.rb
dry-matcher-0.10.0 lib/dry/matcher/maybe_matcher.rb