mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Merge pull request #412 from strzalek/thread-safe-config
Make Config class thread safe.
This commit is contained in:
commit
3efea817b6
@ -1,35 +1,48 @@
|
|||||||
|
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)
|
||||||
@data[key.to_s]
|
@mutex.synchronize do
|
||||||
|
@data[key.to_s]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def []=(key, value)
|
def []=(key, value)
|
||||||
@data[key.to_s] = value
|
@mutex.synchronize do
|
||||||
|
@data[key.to_s] = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
@data.each(&block)
|
@mutex.synchronize do
|
||||||
|
@data.each(&block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear
|
def clear
|
||||||
@data.clear
|
@mutex.synchronize do
|
||||||
|
@data.clear
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(name, *args)
|
def method_missing(name, *args)
|
||||||
name = name.to_s
|
@mutex.synchronize do
|
||||||
return @data[name] if @data.include?(name)
|
name = name.to_s
|
||||||
match = name.match(/\A(.*?)([?=]?)\Z/)
|
return @data[name] if @data.include?(name)
|
||||||
case match[2]
|
match = name.match(/\A(.*?)([?=]?)\Z/)
|
||||||
when "="
|
case match[2]
|
||||||
@data[match[1]] = args.first
|
when "="
|
||||||
when "?"
|
@data[match[1]] = args.first
|
||||||
!!@data[match[1]]
|
when "?"
|
||||||
|
!!@data[match[1]]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user