Sha256: 1b0b6d0a5765d1802d79a5de77b0266cf2e0f13453e1892a53c7f878d6977442

Contents?: true

Size: 1016 Bytes

Versions: 1

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true

module Fluxus
  module Results
    module Chainable
      def then(klass, **kwargs)
        return self if failure?

        data ||= {}
        klass.call!(**data.merge(kwargs))
      end

      def on_success(expected_type = nil)
        yield(data) if __success_type?(expected_type)
        self
      end

      def on_failure(expected_type = nil)
        yield(data) if __failure_type?(expected_type)
        self
      end

      def on_exception(expected_exception = nil)
        return self unless __failure_type?(:exception)

        if expected_exception.nil? ||
           (expected_exception.is_a?(Exception) && data[:exception].is_a?(expected_exception))
          yield(data)
        end
        self
      end

      def __success_type?(expected_type)
        success? && (expected_type.nil? || expected_type == type)
      end

      def __failure_type?(expected_type = nil)
        failure? && (expected_type.nil? || expected_type == type)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluxus-1.0.0 lib/fluxus/results/chainable.rb