Sha256: 5ebbbdce2bfb95fb15cb711b3f47c89af246ae66badb03269e0eb2231dc276cf

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Micro
  module Case
    class Result
      attr_reader :value, :type

      def __set__(is_success, value, type, use_case)
        raise Error::InvalidResultType unless type.is_a?(Symbol)
        raise Error::InvalidUseCase if !is_success && !is_an_use_case?(use_case)

        @success, @value, @type, @use_case = is_success, value, type, use_case

        self
      end

      def success?
        @success
      end

      def failure?
        !success?
      end

      def use_case
        return @use_case if failure?

        raise Error::InvalidAccessToTheUseCaseObject
      end

      def on_success(arg = nil)
        self.tap { yield(value) if success_type?(arg) }
      end

      def on_failure(arg = nil)
        self.tap{ yield(value, @use_case) if failure_type?(arg) }
      end

      private

        def success_type?(arg)
          success? && (arg.nil? || arg == type)
        end

        def failure_type?(arg)
          failure? && (arg.nil? || arg == type)
        end

        def is_an_use_case?(arg)
          (arg.is_a?(Class) && arg < Case::Base) || arg.is_a?(Case::Base)
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
u-case-1.1.0 lib/micro/case/result.rb
u-case-1.0.0 lib/micro/case/result.rb
u-case-1.0.0.rc1 lib/micro/case/result.rb