class Module
def thread_mattr_writer(*syms) # :nodoc:
Current.new.user = "DHH" # => NoMethodError
end
thread_mattr_writer :user, instance_writer: false
class Current
instance_writer: false or instance_accessor: false.
If you want to opt out the instance writer method, pass
Thread.current[:attr_Current_user] # => "DHH"
Current.user = "DHH"
end
thread_mattr_writer :user
module Current
allow assignment to the attribute.
Defines a per-thread class attribute and creates a class and instance writer methods to
def thread_mattr_writer(*syms) # :nodoc: options = syms.extract_options! syms.each do |sym| raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) def self.#{sym}=(obj) Thread.current["attr_"+ name + "_#{sym}"] = obj end EOS unless options[:instance_writer] == false || options[:instance_accessor] == false class_eval(<<-EOS, __FILE__, __LINE__ + 1) def #{sym}=(obj) Thread.current["attr_"+ self.class.name + "_#{sym}"] = obj end EOS end end end