Thread.current
这段代码有什么用?我正在查看在Rails 应用程序中使用 DCI 的示例。在 lib/context.rb 中,有这样的:
module Context
include ContextAccessor
def context=(ctx)
Thread.current[:context] = ctx
end
def in_context
old_context = self.context
self.context = self
res = yield
self.context = old_context
res
end
end
它用于 app/contexts 中的各种上下文,例如:
def bid
in_context do
validator.validate
biddable.create_bid
end
#...
end
in_context
在块中运行代码并在当前线程上设置键值对有什么好处?