Sha256: 10e9600970284ea763fdff893c3d37deef3c5423d3f1136d0edf8d883f7039e1

Contents?: true

Size: 990 Bytes

Versions: 1

Compression:

Stored size: 990 Bytes

Contents

require "resultt/version"
require "resultt/result_methods"

module Resultt
  class NilValueError < StandardError; end;

  def Result
    value = yield_all(yield)
    success = Success.new(value)
    raise NilValueError, 'Resultt returned a nil value' if success.value.nil?

    success
  rescue StandardError => e
    Error.new(e)
  end

  def Success(value)
    Success.new(value)
  end

  def Error(error)
    Error.new(error)
  end

  class Success
    include ResultMethods

    attr_reader :value

    def initialize(value)
      @value = value
    end

    def ok?
      true
    end
    alias success? ok?

    def error?
      false
    end
  end

  class Error
    include ResultMethods

    attr_reader :error

    def initialize(error)
      @error = error
    end

    def error?
      true
    end

    def ok?
      false
    end
    alias success? ok?
  end

  private

  def yield_all(yielded)
    return yield_all(yielded.call) if yielded.is_a? Proc

    yielded
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resultt-1.0.0 lib/resultt.rb