Skip to main content
Version: 1.6

Configuration

Services are configured through the configuration method, which can be placed, for example, in the base class.

Configuration examples

Errors

app/services/application_service/base.rb
module ApplicationService
class Base < Servactory::Base
configuration do
input_error_class ApplicationService::Errors::InputError
output_error_class ApplicationService::Errors::OutputError
internal_error_class ApplicationService::Errors::InternalError

failure_class ApplicationService::Errors::Failure
end
end
end

Helpers for input

Custom helpers for input are based on the must and prepare options.

Example with must

app/services/application_service/base.rb
module ApplicationService
class Base < Servactory::Base
configuration do
input_option_helpers(
[
Servactory::Inputs::OptionHelper.new(
name: :must_be_6_characters,
equivalent: {
must: {
be_6_characters: {
is: ->(value:) { value.all? { |id| id.size == 6 } },
message: lambda do |input:, **|
"Wrong IDs in `#{input.name}`"
end
}
}
}
)
]
)
end
end
end

Example with prepare

app/services/application_service/base.rb
module ApplicationService
class Base < Servactory::Base
configuration do
input_option_helpers(
[
Servactory::Inputs::OptionHelper.new(
name: :to_money,
equivalent: {
prepare: ->(value:) { Money.new(cents: value, currency: :USD) }
}
)
]
)
end
end
end

Method shortcuts

app/services/application_service/base.rb
module ApplicationService
class Base < Servactory::Base
configuration do
method_shortcuts %i[assign perform]
end
end
end