Sha256: 15fb89e229b79f2d93b9d7a0b064438c19f81efe7e3bb96e0386bbbad71abcb1

Contents?: true

Size: 702 Bytes

Versions: 1

Compression:

Stored size: 702 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 do
      define_method "#{name}=" do |value|
        k = (Class === self ? self : self.class).object_id.to_s + "_" + name.to_s
        Thread.current[k] = value
      end

      define_method name do
        k = (Class === self ? self : self.class).object_id.to_s + "_" + name.to_s
        if Thread.current.key?(k)
          Thread.current[k]
        else
          options[:default]
        end
      end
    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.1 lib/thread_local_accessor.rb