簡體   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