Sha256: 1a36ce40ae35481f8b30ff44561e577584508987e3e36f7189cf953241a5cc5b

Contents?: true

Size: 708 Bytes

Versions: 1

Compression:

Stored size: 708 Bytes

Contents

require "thread_local_accessor/version"

# An idea from http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/
class Class
  def thread_local_accessor name, options = {}
    m = Module.new
    m.module_eval %{
      def #{name}
        k = ((Class === self ? self : self.class).object_id.to_s + '_#{name}').to_sym
        if Thread.current.key?(k)
          Thread.current[k]
        else
          #{options[:default].inspect}
        end
      end
 
      def #{name}=(val)
        k = ((Class === self ? self : self.class).object_id.to_s + '_#{name}').to_sym
        Thread.current[k] = val
      end
    }
 
    class_eval do
      include m
      extend m
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thread_local_accessor-0.1.0 lib/thread_local_accessor.rb