@@ -9,12 +9,12 @@ def unformat(arg)
99 arg
1010 end
1111
12- @@format_to_formatter_cache = JSONAPI :: NaiveCache . new do | format |
13- " #{ format . to_s . camelize } Formatter" . safe_constantize
12+ def cached
13+ return FormatterWrapperCache . new ( self )
1414 end
1515
1616 def formatter_for ( format )
17- @@format_to_formatter_cache . calc ( format )
17+ " #{ format . to_s . camelize } Formatter" . safe_constantize
1818 end
1919 end
2020 end
@@ -53,13 +53,32 @@ def unformat(value)
5353 super ( value )
5454 end
5555
56- @@value_type_to_formatter_cache = JSONAPI :: NaiveCache . new do | type |
56+ def value_formatter_for ( type )
5757 "#{ type . to_s . camelize } ValueFormatter" . safe_constantize
5858 end
59+ end
60+ end
5961
60- def value_formatter_for ( type )
61- @@value_type_to_formatter_cache . calc ( type )
62- end
62+ # Warning: Not thread-safe. Wrap in ThreadLocalVar as needed.
63+ class FormatterWrapperCache
64+ attr_reader :formatter_klass
65+
66+ def initialize ( formatter_klass )
67+ @formatter_klass = formatter_klass
68+ @format_cache = NaiveCache . new { |arg | formatter_klass . format ( arg ) }
69+ @unformat_cache = NaiveCache . new { |arg | formatter_klass . unformat ( arg ) }
70+ end
71+
72+ def format ( arg )
73+ @format_cache . get ( arg )
74+ end
75+
76+ def unformat ( arg )
77+ @unformat_cache . get ( arg )
78+ end
79+
80+ def cached
81+ self
6382 end
6483 end
6584end
@@ -69,38 +88,24 @@ class UnderscoredKeyFormatter < JSONAPI::KeyFormatter
6988
7089class CamelizedKeyFormatter < JSONAPI ::KeyFormatter
7190 class << self
72- @@format_cache = JSONAPI ::NaiveCache . new do |key |
73- key . to_s . camelize ( :lower )
74- end
75- @@unformat_cache = JSONAPI ::NaiveCache . new do |formatted_key |
76- formatted_key . to_s . underscore
77- end
78-
7991 def format ( key )
80- @@format_cache . calc ( key )
92+ super . camelize ( :lower )
8193 end
8294
8395 def unformat ( formatted_key )
84- @@unformat_cache . calc ( formatted_key )
96+ formatted_key . to_s . underscore
8597 end
8698 end
8799end
88100
89101class DasherizedKeyFormatter < JSONAPI ::KeyFormatter
90102 class << self
91- @@format_cache = JSONAPI ::NaiveCache . new do |key |
92- key . to_s . underscore . dasherize
93- end
94- @@unformat_cache = JSONAPI ::NaiveCache . new do |formatted_key |
95- formatted_key . to_s . underscore
96- end
97-
98103 def format ( key )
99- @@format_cache . calc ( key )
104+ super . underscore . dasherize
100105 end
101106
102107 def unformat ( formatted_key )
103- @@unformat_cache . calc ( formatted_key )
108+ formatted_key . to_s . underscore
104109 end
105110 end
106111end
@@ -127,38 +132,24 @@ class UnderscoredRouteFormatter < JSONAPI::RouteFormatter
127132
128133class CamelizedRouteFormatter < JSONAPI ::RouteFormatter
129134 class << self
130- @@format_cache = JSONAPI ::NaiveCache . new do |route |
131- route . to_s . camelize ( :lower )
132- end
133- @@unformat_cache = JSONAPI ::NaiveCache . new do |formatted_route |
134- formatted_route . to_s . underscore
135- end
136-
137135 def format ( route )
138- @@format_cache . calc ( route )
136+ super . camelize ( :lower )
139137 end
140138
141139 def unformat ( formatted_route )
142- @@unformat_cache . calc ( formatted_route )
140+ formatted_route . to_s . underscore
143141 end
144142 end
145143end
146144
147145class DasherizedRouteFormatter < JSONAPI ::RouteFormatter
148146 class << self
149- @@format_cache = JSONAPI ::NaiveCache . new do |route |
150- route . to_s . dasherize
151- end
152- @@unformat_cache = JSONAPI ::NaiveCache . new do |formatted_route |
153- formatted_route . to_s . underscore
154- end
155-
156147 def format ( route )
157- @@format_cache . calc ( route )
148+ super . dasherize
158149 end
159150
160151 def unformat ( formatted_route )
161- @@unformat_cache . calc ( formatted_route )
152+ formatted_route . to_s . underscore
162153 end
163154 end
164155end
0 commit comments