Sha256: b02d85f6e8c50d15864789bab25cea4f55d3a0412df3f8961d0f86efc70590ea

Contents?: true

Size: 904 Bytes

Versions: 8

Compression:

Stored size: 904 Bytes

Contents

# frozen_string_literal: true

module Matheus
  module Result
    Success = Data.define(:value) do
      def success? = true

      def failure? = false

      def on_success = yield(value)

      def on_failure = self

      def error = nil

      def inspect = "Success(#{value.inspect})"
    end

    Failure = Data.define(:error) do
      def success? = false

      def failure? = true

      def on_success = self

      def on_failure = yield(error)

      def value = raise "Can't call #value on a #{self.class} object. Object is #{inspect}"

      def inspect = "Failure(#{error.inspect})"
    end

    module Methods
      def Success(value) = Result::Success.new(value)

      def Failure(error) = Result::Failure.new(error)
    end

    def self.from(value)
      if value.is_a?(Success) || value.is_a?(Failure)
        value
      else
        Success.new(value)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
matheus-0.6.1 lib/matheus/result.rb
matheus-0.6.0 lib/matheus/result.rb
matheus-0.5.0 lib/matheus/result.rb
matheus-0.4.0 lib/matheus/result.rb
matheus-0.3.0 lib/matheus/result.rb
matheus-0.2.1 lib/matheus/result.rb
matheus-0.2.0 lib/matheus/result.rb
matheus-0.1.0 lib/matheus/result.rb