bolshakov/fear

View on GitHub
lib/fear/empty_partial_function.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

module Fear
  # Use singleton version of EmptyPartialFunction -- PartialFunction::Empty
  # @api private
  class EmptyPartialFunction
    include PartialFunction

    def defined_at?(_)
      false
    end

    def call(arg)
      raise MatchError, "partial function not defined at: #{arg}"
    end

    alias === call
    alias [] call

    def call_or_else(arg)
      yield arg
    end

    def or_else(other)
      other
    end

    def and_then(*)
      self
    end

    def to_s
      "Empty partial function"
    end
  end

  private_constant :EmptyPartialFunction
end