简体   繁体   English

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

[英]Can I use the context as @context inside an interactor class?

In some interactors, several functions are defined to be used in call function (I'm not using utility for organize such functions).在某些交互器中,定义了几个函数以在调用 function 中使用(我没有使用实用程序来组织此类函数)。
Is it fine to use @context instead of context in this situation?在这种情况下使用@context而不是context可以吗?

Ref URL: https://github.com/collectiveidea/interactor参考 URL: https://github.com/collectiveidea/interactor

Thanks for any help谢谢你的帮助

As you can see from the source code context is just a simple accessor (getter method) declared by attr_reader :从源代码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

There is thus almost no discernible difference between accessing the instance variable directly ( @context ) and through the context method as long as you're accessing it from within the class that includes this module.因此,只要您从包含此模块的 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.

相关问题 RoR-当方法中自身的上下文发生变化时,如何引用“旧的”自身对象 - RoR - how can I refer to 'old' self object when self's context changes inside a method Rails交互器:如何获得即时消息? - Rails interactor : how can I have flash messages? 如何在测试中使用上下文? - how to use the context in the tests? 在 GraphQL 模式中获取上下文 - Get context inside a GraphQL schema 在函数内创建RSpec上下文 - Create RSpec context inside a function Ruby:我可以在类方法中使用实例方法吗? - Ruby: Can I use instance methods inside a class method? 在Rails的上下文中,如何在第二秒之前对列进行排序? - In the context of rails, how can I order columns by the second? 我可以在 GraphQL 请求标头中设置默认上下文吗? - Can I set a default context in an GraphQL request header? 如何从类方法内部添加实例方法,该类方法接受要在实例上下文中执行的块 - How to add an instance method from inside of a class method which accepts a block to be executed in the context of the instance 如何使“ self”在Mixin模块内部引用我的类(即使在方法的上下文之外声明)? - How to get the `self` to refer to a my class inside a Mixin module (even if it is stated outside the context of a method)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM