简体   繁体   中英

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).
Is it fine to use @context instead of context in this situation?

Ref 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 :

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 MyInteractor
  include Interactor

  def my_method
    @context
    # is the exact same as 
    context
    # except for the method call
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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