Class: Dry::Transaction::Extra::Steps::Tap

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/transaction/extra/steps/tap.rb

Overview

A step that mimics Ruby’s builtin [Kernel#tap](ruby-doc.org/3.1.2/Kernel.html#method-i-tap) method. If the step succeeds, the step output is ignored and the original input is returned. However, if the step fails, then that Failure is returned instead.

Examples:


tap :track_user
map :next_step

def track_user(user)
  response = Tracker.track(user_id: user.email)
  return Failure(response.body) if response.status >= 400
end

def next_step(user)
  # Normally, the return value if the previous step would be passed
  # as the input to this step. In this case, we don't care, we want
  # to keep going with the original input `user`.
end

Instance Method Summary collapse

Instance Method Details

#call(operation, _options, args) ⇒ Object



31
32
33
34
35
36
# File 'lib/dry/transaction/extra/steps/tap.rb', line 31

def call(operation, _options, args)
  result = operation.call(*args)
  return result if result.is_a?(Failure)

  Success(*args)
end