Module: ZeevexThreadsafe::ThreadLocals::UtilityMethods
- Defined in:
- lib/zeevex_threadsafe/thread_locals.rb
Class Method Summary collapse
- .define_thread_local_accessors(base, args, block = nil) ⇒ Object
- .thread_local_hash(base, autocreate = false) ⇒ Object
Class Method Details
.define_thread_local_accessors(base, args, block = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/zeevex_threadsafe/thread_locals.rb', line 67 def self.define_thread_local_accessors(base, args, block = nil) = args[-1].instance_of?(Hash) ? args.pop : {} args.each do |name| key = name.to_sym base.class_eval do define_method name.to_sym do hash = ZeevexThreadsafe::ThreadLocals::UtilityMethods.thread_local_hash(self) return hash[key] if hash && hash.key?(key) return block.call(self, key) if block return [:default] end define_method (name.to_s + "=").to_sym do |value| ZeevexThreadsafe::ThreadLocals::UtilityMethods.thread_local_hash(self, true)[key] = value end if [:visibility] send [:visibility], name.to_sym, (name.to_s + "=").to_sym end end end end |
.thread_local_hash(base, autocreate = false) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/zeevex_threadsafe/thread_locals.rb', line 56 def self.thread_local_hash(base, autocreate = false) base.instance_eval do if autocreate @_thread_local_threads ||= {} @_thread_local_threads[Thread.current.object_id] ||= {} else @_thread_local_threads ? @_thread_local_threads[Thread.current.object_id] : nil end end end |