Merge pull request #412 from strzalek/thread-safe-config

Make Config class thread safe.
This commit is contained in:
Santiago Pastorino 2013-10-21 13:30:53 -07:00
commit 3efea817b6

View File

@ -1,27 +1,39 @@
require 'thread'
module ActiveModel module ActiveModel
class Serializer class Serializer
class Config class Config
def initialize def initialize
@data = {} @data = {}
@mutex = Mutex.new
end end
def [](key) def [](key)
@mutex.synchronize do
@data[key.to_s] @data[key.to_s]
end end
end
def []=(key, value) def []=(key, value)
@mutex.synchronize do
@data[key.to_s] = value @data[key.to_s] = value
end end
end
def each(&block) def each(&block)
@mutex.synchronize do
@data.each(&block) @data.each(&block)
end end
end
def clear def clear
@mutex.synchronize do
@data.clear @data.clear
end end
end
def method_missing(name, *args) def method_missing(name, *args)
@mutex.synchronize do
name = name.to_s name = name.to_s
return @data[name] if @data.include?(name) return @data[name] if @data.include?(name)
match = name.match(/\A(.*?)([?=]?)\Z/) match = name.match(/\A(.*?)([?=]?)\Z/)
@ -33,6 +45,7 @@ module ActiveModel
end end
end end
end end
end
CONFIG = Config.new CONFIG = Config.new
end end