Class: Dry::Matcher::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/matcher/evaluator.rb

Overview

Evaluator is used in Dry::Matcher#call block to handle different Cases

Instance Method Summary collapse

Constructor Details

#initialize(result, cases) ⇒ Evaluator



11
12
13
14
15
16
# File 'lib/dry/matcher/evaluator.rb', line 11

def initialize(result, cases)
  @cases = cases
  @result = result

  @unhandled_cases = @cases.keys.map(&:to_sym)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) {|v| ... } ⇒ Object

Handles method ‘name` called after one of the keys in `cases` hash given to #initialize

Yield Parameters:

  • v (Object)

    resolved value

Raises:

  • (NoMethodError)

    if there was no case called ‘name` given to #initialize in `cases` hash



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dry/matcher/evaluator.rb', line 48

def method_missing(name, *args, &block)
  kase = @cases.fetch(name) { return super }

  @unhandled_cases.delete name

  unless defined? @output
    kase.(@result, args) do |result|
      @output = yield(result)
    end
  end
end

Instance Method Details

#call {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
# File 'lib/dry/matcher/evaluator.rb', line 18

def call
  yield self

  ensure_exhaustive_match

  @output if defined? @output
end

#respond_to_missing?(name, _include_private = false) ⇒ Boolean

Checks whether ‘cases` given to #initialize contains one called `name`



30
31
32
# File 'lib/dry/matcher/evaluator.rb', line 30

def respond_to_missing?(name, _include_private = false)
  @cases.key?(name)
end