Sha256: 811b2df1b344d60ae1f6b92ad87615e7007f00659d68202705b260a78a691ffe

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Fear
  class Success
    include Try
    include RightBiased::Right
    include SuccessPatternMatch.mixin

    EXTRACTOR = proc do |try|
      if Fear::Success === try
        Fear.some([try.get])
      else
        Fear.none
      end
    end

    attr_reader :value
    protected :value

    # @param [any]
    def initialize(value)
      @value = value
    end

    # @return [any]
    def get
      @value
    end

    # @return [Boolean]
    def success?
      true
    end

    # @return [false]
    def failure?
      false
    end

    # @return [Success]
    def or_else
      self
    end

    # @return [Try]
    def flatten
      if value.is_a?(Try)
        value.flatten
      else
        self
      end
    end

    # @yieldparam [any] value
    # @yieldreturn [Boolean]
    # @return [Try]
    def select
      if yield(value)
        self
      else
        raise NoSuchElementError, "Predicate does not hold for `#{value}`"
      end
    rescue StandardError => error
      Failure.new(error)
    end

    # @return [Success]
    def recover_with
      self
    end

    # @return [Success]
    def recover
      self
    end

    # @return [Try]
    def map
      super
    rescue StandardError => error
      Failure.new(error)
    end

    # @return [Try]
    def flat_map
      super
    rescue StandardError => error
      Failure.new(error)
    end

    # @return [Right]
    def to_either
      Right.new(value)
    end

    # @param other [Any]
    # @return [Boolean]
    def ==(other)
      other.is_a?(Success) && value == other.value
    end

    # @return [String]
    def inspect
      "#<Fear::Success value=#{value.inspect}>"
    end

    # @return [String]
    alias to_s inspect
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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