Getting started with Servactory
Conventions
- Services inherit from
Servactory::Baseand reside inapp/services. Common practice: createApplicationService::Baseas your project's base class. - Name services by what they do, not what they accept. Use verbs. Example:
UsersService::Createinstead ofUsersService::Creation.
Version support
| Ruby/Rails | 8.1 | 8.0 | 7.2 | 7.1 | 7.0 | 6.1 | 6.0 | 5.2 | 5.1 | 5.0 |
|---|---|---|---|---|---|---|---|---|---|---|
| 4.0 | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| 3.4 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| 3.3 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| 3.2 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| 3.1 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
Installation
Add this to Gemfile:
ruby
gem "servactory"And execute:
shell
bundle installPreparation
First, prepare the base class for inheritance.
Automatically Since 2.5.0
Run the generator:
shell
bundle exec rails g servactory:installThis creates all necessary files.
Manually
ApplicationService::Exceptions
ruby
module ApplicationService
module Exceptions
class Input < Servactory::Exceptions::Input; end
class Output < Servactory::Exceptions::Output; end
class Internal < Servactory::Exceptions::Internal; end
class Failure < Servactory::Exceptions::Failure; end
end
endApplicationService::Result Since 2.5.0
ruby
module ApplicationService
class Result < Servactory::Result; end
endApplicationService::Base
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
input_exception_class ApplicationService::Exceptions::Input
internal_exception_class ApplicationService::Exceptions::Internal
output_exception_class ApplicationService::Exceptions::Output
failure_class ApplicationService::Exceptions::Failure
result_class ApplicationService::Result
end
end
endFirst service
Create your first service:
shell
bundle exec rails g servactory:service users_service/create first_name middle_name last_nameGenerate a spec file:
shell
bundle exec rails g servactory:rspec users_service/create first_name middle_name last_name