Skip to main content

Skip `result` call for boolean services

Let's check the following boolean service:

class IsConnectionActive
include ::ConvenientService::Standard::Config

attr_reader :connection

def initialize(:connection)
@connection = connection
end

def result
return error("Connection: can't be blank") if connection.blank?
return failure("Connection `#{connection.id}` is NOT connected") unless connection.connected?
return failure("Connection `#{connection.id}` has NO task") if connection.task.blank?
return failure("Connection `#{connection.id}` task is NOT in progress") unless connection.task.in_progres?

success
end
end

A common way to run a usual service is like this:

# okish
result = IsConnectionActive.result(connection: connection)

if result.success?
##
# `result.data` is processed somehow by the end-user here...
#
result.data
else

end

Most of the time you will probably work with result.data inside if result.success? branch.

But that is not the case for the boolean services.

Their success calls do NOT accept any data, that is why the previous chunk of code can be written in a shorter form:

# better
if IsConnectionActive.success?(connection: connection)

else

end