Sha256: 942102e57005cfef0b58516fd9783d534a2123dca563ddd1922d3c3bc0fd1b34

Contents?: true

Size: 860 Bytes

Versions: 2

Compression:

Stored size: 860 Bytes

Contents

module Fear
  # Try pattern matcher
  #
  # @note it has two optimized subclasses +Fear::SuccessPatternMatch+ and +Fear::FailurePatternMatch+
  # @api private
  class TryPatternMatch < Fear::PatternMatch
    SUCCESS_EXTRACTOR = :get.to_proc
    FAILURE_EXTRACTOR = :exception.to_proc

    # Match against +Fear::Success+
    #
    # @param conditions [<#==>]
    # @return [Fear::TryPatternMatch]
    def success(*conditions, &effect)
      branch = Fear.case(Fear::Success, &SUCCESS_EXTRACTOR).and_then(Fear.case(*conditions, &effect))
      or_else(branch)
    end

    # Match against +Fear::Failure+
    #
    # @param conditions [<#==>]
    # @return [Fear::TryPatternMatch]
    def failure(*conditions, &effect)
      branch = Fear.case(Fear::Failure, &FAILURE_EXTRACTOR).and_then(Fear.case(*conditions, &effect))
      or_else(branch)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fear-1.0.0 lib/fear/try_pattern_match.rb
fear-0.11.0 lib/fear/try_pattern_match.rb