Configuration
Configure services via the configuration method, typically placed in the base class.
Configuration examples
For exceptions
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
end
end
endruby
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
endFor result Since 2.5.0
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
# ...
result_class ApplicationService::Result
end
end
endruby
module ApplicationService
class Result < Servactory::Result; end
endCollection mode
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
collection_mode_class_names([ActiveRecord::Relation])
end
end
endHash mode
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
hash_mode_class_names([CustomHash])
end
end
endHelpers for input
Base custom helpers for input on the must and prepare options.
Example with must
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
input_option_helpers(
[
Servactory::Maintenance::Attributes::OptionHelper.new(
name: :must_be_6_characters,
equivalent: {
must: {
be_6_characters: {
is: ->(value:, input:) { value.all? { |id| id.size == 6 } },
message: lambda do |input:, **|
"Wrong IDs in `#{input.name}`"
end
}
}
}
)
]
)
end
end
endExample with prepare
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
input_option_helpers(
[
Servactory::Maintenance::Attributes::OptionHelper.new(
name: :to_money,
equivalent: {
prepare: ->(value:) { Money.from_cents(value, :USD) }
}
)
]
)
end
end
endHelpers for internal
Base custom helpers for internal on the must option.
Example with must
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
internal_option_helpers(
[
Servactory::Maintenance::Attributes::OptionHelper.new(
name: :must_be_6_characters,
equivalent: {
must: {
be_6_characters: {
is: ->(value:, internal:) { value.all? { |id| id.size == 6 } },
message: lambda do |internal:, **|
"Wrong IDs in `#{internal.name}`"
end
}
}
}
)
]
)
end
end
endHelpers for output
Base custom helpers for output on the must option.
Example with must
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
output_option_helpers(
[
Servactory::Maintenance::Attributes::OptionHelper.new(
name: :must_be_6_characters,
equivalent: {
must: {
be_6_characters: {
is: ->(value:, output:) { value.all? { |id| id.size == 6 } },
message: lambda do |output:, **|
"Wrong IDs in `#{output.name}`"
end
}
}
}
)
]
)
end
end
endAliases for make
Add alternatives to make via action_aliases configuration.
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
action_aliases %i[execute]
end
end
endCustomization for make
Implement shortcuts for make via action_shortcuts configuration.
Values replace make and serve as prefix to the instance method.
Simple mode
In simple mode, values are passed as an array of symbols.
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
action_shortcuts(
%i[assign perform]
)
end
end
endExample of use
ruby
class CMSService::API::Posts::Create < CMSService::API::Base
# ...
assign :model
perform :request
private
def assign_model
# Build model for API request
end
def perform_request
# Perform API request
end
# ...
endAdvanced mode Since 2.14.0
In advanced mode, values are passed as a hash.
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
action_shortcuts(
%i[assign],
{
restrict: { # replacement for make
prefix: :create, # method name prefix
suffix: :restriction # method name suffix
}
}
)
end
end
endExample of use
ruby
class PaymentsService::Restrictions::Create < ApplicationService::Base
input :payment, type: Payment
# The exclamation mark will be moved to the end of the method name
restrict :payment!
private
def create_payment_restriction!
inputs.payment.restrictions.create!(
reason: "Suspicion of fraud"
)
end
endPredicate methods Since 2.5.0
Predicate methods for all attributes are enabled by default. Disable them if necessary.
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
predicate_methods_enabled false
end
end
end