Sha256: 1204b211505f11f17b85557831e4519cc38aaec4ba04abc5e7b77608195c1a4b

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

# frozen_string_literal: true

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

1 entries across 1 versions & 1 rubygems

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