What is Thread.current for in this code? I'm looking at this example of using DCI in a Rails application. In lib/context.rb, there's this:
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
which is used in the various contexts in app/contexts, e.g.:
def bid
in_context do
validator.validate
biddable.create_bid
end
#...
end
What is the benefit of running the code in the in_context block and setting a key-value pair on the current thread?
Generally, inside block you do not have an access to the caller’s context (besides closure variables.)
But with context you still might have an access to
instfrom inside block:So, one might introduce the
proc(consider bothAandBhaveContextincluded):Now external
procprhas virtually full access to its caller.