Class: Fear::OptionPatternMatch Private

Inherits:
PatternMatch show all
Defined in:
lib/fear/option_pattern_match.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

it has two optimized subclasses Fear::SomePatternMatch and Fear::NonePatternMatch

Option pattern matcher

@example the same matcher may be defined using block syntax
  OptionPatternMatch.new do |m|
     m.some(Integer) { |x| x * 2 }
     m.some(String) { |x| x.to_i * 2 }
     m.none { 'NaN' }
     m.else { 'error '}
  end

Examples:

pattern_match =
  OptionPatternMatch.new
    .some(Integer) { |x| x * 2 }
    .some(String) { |x| x.to_i * 2 }
    .none { 'NaN' }
    .else { 'error '}

pattern_match.call(42) => 'NaN'

Constant Summary collapse

GET_METHOD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

:get.to_proc

Instance Attribute Summary

Attributes inherited from PatternMatch

#result

Instance Method Summary collapse

Methods inherited from PatternMatch

__new__, #case, #else, #initialize, mixin, new, #or_else, #xcase

Constructor Details

This class inherits a constructor from Fear::PatternMatch

Instance Method Details

#none(&effect) ⇒ Fear::OptionPatternMatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Match against None


42
43
44
45
# File 'lib/fear/option_pattern_match.rb', line 42

def none(&effect)
  branch = Fear.case(Fear::None, &effect)
  or_else(branch)
end

#some(*conditions, &effect) ⇒ Fear::OptionPatternMatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Match against Some


33
34
35
36
# File 'lib/fear/option_pattern_match.rb', line 33

def some(*conditions, &effect)
  branch = Fear.case(Fear::Some, &GET_METHOD).and_then(Fear.case(*conditions, &effect))
  or_else(branch)
end