Sha256: 3378cd2da9983ef69cd46604b3af39fd6498d5ae36e4d93843df18aad1df90d2

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Fear
  class Success
    include Try
    include Dry::Equalizer(:value)
    include RightBiased::Right
    include SuccessPatternMatch.mixin

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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