Sha256: 2ad6d2005718e9994aaaf389db60e000c8cabd24973fc7a6a1f52fa227f3e11d

Contents?: true

Size: 875 Bytes

Versions: 2

Compression:

Stored size: 875 Bytes

Contents

# frozen_string_literal: true

module Micro
  module Service
    class Result
      InvalidType = TypeError.new('type must be a symbol'.freeze)

      attr_reader :value, :type

      def __set__(is_success, value, type)
        raise InvalidType unless type.is_a?(Symbol)

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

        self
      end

      def success?
        @success
      end

      def failure?
        !success?
      end

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

      def on_failure(arg = :error)
        self.tap{ yield(value) if failure_type?(arg) }
      end

      private

        def success_type?(arg)
          success? && (arg == :ok || arg == type)
        end

        def failure_type?(arg)
          failure? && (arg == :error || arg == type)
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
u-service-0.13.1 lib/micro/service/result.rb
u-service-0.13.0 lib/micro/service/result.rb