@@ -9,9 +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
14+ end
15+
1216 def formatter_for ( format )
13- formatter_class_name = "#{ format . to_s . camelize } Formatter"
14- formatter_class_name . safe_constantize
17+ @@format_to_formatter_cache . calc ( format )
1518 end
1619 end
1720 end
@@ -50,9 +53,12 @@ def unformat(value)
5053 super ( value )
5154 end
5255
56+ @@value_type_to_formatter_cache = JSONAPI ::NaiveCache . new do |type |
57+ "#{ type . to_s . camelize } ValueFormatter" . safe_constantize
58+ end
59+
5360 def value_formatter_for ( type )
54- formatter_name = "#{ type . to_s . camelize } Value"
55- formatter_for ( formatter_name )
61+ @@value_type_to_formatter_cache . calc ( type )
5662 end
5763 end
5864 end
@@ -63,24 +69,38 @@ class UnderscoredKeyFormatter < JSONAPI::KeyFormatter
6369
6470class CamelizedKeyFormatter < JSONAPI ::KeyFormatter
6571 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+
6679 def format ( key )
67- super . camelize ( :lower )
80+ @@format_cache . calc ( key )
6881 end
6982
7083 def unformat ( formatted_key )
71- formatted_key . to_s . underscore
84+ @@unformat_cache . calc ( formatted_key )
7285 end
7386 end
7487end
7588
7689class DasherizedKeyFormatter < JSONAPI ::KeyFormatter
7790 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+
7898 def format ( key )
79- super . underscore . dasherize
99+ @@format_cache . calc ( key )
80100 end
81101
82102 def unformat ( formatted_key )
83- formatted_key . to_s . underscore
103+ @@unformat_cache . calc ( formatted_key )
84104 end
85105 end
86106end
@@ -107,24 +127,38 @@ class UnderscoredRouteFormatter < JSONAPI::RouteFormatter
107127
108128class CamelizedRouteFormatter < JSONAPI ::RouteFormatter
109129 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+
110137 def format ( route )
111- super . camelize ( :lower )
138+ @@format_cache . calc ( route )
112139 end
113140
114141 def unformat ( formatted_route )
115- formatted_route . to_s . underscore
142+ @@unformat_cache . calc ( formatted_route )
116143 end
117144 end
118145end
119146
120147class DasherizedRouteFormatter < JSONAPI ::RouteFormatter
121148 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+
122156 def format ( route )
123- super . dasherize
157+ @@format_cache . calc ( route )
124158 end
125159
126160 def unformat ( formatted_route )
127- formatted_route . to_s . underscore
161+ @@unformat_cache . calc ( formatted_route )
128162 end
129163 end
130164end
0 commit comments