Sha256: 42fe6740a7956d411a7c21f9550d99c49fa9d33030112fe10ded69998eee90ae

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'kind/basic'

module Kind
  module Maybe
    require 'kind/maybe/monad'
    require 'kind/maybe/none'
    require 'kind/maybe/some'
    require 'kind/maybe/wrapper'
    require 'kind/maybe/typed'
    require 'kind/maybe/methods'

    extend self

    def new(value)
      (::Exception === value || KIND.nil_or_undefined?(value) ? None : Some)
        .new(value)
    end

    alias_method :[], :new

    module Buildable
      def maybe(value = UNDEFINED, &block)
        return __maybe[value] if UNDEFINED != value && !block
        return __maybe.wrap(&block) if UNDEFINED == value && block
        return __maybe.wrap(value, &block) if UNDEFINED != value && block

        __maybe
      end

      alias_method :optional, :maybe

      private

        def __maybe
          @__maybe ||= Maybe::Typed[self]
        end
    end

    extend Wrapper
  end

  Optional = Maybe

  None = Maybe::NONE_INSTANCE

  def self.None
    Maybe::NONE_INSTANCE
  end

  def self.Some(value)
    Maybe::Some[value]
  end

  def self.Maybe(kind)
    warn '[DEPRECATION] Kind::Maybe() is deprecated; use Kind::Maybe::Typed[] instead. ' \
        'It will be removed on next major release.'

    Maybe::Typed[kind]
  end

  def self.Optional(kind)
    warn '[DEPRECATION] Kind::Optional() is deprecated; use Kind::Optional::Typed[] instead. ' \
        'It will be removed on next major release.'

    Optional::Typed[kind]
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
kind-5.10.0 lib/kind/maybe.rb
kind-5.9.0 lib/kind/maybe.rb
kind-5.8.1 lib/kind/maybe.rb
kind-5.8.0 lib/kind/maybe.rb
kind-5.7.0 lib/kind/maybe.rb
kind-5.6.0 lib/kind/maybe.rb
kind-5.5.0 lib/kind/maybe.rb
kind-5.4.1 lib/kind/maybe.rb
kind-5.4.0 lib/kind/maybe.rb
kind-5.3.0 lib/kind/maybe.rb