Skip to main content

How to debug services via callbacks?

Use after result

after :result do |result|
binding.pry
end

Use after step

##
# Step is Result-like duck.
#
after :step do |step|
byebug
end

Use around result

after :result do |chain|
puts "before"

result = chain.yield

p "after"
end

Use after step

##
# Step is Result-like duck.
#
after :step do |step|
binding.break

step = chain.yield

print "after"
end

Real-world example

# frozen_string_literal: true

module Services
class AssertNpmPackageAvailable
include ConvenientService::Standard::Config

attr_reader :name

step :validate_name

step Services::AssertNodeAvailable

step Services::RunShell, in: {command: -> { "npm list #{name} --depth=0 > /dev/null 2>&1" }}

after :result do |result|
binding.pry
end

after :step do |step|
byebug
end

def initialize(name:)
@name = name
end

private

def validate_name
return error("Name is `nil`") if name.nil?
return error("Name is empty") if name.empty?

success
end
end
end