Class: Dry::Matcher::Evaluator
- Inherits:
-
Object
- Object
- Dry::Matcher::Evaluator
- Defined in:
- lib/dry/matcher/evaluator.rb
Overview
Evaluator is used in Dry::Matcher#call block to handle different Cases
Instance Method Summary collapse
- #call {|_self| ... } ⇒ Object
-
#initialize(result, cases) ⇒ Evaluator
constructor
A new instance of Evaluator.
-
#method_missing(name, *args, &block) {|v| ... } ⇒ Object
Handles method ‘name` called after one of the keys in `cases` hash given to #initialize.
-
#respond_to_missing?(name, _include_private = false) ⇒ Boolean
Checks whether ‘cases` given to #initialize contains one called `name`.
Constructor Details
#initialize(result, cases) ⇒ Evaluator
Returns a new instance of 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
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
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 |