ruby-on-rails - 我可以在交互器类中将上下文用作@context 吗?
问题描述
在一些交互器中,定义了几个函数用于调用函数(我没有使用实用程序来组织这些函数)。在这种情况下
使用@context
而不是可以吗?
参考网址:https ://github.com/collectiveidea/interactor
感谢您的帮助context
解决方案
正如您从源代码中看到的那样,context
它只是一个简单的访问器(getter 方法),声明如下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
@context
因此,context
只要您从包含该模块的类中访问实例变量,直接访问实例变量(
class MyInteractor
include Interactor
def my_method
@context
# is the exact same as
context
# except for the method call
end
end
推荐阅读
- python - 在 Python 中使用 Wordnet 进行语义相似性
- python - 从静态字段生成的 Django F 日期表达式
- python - 如何扩展此正则表达式以匹配备选方案和分组?
- javascript - 无法在 React 表单中提交所有数据
- typescript - 确保字符串文字联合属于对象的键
- powerbi - 带有分发列表的 Power BI RLS 不起作用
- c - 使用 OpenSSL 进行高性能 HMAC 计算
- flutter - 在屏幕顶部颤动BottomSheet?
- amazon-ec2 - grafana ec2-instance 按标签过滤
- html - 将脚本生成的 HTML 发送到具有正确表格边距的 Google Doc