-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathformatter.rb
More file actions
180 lines (150 loc) · 3.3 KB
/
formatter.rb
File metadata and controls
180 lines (150 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module JSONAPI
class Formatter
class << self
def format(arg)
arg.to_s
end
def unformat(arg)
arg
end
def cached
return FormatterWrapperCache.new(self)
end
def uncached
return self
end
def formatter_for(format)
"#{format.to_s.camelize}Formatter".safe_constantize
end
end
end
class KeyFormatter < Formatter
class << self
def format(key)
super
end
def unformat(formatted_key)
super
end
end
end
class RouteFormatter < Formatter
class << self
def format(route)
super
end
def unformat(formatted_route)
super
end
end
end
class ValueFormatter < Formatter
class << self
def format(raw_value)
super(raw_value)
end
def unformat(value)
super(value)
end
def value_formatter_for(type)
"#{type.to_s.camelize}ValueFormatter".safe_constantize
end
end
end
# Warning: Not thread-safe. Wrap in ThreadLocalVar as needed.
class FormatterWrapperCache
attr_reader :formatter_klass
def initialize(formatter_klass)
@formatter_klass = formatter_klass
@format_cache = NaiveCache.new{|arg| formatter_klass.format(arg) }
@unformat_cache = NaiveCache.new{|arg| formatter_klass.unformat(arg) }
end
def format(arg)
@format_cache.get(arg)
end
def unformat(arg)
@unformat_cache.get(arg)
end
def cached
self
end
def uncached
return @formatter_klass
end
end
end
class UnderscoredKeyFormatter < JSONAPI::KeyFormatter
class << self
def format(arg)
# take care of modules
arg.to_s.split('/').last
end
def unformat(arg, modules: [])
# take care of modules
modules.push(arg.to_s).join('/')
end
end
end
class CamelizedKeyFormatter < JSONAPI::KeyFormatter
class << self
def format(key)
super.camelize(:lower)
end
def unformat(formatted_key)
formatted_key.to_s.underscore
end
end
end
class DasherizedKeyFormatter < JSONAPI::KeyFormatter
class << self
def format(_key)
super.underscore.dasherize
end
def unformat(formatted_key)
formatted_key.to_s.underscore
end
end
end
class DefaultValueFormatter < JSONAPI::ValueFormatter
class << self
def format(raw_value)
case raw_value
when Date, Time, DateTime, ActiveSupport::TimeWithZone, BigDecimal
# Use the as_json methods added to various base classes by ActiveSupport
return raw_value.as_json
else
return raw_value
end
end
end
end
class IdValueFormatter < JSONAPI::ValueFormatter
class << self
def format(raw_value)
return if raw_value.nil?
raw_value.to_s
end
end
end
class UnderscoredRouteFormatter < JSONAPI::RouteFormatter
end
class CamelizedRouteFormatter < JSONAPI::RouteFormatter
class << self
def format(_route)
super.camelize(:lower)
end
def unformat(formatted_route)
formatted_route.to_s.underscore
end
end
end
class DasherizedRouteFormatter < JSONAPI::RouteFormatter
class << self
def format(_route)
super.dasherize
end
def unformat(formatted_route)
formatted_route.to_s.underscore
end
end
end