Sha256: 25a13e1e38f8c5fdce06d54c2172c108351caee1e8489de100d0e719e6147a67

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Dry
  class Matcher
    # {Case} object contains logic for pattern matching and resolving result
    # from matched pattern
    class Case
      DEFAULT_RESOLVE = -> result { result }

      # @param match [#call] callable used to test given pattern against value
      # @param resolve [#call] callable used to resolve value into a result
      def initialize(match: Undefined, resolve: DEFAULT_RESOLVE, &block)
        @match = block || proc do |value, patterns|
          if match.(value, *patterns)
            resolve.(value)
          else
            Undefined
          end
        end
      end

      # @param [Object] value Value to match
      # @param [Array<Object>] patterns Optional list of patterns to match against
      # @yieldparam [Object] v Resolved value if match succeeds
      # @return [Object,Dry::Core::Constants::Undefined] Either the yield result
      #   or Undefined if match wasn't successful
      def call(value, patterns = EMPTY_ARRAY, &block)
        Undefined.map(@match.(value, patterns), &block)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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