Skip to main content

Boolean Service

  • Boolean service is a service that confirms/denies the concrete assumption.

  • Boolean service is a service that answers to the specific question.

  • Boolean service is a service whose success result has no data.

success result from the boolean service confirms the assumption stated in its name.

In other words, it answers positively to the question.

failure result from the boolean service tells why exactly the assumption is NOT accurate.

In other words, it answers negatively to the question + provides the reason.

error result explains why there is no even hypothetical opportunity to confirm/deny the assumption.

In other words, it gives no answer to the question at all.

Here are some examples of such services:

class IsEmployeeFree
attr_reader :employee

def initialize(employee:)
@employee = employee
end

def result
return error("Employee can't be blank") if employee.blank?
return failure("Employee `#{employee.id}` has task `#{employee.tasks.find(&:in_progress?).id}` in progress") if employee.tasks.any?(&:in_progress?)

success
end
end

IsEmployeeFree assumes that an employee is available.

The following command either confirms or denies that assumption.

IsEmployeeFree.success?(employee: employee)
class IsTaskUnassigned
attr_reader :task

def initialize(task:)
@task = task
end

def result
return error("Task can't be blank") if task.blank?
return failure("Task `#{task.id}` has employee `#{task.employee}`") if task.employee.present?

success
end
end

IsTaskUnassigned asks a question - is a task not assigned to anyone?

The expression below answers it either positively or negatively.

IsEmployeeFree.success?(employee: employee)

Also as with any other type of service, the boolean service can be triggered via the result method or a step:

result = IsEmployeeFree.result(employee: employee)
class AssignTaskToEmployee
include ConvenientService::Standard::Config

attr_reader :task, :employee

step IsTaskUnassigned,
in: :task

step IsEmployeeFree,
in: :employee

def initialize(task:, employee:)
@task = task
@employee = employee
end

def result
task.employee = employee

saved = task.save

return failure("Employee `#{employee.id}` is NOT saved to task `#{task.id}`") unless saved

success
end
end