Sha256: 32f0b519e8f6829a1d3cca9981042dc57ff1ded3355174965c6dc1a43195c62c

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Kind
  module Maybe
    class None < Result
      INVALID_DEFAULT_ARG = 'the default value must be defined as an argument or block'.freeze

      def value_or(default = UNDEFINED, &block)
        raise ArgumentError, INVALID_DEFAULT_ARG if UNDEFINED == default && !block

        UNDEFINED != default ? default : block.call
      end

      def none?; true; end

      def map(&fn)
        self
      end

      alias_method :map!, :map
      alias_method :then, :map
      alias_method :then!, :map
      alias_method :check, :map

      def try!(method_name = UNDEFINED, *args, &block)
        KIND.of!(::Symbol, method_name)if UNDEFINED != method_name

        self
      end

      alias_method :try, :try!

      def dig(*keys)
        self
      end

      def presence
        self
      end

      private_constant :INVALID_DEFAULT_ARG
    end

    NONE_WITH_NIL_VALUE = None.new(nil)
    NONE_WITH_UNDEFINED_VALUE = None.new(Undefined)

    def none
      NONE_WITH_NIL_VALUE
    end

    def __none__(value) # :nodoc:
      None.new(value)
    end

    private_constant :NONE_WITH_NIL_VALUE, :NONE_WITH_UNDEFINED_VALUE
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kind-5.0.0 lib/kind/maybe/none.rb
kind-4.1.0 lib/kind/maybe/none.rb