Sha256: 8cd41e5caffc0a4ff227dad3e0aa5e835d2424ee13553d66778d6492803b28c2

Contents?: true

Size: 824 Bytes

Versions: 1

Compression:

Stored size: 824 Bytes

Contents

require "dry/matcher/case"
require "dry/matcher/evaluator"

module Dry
  class Matcher
    def self.for(*match_methods, with:)
      matcher = with

      matchers_mod = Module.new do
        match_methods.each do |match_method|
          define_method(match_method) do |*args, &block|
            result = super(*args)

            if block
              matcher.(result, &block)
            else
              result
            end
          end
        end
      end

      Module.new do
        const_set :Matchers, matchers_mod

        def self.included(klass)
          klass.prepend const_get(:Matchers)
        end
      end
    end

    attr_reader :cases

    def initialize(cases = {})
      @cases = cases
    end

    def call(result, &block)
      Evaluator.new(result, cases).call(&block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-matcher-0.5.0 lib/dry/matcher.rb