[英]RoR - how can I refer to 'old' self object when self's context changes inside a method
[英]Can I use the context as @context inside an interactor class?
在某些交互器中,定义了几个函数以在调用 function 中使用(我没有使用实用程序来组织此类函数)。
在这种情况下使用@context
而不是context
可以吗?
参考 URL: https://github.com/collectiveidea/interactor
谢谢你的帮助
从源代码context
中可以看出,它只是attr_reader
声明的一个简单的访问器(getter 方法):
module Interactor
# Internal: Install Interactor's behavior in the given class.
def self.included(base)
base.class_eval do
extend ClassMethods
include Hooks
# Public: Gets the Interactor::Context of the Interactor instance.
attr_reader :context
end
end
因此,只要您从包含此模块的 class 中访问实例变量,直接访问实例变量 ( @context
) 和通过context
方法几乎没有明显区别。
class MyInteractor
include Interactor
def my_method
@context
# is the exact same as
context
# except for the method call
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.