What is a regular service?

  • A regular service is a class with one responsibility - to calculate and return a result of a logical operation. It implements result directly and has no steps.

    class FindUser
      include ConvenientService::Standard::Config
    
      attr_reader :id
    
      def initialize(id:)
        @id = id
      end
    
      def result
        return error("Id is `nil`") if id.nil?
    
        users = {1 => {name: "John"}}
    
        return failure("User with id `#{id}` does not exist") unless users.key?(id)
    
        success(user: users[id])
      end
    end
    
    result = FindUser.result(id: 1)
    
    result.success?
    # => true
    
    result.data[:user]
    # => {name: "John"}
  • A regular service is contrasted with an organizer service, which composes other services via steps instead of implementing the logic directly.

See also

Sources