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,35 +1,48 @@
require 'thread'
module ActiveModel
class Serializer
class Config
def initialize
@data = {}
@mutex = Mutex.new
end
def [](key)
@data[key.to_s]
@mutex.synchronize do
@data[key.to_s]
end
end
def []=(key, value)
@data[key.to_s] = value
@mutex.synchronize do
@data[key.to_s] = value
end
end
def each(&block)
@data.each(&block)
@mutex.synchronize do
@data.each(&block)
end
end
def clear
@data.clear
@mutex.synchronize do
@data.clear
end
end
def method_missing(name, *args)
name = name.to_s
return @data[name] if @data.include?(name)
match = name.match(/\A(.*?)([?=]?)\Z/)
case match[2]
when "="
@data[match[1]] = args.first
when "?"
!!@data[match[1]]
@mutex.synchronize do
name = name.to_s
return @data[name] if @data.include?(name)
match = name.match(/\A(.*?)([?=]?)\Z/)
case match[2]
when "="
@data[match[1]] = args.first
when "?"
!!@data[match[1]]
end
end
end
end