如何解决我可以在交互器类中将上下文用作@context吗?
在某些交互器中,调用函数中定义了几个函数(我没有使用实用程序来组织此类函数)。
在这种情况下,使用@context
代替context
是否合适?
引用网址: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
因此,直接访问实例变量(@context
)与通过context
方法访问实例变量之间几乎没有明显的区别,只要您是从包含此模块的类中进行访问即可。 / p>
class MyInteractor
include Interactor
def my_method
@context
# is the exact same as
context
# except for the method call
end
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。