Sha256: 584700b2d784a7ee1c27137de56ee6affcdc77dce4c85efcb9cd016472f67853

Contents?: true

Size: 1023 Bytes

Versions: 3

Compression:

Stored size: 1023 Bytes

Contents

# frozen_string_literal: true

module Micro
  module Service
    class Result
      module Type
        INVALID = 'type must be nil or a symbol'.freeze

        def self.[](arg)
          return arg if arg.nil? || arg.is_a?(Symbol)
          raise TypeError, INVALID
        end
      end

      private_constant :Type

      include Micro::Attributes.with(:strict_initialize)

      def self.[](value:, type: nil)
        new(value: value, type: Type[type])
      end

      attributes :type, :value

      def success?
        raise NotImplementedError
      end

      def failure?
        !success?
      end

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

      def on_failure(arg=nil)
        self.tap{ yield(value) 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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
u-service-0.11.0 lib/micro/service/result.rb
u-service-0.10.0 lib/micro/service/result.rb
u-service-0.9.0 lib/micro/service/result.rb