class Dry::Matcher::Case

{Case} object contains logic for pattern matching and resolving result from matched pattern

Constants

DEFAULT_RESOLVE

Public Class Methods

new(match: Undefined, resolve: DEFAULT_RESOLVE, &block) click to toggle source

@param match [#call] callable used to test given pattern against value @param resolve [#call] callable used to resolve value into a result

# File lib/dry/matcher/case.rb, line 12
def initialize(match: Undefined, resolve: DEFAULT_RESOLVE, &block)
  if block
    @match = block
  else
    @match = proc do |value, patterns|
      if match.(value, *patterns)
        resolve.(value)
      else
        Undefined
      end
    end
  end
end

Public Instance Methods

call(value, patterns = EMPTY_ARRAY, &block) click to toggle source

@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
# File lib/dry/matcher/case.rb, line 31
def call(value, patterns = EMPTY_ARRAY, &block)
  Undefined.map(@match.(value, patterns), &block)
end