require 'thread_variable/version'

module ThreadVariable
  def thread_variable *names
    names.each do |name|
      define_singleton_method :"#{name}" do
        namespace = (self.name || self.object_id.to_s).to_sym
        (Thread.current[namespace] ||= {})[name]
      end

      define_singleton_method :"#{name}=" do |val|
        namespace = (self.name || self.object_id.to_s).to_sym
        (Thread.current[namespace] ||= {})[name] = val
      end
    end
  end
end

require 'thread_variable/version'

module ThreadVariable
  def thread_variable *names
    names.each do |name|
      namespace = (self.name || self.object_id.to_s).to_sym

      define_singleton_method :"#{name}" do
        (Thread.current[namespace] ||= {})[name]
      end

      define_singleton_method :"#{name}=" do |val|
        (Thread.current[namespace] ||= {})[name] = val
      end
    end
  end
end

Define namespace outside of method to avoid recalculating
Define namespace outside of method to avoid recalculating

Since the method is defined as a closure, we can pre-calculate it and it
will always use that namespace. This should give a slight speed-up for
the getters and setters.
