Sha256: 040eb2c063767378258de685f4f011d58afb4ca8aeb5860eb38258fbc884fd02

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

require_relative "failed"

module Core
  module Operation
    # [public]
    #
    class Result
      include Core::Inspect
      inspects :value, :failure

      def initialize
        @value = nil
        @failure = nil
        @succeeded = false
        @failed = false
      end

      # [public]
      #
      attr_reader :failure

      # [public]
      #
      def value
        raise Failed.new(@failure) if failure?
        @value
      end

      # [public]
      #
      def succeeded(value)
        @value = value
        @succeeded = true
        finalize
      end

      # [public]
      #
      def failed(value)
        @failure = value
        @failed = true
        finalize
      end

      # [public]
      #
      def success?
        @succeeded == true
      end

      # [public]
      #
      def failure?
        @failed == true
      end

      # [public]
      #
      def finalize
        freeze
        self
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
core-operation-0.2.0 lib/core/operation/result.rb