Wrap config's data hash in mutex

This commit is contained in:
Łukasz Strzałkowski 2013-10-21 20:15:06 +00:00
parent 55baaf2005
commit d46584af40

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