私有服务对象方法 (Ruby + Rails)
Private service object methods (Ruby + Rails)
我有一个 PaymentManager
服务:
/payment_manager/
- online_payment_creator.rb
- manual_payment_creator.rb
- payment_splitter.rb
3 项服务的代码:
module PaymentManager
class ManualPaymentCreator < ApplicationService
def call
# do stuff in transaction
PaymentSplitter.call(a, b, c)
end
end
end
module PaymentManager
class OnlinePaymentCreator < ApplicationService
# do stuff in transaction
PaymentSplitter.call(a2, b2, c2)
end
end
module PaymentManager
class PaymentSplitter < ApplicationService
def call(x, y, z)
# do very dangerous stuff
end
end
end
因此,PaymentSplitter
本质上是将付款拆分到多张发票中,并且必须 仅 在其他 2 项服务中的任何一项中完成,但绝不能调用它自己的(来自控制器或控制台等)。
我通常会在 ManualPaymentCreator
中创建一个 private
方法,但是因为这两个服务都需要它,所以我不知道该怎么做,因为我不能使用简单的私有实例没有重复的方法。
也许您正在寻找继承和受保护的方法
class Foo < ApplicationService
def public_method
end
protected
#everything after here is private to this class and its descendants
def some_method_to_be_shared_with_descendants
#I am only accessible from this class and from descendants of this class
...
end
private
#Everything after here is private to this class only
def some_private_methods_that_are_only_accessible_from_this_class
end
...
end
然后子孙类这样
class Bar < Foo
def do_something
# can access protected methods from Foo class here as native methods
some_res = some_method_to_be_shared_here
# do something with some_res
end
end
所以你应该从 PaymentSplitter 继承你的其他 2 类 并将共享方法设置为受保护
也许包含在模块中的私有 class 可能会更好,例如。
module PaymentSplitting
def self.included(klass)
raise 'Error' unless klass.in?([ManualPaymentCreator, OnlinePaymentCreator])
end
class PaymentSplitter
end
private_constant :PaymentSplitter
end
这应该允许您在包含 PaymentSplitting
的任何 class 中自由引用 PaymentSplitter
,并且您只能在 ManualPaymentCreator
或 OnlinePaymentCreator
。引用 PaymentSplitting::PaymentSplitter
将抛出 NameError: private constant
异常。
我有一个 PaymentManager
服务:
/payment_manager/
- online_payment_creator.rb
- manual_payment_creator.rb
- payment_splitter.rb
3 项服务的代码:
module PaymentManager
class ManualPaymentCreator < ApplicationService
def call
# do stuff in transaction
PaymentSplitter.call(a, b, c)
end
end
end
module PaymentManager
class OnlinePaymentCreator < ApplicationService
# do stuff in transaction
PaymentSplitter.call(a2, b2, c2)
end
end
module PaymentManager
class PaymentSplitter < ApplicationService
def call(x, y, z)
# do very dangerous stuff
end
end
end
因此,PaymentSplitter
本质上是将付款拆分到多张发票中,并且必须 仅 在其他 2 项服务中的任何一项中完成,但绝不能调用它自己的(来自控制器或控制台等)。
我通常会在 ManualPaymentCreator
中创建一个 private
方法,但是因为这两个服务都需要它,所以我不知道该怎么做,因为我不能使用简单的私有实例没有重复的方法。
也许您正在寻找继承和受保护的方法
class Foo < ApplicationService
def public_method
end
protected
#everything after here is private to this class and its descendants
def some_method_to_be_shared_with_descendants
#I am only accessible from this class and from descendants of this class
...
end
private
#Everything after here is private to this class only
def some_private_methods_that_are_only_accessible_from_this_class
end
...
end
然后子孙类这样
class Bar < Foo
def do_something
# can access protected methods from Foo class here as native methods
some_res = some_method_to_be_shared_here
# do something with some_res
end
end
所以你应该从 PaymentSplitter 继承你的其他 2 类 并将共享方法设置为受保护
也许包含在模块中的私有 class 可能会更好,例如。
module PaymentSplitting
def self.included(klass)
raise 'Error' unless klass.in?([ManualPaymentCreator, OnlinePaymentCreator])
end
class PaymentSplitter
end
private_constant :PaymentSplitter
end
这应该允许您在包含 PaymentSplitting
的任何 class 中自由引用 PaymentSplitter
,并且您只能在 ManualPaymentCreator
或 OnlinePaymentCreator
。引用 PaymentSplitting::PaymentSplitter
将抛出 NameError: private constant
异常。