Class: Dry::Matcher::Case
- Inherits:
-
Object
- Object
- Dry::Matcher::Case
- Defined in:
- lib/dry/matcher/case.rb
Overview
Case object contains logic for pattern matching and resolving result from matched pattern
Constant Summary collapse
- DEFAULT_RESOLVE =
-> result { result }
Instance Method Summary collapse
-
#call(value, patterns = EMPTY_ARRAY) {|v| ... } ⇒ Object, Dry::Core::Constants::Undefined
Either the yield result or Undefined if match wasn’t successful.
-
#initialize(match: Undefined, resolve: DEFAULT_RESOLVE, &block) ⇒ Case
constructor
A new instance of Case.
Constructor Details
#initialize(match: Undefined, resolve: DEFAULT_RESOLVE, &block) ⇒ Case
Returns a new instance of Case.
12 13 14 15 16 17 18 19 20 |
# File 'lib/dry/matcher/case.rb', line 12 def initialize(match: Undefined, resolve: DEFAULT_RESOLVE, &block) @match = block || proc do |value, patterns| if match.(value, *patterns) resolve.(value) else Undefined end end end |
Instance Method Details
#call(value, patterns = EMPTY_ARRAY) {|v| ... } ⇒ Object, Dry::Core::Constants::Undefined
Returns Either the yield result or Undefined if match wasn’t successful.
27 28 29 |
# File 'lib/dry/matcher/case.rb', line 27 def call(value, patterns = EMPTY_ARRAY, &block) Undefined.map(@match.(value, patterns), &block) end |