@@ -9,9 +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
14
+ end
15
+
12
16
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 )
15
18
end
16
19
end
17
20
end
@@ -50,9 +53,12 @@ def unformat(value)
50
53
super ( value )
51
54
end
52
55
56
+ @@value_type_to_formatter_cache = JSONAPI ::NaiveCache . new do |type |
57
+ "#{ type . to_s . camelize } ValueFormatter" . safe_constantize
58
+ end
59
+
53
60
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 )
56
62
end
57
63
end
58
64
end
@@ -63,24 +69,38 @@ class UnderscoredKeyFormatter < JSONAPI::KeyFormatter
63
69
64
70
class CamelizedKeyFormatter < JSONAPI ::KeyFormatter
65
71
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
+
66
79
def format ( key )
67
- super . camelize ( :lower )
80
+ @@format_cache . calc ( key )
68
81
end
69
82
70
83
def unformat ( formatted_key )
71
- formatted_key . to_s . underscore
84
+ @@unformat_cache . calc ( formatted_key )
72
85
end
73
86
end
74
87
end
75
88
76
89
class DasherizedKeyFormatter < JSONAPI ::KeyFormatter
77
90
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
+
78
98
def format ( key )
79
- super . underscore . dasherize
99
+ @@format_cache . calc ( key )
80
100
end
81
101
82
102
def unformat ( formatted_key )
83
- formatted_key . to_s . underscore
103
+ @@unformat_cache . calc ( formatted_key )
84
104
end
85
105
end
86
106
end
@@ -107,24 +127,38 @@ class UnderscoredRouteFormatter < JSONAPI::RouteFormatter
107
127
108
128
class CamelizedRouteFormatter < JSONAPI ::RouteFormatter
109
129
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
+
110
137
def format ( route )
111
- super . camelize ( :lower )
138
+ @@format_cache . calc ( route )
112
139
end
113
140
114
141
def unformat ( formatted_route )
115
- formatted_route . to_s . underscore
142
+ @@unformat_cache . calc ( formatted_route )
116
143
end
117
144
end
118
145
end
119
146
120
147
class DasherizedRouteFormatter < JSONAPI ::RouteFormatter
121
148
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
+
122
156
def format ( route )
123
- super . dasherize
157
+ @@format_cache . calc ( route )
124
158
end
125
159
126
160
def unformat ( formatted_route )
127
- formatted_route . to_s . underscore
161
+ @@unformat_cache . calc ( formatted_route )
128
162
end
129
163
end
130
164
end
0 commit comments