Sha256: 27fc170912fcc5ee781aed14300a0a28d415d670b512b1509e48263f2054a827

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

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:, resolve: DEFAULT_RESOLVE)
        @match = match
        @resolve = resolve
      end

      # Tests whether `value` (with optional `*pattern`) matches pattern using
      # callable given to {#initialize} as `match:` argument
      #
      # @param [Object] value
      # @param [<Object>] pattern optional pattern given after the `value` to
      #   `match:` callable
      # @return [Boolean]
      def matches?(value, *pattern)
        @match.(value, *pattern)
      end

      # Resolves result from `value` using callable given to {#initialize}
      # as `resolve:` argument
      #
      # @param [Object] value
      # @return [Object] result resolved from given `value`
      def resolve(value)
        @resolve.(value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-matcher-0.7.0 lib/dry/matcher/case.rb
dry-matcher-0.6.0 lib/dry/matcher/case.rb