Sha256: 63543e7e458155b5b3ab6abc78e875119aac3c81d00bed42ae2a85662a9c5454

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Fear
  # Option pattern matcher
  #
  # @example
  #   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'
  #
  #  @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
  #
  # @note it has two optimized subclasses +Fear::SomePatternMatch+ and +Fear::NonePatternMatch+
  # @api private
  class OptionPatternMatch < Fear::PatternMatch
    GET_METHOD = :get.to_proc

    # Match against Some
    #
    # @param conditions [<#==>]
    # @return [Fear::OptionPatternMatch]
    def some(*conditions, &effect)
      branch = Fear.case(Fear::Some, &GET_METHOD).and_then(Fear.case(*conditions, &effect))
      or_else(branch)
    end

    # Match against None
    #
    # @param effect [Proc]
    # @return [Fear::OptionPatternMatch]
    def none(&effect)
      branch = Fear.case(Fear::None, &effect)
      or_else(branch)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fear-1.1.0 lib/fear/option_pattern_match.rb