Sha256: 55b3ed99eb757537e6bfab67b936cf8669a378d26768e746809c6fe1af30a9d0

Contents?: true

Size: 1006 Bytes

Versions: 5

Compression:

Stored size: 1006 Bytes

Contents

# frozen_string_literal: true

module Pathway
  class Result
    extend Forwardable
    attr_reader :value, :error

    class Success < Result
      def initialize(value)
        @value = value
      end

      def success?
        true
      end

      def then(bl=nil)
        result(block_given? ? yield(value): bl.call(value))
      end

      def tee(bl=nil, &block)
        follow = self.then(bl, &block)
        follow.failure? ? follow : self
      end
    end

    class Failure < Result
      def initialize(error)
        @error = error
      end

      def success?
        false
      end

      def then(_=nil)
        self
      end

      def tee(_=nil)
        self
      end
    end

    def failure?
      !success?
    end

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

    def self.failure(error)
      Failure.new(error)
    end

    def self.result(object)
      object.is_a?(Result) ? object : success(object)
    end

    delegate :result => 'self.class'
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pathway-0.12.0 lib/pathway/result.rb
pathway-0.11.3 lib/pathway/result.rb
pathway-0.11.2 lib/pathway/result.rb
pathway-0.11.1 lib/pathway/result.rb
pathway-0.11.0 lib/pathway/result.rb