class Module
def thread_mattr_reader(*syms) # :nodoc:
Current.new.user # => NoMethodError
end
thread_mattr_reader :user, instance_reader: false
class Current
instance_reader: false or instance_accessor: false.
If you want to opt out the creation on the instance reader method, pass
# => NameError: invalid attribute name: 1_Badname
end
thread_mattr_reader :"1_Badname"
module Foo
The attribute name must be a valid method name in Ruby.
Current.user # => "DHH"
Thread.current[:attr_Current_user] = "DHH"
Current.user # => nil
end
thread_mattr_reader :user
module Current
The underlying per-thread class variable is set to +nil+, if it is not previously defined.
Defines a per-thread class attribute and creates class and instance reader methods.
def thread_mattr_reader(*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} Thread.current["attr_"+ name + "_#{sym}"] end EOS unless options[:instance_reader] == false || options[:instance_accessor] == false class_eval(<<-EOS, __FILE__, __LINE__ + 1) def #{sym} Thread.current["attr_"+ self.class.name + "_#{sym}"] end EOS end end end