設定
configurationメソッドを使用してサービスを設定します。通常はベースクラスに配置します。
設定例
例外の設定
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
end結果の設定 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
endコレクションモード
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
collection_mode_class_names([ActiveRecord::Relation])
end
end
endハッシュモード
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
hash_mode_class_names([CustomHash])
end
end
endinputのヘルパー
mustおよびprepareオプションに基づくinputのカスタムヘルパーです。
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
endprepareの例
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
endinternalのヘルパー
mustオプションに基づくinternalのカスタムヘルパーです。
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
endoutputのヘルパー
mustオプションに基づくoutputのカスタムヘルパーです。
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
endmakeのエイリアス
action_aliases設定を使用してmakeの代替を追加します。
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
action_aliases %i[execute]
end
end
endmakeのカスタマイズ
action_shortcuts設定を使用してmakeのショートカットを実装します。
値はmakeを置き換え、インスタンスメソッドのプレフィックスとして機能します。
シンプルモード
シンプルモードでは、値はシンボルの配列として渡されます。
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
action_shortcuts(
%i[assign perform]
)
end
end
end使用例
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
# ...
endアドバンスドモード 2.14.0以降
アドバンスドモードでは、値はハッシュとして渡されます。
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
end使用例
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
endプレディケートメソッド 2.5.0以降
すべてのアトリビュートに対するプレディケートメソッドはデフォルトで有効です。 必要に応じて無効にしてください。
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
predicate_methods_enabled false
end
end
endI18nのルートキー 2.6.0以降
翻訳のデフォルトのルートキー(servactory)をオーバーライドします。
ruby
module ApplicationService
class Base < Servactory::Base
configuration do
i18n_root_key :my_app
end
end
end翻訳の検索がservactory.*からmy_app.*に変更されます。
国際化(I18n)も参照してください。