繁体   English   中英

我可以在交互器 class 中将上下文用作 @context 吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM