Sha256: 01a08c715e8b8642706fcb3855db216a194120c0e42410b96ea408b7878d7496

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Kind
  module Maybe
    class Monad
      require 'kind/maybe/monad/wrapper'

      attr_reader :value

      Value = ->(arg) { arg.kind_of?(Maybe::Monad) ? arg.value : arg } # :nodoc:

      def initialize(value)
        @value = Value[value]
      end

      def value_or(_method_name = UNDEFINED, &block)
        raise NotImplementedError
      end

      def none?
        raise NotImplementedError
      end

      def some?; !none?; end

      def map(&fn)
        raise NotImplementedError
      end

      alias_method :map!, :map
      alias_method :then, :map
      alias_method :then!, :map
      alias_method :and_then, :map
      alias_method :and_then!, :map!

      def check(&fn)
        raise NotImplementedError
      end

      alias_method :accept, :check
      alias_method :reject, :check

      def try(_method_name = UNDEFINED, &block)
        raise NotImplementedError
      end

      alias_method :try!, :try

      def dig(*keys)
        raise NotImplementedError
      end

      def presence
        raise NotImplementedError
      end

      def on
        monad = Wrapper.new(self)

        yield(monad)

        monad.output
      end

      def on_some(matcher = UNDEFINED)
        yield(value) if some? && maybe?(matcher)

        self
      end

      def on_none(matcher = UNDEFINED)
        yield(value) if none? && maybe?(matcher)

        self
      end

      def maybe?(matcher)
        UNDEFINED == matcher || matcher === value
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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