@@ -9,12 +9,12 @@ def unformat(arg)
9
9
arg
10
10
end
11
11
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 )
14
14
end
15
15
16
16
def formatter_for ( format )
17
- @@format_to_formatter_cache . calc ( format )
17
+ " #{ format . to_s . camelize } Formatter" . safe_constantize
18
18
end
19
19
end
20
20
end
@@ -53,13 +53,32 @@ def unformat(value)
53
53
super ( value )
54
54
end
55
55
56
- @@value_type_to_formatter_cache = JSONAPI :: NaiveCache . new do | type |
56
+ def value_formatter_for ( type )
57
57
"#{ type . to_s . camelize } ValueFormatter" . safe_constantize
58
58
end
59
+ end
60
+ end
59
61
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
63
82
end
64
83
end
65
84
end
@@ -69,38 +88,24 @@ class UnderscoredKeyFormatter < JSONAPI::KeyFormatter
69
88
70
89
class CamelizedKeyFormatter < JSONAPI ::KeyFormatter
71
90
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
-
79
91
def format ( key )
80
- @@format_cache . calc ( key )
92
+ super . camelize ( :lower )
81
93
end
82
94
83
95
def unformat ( formatted_key )
84
- @@unformat_cache . calc ( formatted_key )
96
+ formatted_key . to_s . underscore
85
97
end
86
98
end
87
99
end
88
100
89
101
class DasherizedKeyFormatter < JSONAPI ::KeyFormatter
90
102
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
-
98
103
def format ( key )
99
- @@format_cache . calc ( key )
104
+ super . underscore . dasherize
100
105
end
101
106
102
107
def unformat ( formatted_key )
103
- @@unformat_cache . calc ( formatted_key )
108
+ formatted_key . to_s . underscore
104
109
end
105
110
end
106
111
end
@@ -127,38 +132,24 @@ class UnderscoredRouteFormatter < JSONAPI::RouteFormatter
127
132
128
133
class CamelizedRouteFormatter < JSONAPI ::RouteFormatter
129
134
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
-
137
135
def format ( route )
138
- @@format_cache . calc ( route )
136
+ super . camelize ( :lower )
139
137
end
140
138
141
139
def unformat ( formatted_route )
142
- @@unformat_cache . calc ( formatted_route )
140
+ formatted_route . to_s . underscore
143
141
end
144
142
end
145
143
end
146
144
147
145
class DasherizedRouteFormatter < JSONAPI ::RouteFormatter
148
146
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
-
156
147
def format ( route )
157
- @@format_cache . calc ( route )
148
+ super . dasherize
158
149
end
159
150
160
151
def unformat ( formatted_route )
161
- @@unformat_cache . calc ( formatted_route )
152
+ formatted_route . to_s . underscore
162
153
end
163
154
end
164
155
end
0 commit comments