jat 0.0.3 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jat/attribute.rb +26 -5
- data/lib/jat/config.rb +4 -7
- data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
- data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
- data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
- data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
- data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
- data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
- data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
- data/lib/jat/plugins/cache/cache.rb +14 -6
- data/lib/jat/plugins/json_api/json_api.rb +150 -106
- data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
- data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
- data/lib/jat/plugins/json_api/lib/map.rb +92 -27
- data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
- data/lib/jat/plugins/json_api/lib/response.rb +81 -197
- data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
- data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
- data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
- data/lib/jat/plugins/{_json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
- data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
- data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
- data/lib/jat/plugins/preloads/preloads.rb +23 -0
- data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
- data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
- data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
- data/lib/jat/plugins/simple_api/simple_api.rb +87 -27
- data/lib/jat/plugins/to_str/to_str.rb +15 -5
- data/lib/jat/plugins/types/types.rb +54 -0
- data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
- data/lib/jat/plugins.rb +42 -30
- data/lib/jat.rb +40 -35
- data/test/lib/jat/attribute_test.rb +22 -4
- data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
- data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
- data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
- data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
- data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
- data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
- data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
- data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +38 -32
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -0
- data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
- data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
- data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
- data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
- data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
- data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
- data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +87 -0
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
- data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +90 -76
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -0
- data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
- data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
- data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
- data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
- data/test/lib/jat/plugins/types/types_test.rb +84 -0
- data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
- data/test/lib/jat/plugins_test.rb +88 -0
- data/test/lib/jat_test.rb +47 -24
- data/test/lib/plugin_test.rb +3 -3
- data/test/test_helper.rb +7 -6
- data/test/test_plugin.rb +9 -12
- metadata +107 -88
- data/CHANGELOG.md +0 -7
- data/README.md +0 -21
- data/jat.gemspec +0 -37
- data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
- data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
- data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
- data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
- data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
- data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
- data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
- data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
- data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
- data/lib/jat/presenter.rb +0 -51
- data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
- data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
- data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
- data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
- data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
- data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
- data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
- data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
- data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
- data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
- data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
- data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
- data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
- data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
- data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
- data/test/lib/jat/presenter_test.rb +0 -61
@@ -5,234 +5,118 @@ class Jat
|
|
5
5
|
module Plugins
|
6
6
|
module JsonApi
|
7
7
|
class Response
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def response
|
18
|
-
data, includes = data_with_includes
|
19
|
-
meta = document_meta
|
20
|
-
links = document_links
|
21
|
-
jsonapi = jsonapi_data
|
22
|
-
|
23
|
-
result = {}
|
24
|
-
result[:links] = links if links.any?
|
25
|
-
result[:data] = data if data
|
26
|
-
result[:included] = includes.values if includes.any?
|
27
|
-
result[:meta] = meta if meta.any?
|
28
|
-
result[:jsonapi] = jsonapi if jsonapi.any?
|
29
|
-
result
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def data_with_includes
|
35
|
-
includes = {}
|
36
|
-
data = many?(object, context) ? many(object, includes) : one(object, includes)
|
37
|
-
[data, includes]
|
38
|
-
end
|
39
|
-
|
40
|
-
def many(objs, includes)
|
41
|
-
objs.map { |obj| one(obj, includes) }
|
42
|
-
end
|
43
|
-
|
44
|
-
def one(obj, includes)
|
45
|
-
ResponseData.new(jat_class, obj, context, full_map, includes).data
|
46
|
-
end
|
47
|
-
|
48
|
-
def many?(data, context)
|
49
|
-
many = context[:many]
|
50
|
-
many.nil? ? data.is_a?(Enumerable) : many
|
51
|
-
end
|
52
|
-
|
53
|
-
def full_map
|
54
|
-
@full_map ||= jat.traversal_map.current
|
55
|
-
end
|
56
|
-
|
57
|
-
def jsonapi_data
|
58
|
-
data = context[:jsonapi]&.transform_keys(&:to_sym) || {}
|
59
|
-
jsonapi_data = jat_class.jsonapi_data
|
60
|
-
return data if jsonapi_data.empty?
|
61
|
-
|
62
|
-
presenter = jat_class::JsonapiPresenter.new(object, context)
|
63
|
-
jsonapi_data.each_key do |key|
|
64
|
-
data[key] = presenter.public_send(key) unless data.key?(key)
|
8
|
+
module ClassMethods
|
9
|
+
# Returns the Jat class that this Response class is namespaced under.
|
10
|
+
attr_accessor :jat_class
|
11
|
+
|
12
|
+
# Since Response is anonymously subclassed when Jat is subclassed,
|
13
|
+
# and then assigned to a constant of the Jat subclass, make inspect
|
14
|
+
# reflect the likely name for the class.
|
15
|
+
def inspect
|
16
|
+
"#{jat_class.inspect}::Response"
|
65
17
|
end
|
66
|
-
data.compact!
|
67
|
-
data
|
68
|
-
end
|
69
18
|
|
70
|
-
|
71
|
-
|
72
|
-
document_links = jat_class.document_links
|
73
|
-
return data if document_links.empty?
|
74
|
-
|
75
|
-
presenter = jat_class::DocumentLinksPresenter.new(object, context)
|
76
|
-
document_links.each_key do |key|
|
77
|
-
data[key] = presenter.public_send(key) unless data.key?(key)
|
19
|
+
def call(object, context)
|
20
|
+
new(object, context).to_h
|
78
21
|
end
|
79
|
-
data.compact!
|
80
|
-
data
|
81
22
|
end
|
82
23
|
|
83
|
-
|
84
|
-
|
85
|
-
document_meta = jat_class.added_document_meta
|
86
|
-
return data if document_meta.empty?
|
24
|
+
module InstanceMethods
|
25
|
+
attr_reader :jat_class, :object, :context
|
87
26
|
|
88
|
-
|
89
|
-
|
90
|
-
|
27
|
+
def initialize(object, context)
|
28
|
+
@object = object
|
29
|
+
@context = context
|
30
|
+
@jat_class = self.class.jat_class
|
91
31
|
end
|
92
|
-
data.compact!
|
93
|
-
data
|
94
|
-
end
|
95
|
-
end
|
96
32
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
@object = object
|
103
|
-
@context = context
|
104
|
-
@includes = includes
|
105
|
-
@full_map = full_map
|
106
|
-
@map = full_map.fetch(jat_class.type)
|
107
|
-
@presenter = jat_class::Presenter.new(object, context)
|
108
|
-
end
|
109
|
-
|
110
|
-
def data
|
111
|
-
return unless object
|
112
|
-
|
113
|
-
attributes = get_attributes
|
114
|
-
relationships = get_relationships
|
115
|
-
links = get_links
|
116
|
-
meta = get_meta
|
33
|
+
def to_h
|
34
|
+
data, includes = data_with_includes
|
35
|
+
meta = document_meta
|
36
|
+
links = document_links
|
37
|
+
jsonapi = jsonapi_data
|
117
38
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
def uid
|
127
|
-
{type: jat_class.type, id: presenter.id}
|
128
|
-
end
|
129
|
-
|
130
|
-
private
|
39
|
+
result = {}
|
40
|
+
result[:links] = links if links.any?
|
41
|
+
result[:data] = data if data
|
42
|
+
result[:included] = includes.values if includes.any?
|
43
|
+
result[:meta] = meta if meta.any?
|
44
|
+
result[:jsonapi] = jsonapi if jsonapi.any?
|
45
|
+
result
|
46
|
+
end
|
131
47
|
|
132
|
-
|
133
|
-
attributes_names = map[:attributes]
|
134
|
-
return if attributes_names.empty?
|
48
|
+
private
|
135
49
|
|
136
|
-
|
137
|
-
|
138
|
-
|
50
|
+
def data_with_includes
|
51
|
+
includes = {}
|
52
|
+
map = jat_class::Map.call(context)
|
53
|
+
data = many?(object, context) ? many(object, includes, map) : one(object, includes, map)
|
54
|
+
[data, includes]
|
139
55
|
end
|
140
|
-
end
|
141
56
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
relationships_names.each_with_object({}) do |name, rels|
|
147
|
-
rel_attribute = jat_class.attributes[name]
|
148
|
-
rel_object = presenter.public_send(rel_attribute.original_name)
|
149
|
-
|
150
|
-
rel_serializer = rel_attribute.serializer.call
|
151
|
-
rel_links = get_relationship_links(rel_serializer, rel_object)
|
152
|
-
rel_meta = get_relationship_meta(rel_serializer, rel_object)
|
153
|
-
rel_data =
|
154
|
-
if many?(rel_attribute, rel_object)
|
155
|
-
many_relationships_data(rel_serializer, rel_object)
|
156
|
-
else
|
157
|
-
one_relationship_data(rel_serializer, rel_object)
|
158
|
-
end
|
57
|
+
def many(objects, includes, map)
|
58
|
+
objects.map { |object| one(object, includes, map) }
|
59
|
+
end
|
159
60
|
|
160
|
-
|
161
|
-
|
162
|
-
result[:links] = rel_links if rel_links.any?
|
163
|
-
result[:meta] = rel_meta if rel_meta.any?
|
164
|
-
rels[name] = result
|
61
|
+
def one(object, includes, map)
|
62
|
+
jat_class::ResponsePiece.call(object, context, map, includes)
|
165
63
|
end
|
166
|
-
end
|
167
64
|
|
168
|
-
|
169
|
-
|
65
|
+
def many?(data, context)
|
66
|
+
many = context[:many]
|
67
|
+
many.nil? ? data.is_a?(Enumerable) : many
|
68
|
+
end
|
170
69
|
|
171
|
-
|
172
|
-
|
70
|
+
def jsonapi_data
|
71
|
+
combine(jat_class.jsonapi_data, context_jsonapi)
|
72
|
+
end
|
173
73
|
|
174
|
-
|
175
|
-
|
74
|
+
def document_links
|
75
|
+
combine(jat_class.document_links, context_links)
|
76
|
+
end
|
176
77
|
|
177
|
-
|
178
|
-
|
78
|
+
def document_meta
|
79
|
+
combine(jat_class.added_document_meta, context_meta)
|
80
|
+
end
|
179
81
|
|
180
|
-
|
181
|
-
|
182
|
-
rel_uid = rel_response_data.uid
|
183
|
-
includes[rel_uid] ||= rel_response_data.data
|
184
|
-
rel_uid
|
185
|
-
end
|
82
|
+
def combine(attributes, context_data)
|
83
|
+
return context_data if attributes.empty?
|
186
84
|
|
187
|
-
|
188
|
-
is_many = attribute.many?
|
85
|
+
data = context_data
|
189
86
|
|
190
|
-
|
191
|
-
|
87
|
+
attributes.each do |name, attribute|
|
88
|
+
next if data.key?(name)
|
192
89
|
|
193
|
-
|
194
|
-
object.is_a?(Enumerable)
|
195
|
-
end
|
90
|
+
value = attribute.value(object, context)
|
196
91
|
|
197
|
-
|
198
|
-
|
199
|
-
|
92
|
+
unless value.nil?
|
93
|
+
data = data.dup if data.equal?(FROZEN_EMPTY_HASH)
|
94
|
+
data[name] = value
|
95
|
+
end
|
96
|
+
end
|
200
97
|
|
201
|
-
|
202
|
-
|
203
|
-
result.compact!
|
204
|
-
result
|
205
|
-
end
|
98
|
+
data
|
99
|
+
end
|
206
100
|
|
207
|
-
|
208
|
-
|
209
|
-
|
101
|
+
def context_jsonapi
|
102
|
+
context_attr_transform(:jsonapi)
|
103
|
+
end
|
210
104
|
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
result
|
215
|
-
end
|
105
|
+
def context_links
|
106
|
+
context_attr_transform(:links)
|
107
|
+
end
|
216
108
|
|
217
|
-
|
218
|
-
|
219
|
-
|
109
|
+
def context_meta
|
110
|
+
context_attr_transform(:meta)
|
111
|
+
end
|
220
112
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
result
|
113
|
+
def context_attr_transform(key)
|
114
|
+
context[key]&.transform_keys(&:to_sym) || FROZEN_EMPTY_HASH
|
115
|
+
end
|
225
116
|
end
|
226
117
|
|
227
|
-
|
228
|
-
|
229
|
-
return meta if meta.empty?
|
230
|
-
|
231
|
-
presenter = rel_serializer::RelationshipMetaPresenter.new(object, rel_object, context)
|
232
|
-
result = meta.each_key.each_with_object({}) { |key, data| data[key] = presenter.public_send(key) }
|
233
|
-
result.compact!
|
234
|
-
result
|
235
|
-
end
|
118
|
+
extend ClassMethods
|
119
|
+
include InstanceMethods
|
236
120
|
end
|
237
121
|
end
|
238
122
|
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module JsonApi
|
6
|
+
class ResponsePiece
|
7
|
+
module ClassMethods
|
8
|
+
# Returns the Jat class that this ResponsePiece class is namespaced under.
|
9
|
+
attr_accessor :jat_class
|
10
|
+
|
11
|
+
# Since ResponsePiece is anonymously subclassed when Jat is subclassed,
|
12
|
+
# and then assigned to a constant of the Jat subclass, make inspect
|
13
|
+
# reflect the likely name for the class.
|
14
|
+
def inspect
|
15
|
+
"#{jat_class.inspect}::ResponsePiece"
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(object, context, map, includes)
|
19
|
+
new(object, context, map, includes).to_h
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module InstanceMethods
|
24
|
+
attr_reader :jat_class, :object, :context, :map, :type_map, :includes
|
25
|
+
|
26
|
+
def initialize(object, context, map, includes)
|
27
|
+
@jat_class = self.class.jat_class
|
28
|
+
@object = object
|
29
|
+
@context = context
|
30
|
+
@map = map
|
31
|
+
@type_map = map.fetch(jat_class.get_type)
|
32
|
+
@includes = includes
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_h
|
36
|
+
return unless object
|
37
|
+
|
38
|
+
attributes = get_attributes
|
39
|
+
relationships = get_relationships
|
40
|
+
links = get_links
|
41
|
+
meta = get_meta
|
42
|
+
|
43
|
+
result = uid
|
44
|
+
result[:attributes] = attributes if attributes
|
45
|
+
result[:relationships] = relationships if relationships
|
46
|
+
result[:links] = links if links.any?
|
47
|
+
result[:meta] = meta if meta.any?
|
48
|
+
result
|
49
|
+
end
|
50
|
+
|
51
|
+
def uid
|
52
|
+
{type: type, id: id}
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def get_attributes
|
58
|
+
attributes_names = type_map[:attributes]
|
59
|
+
return if attributes_names.empty?
|
60
|
+
|
61
|
+
attributes_names.each_with_object({}) do |name, attrs|
|
62
|
+
attribute = jat_class.attributes[name]
|
63
|
+
attrs[name] = attribute.value(object, context)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_relationships
|
68
|
+
relationships_names = type_map[:relationships]
|
69
|
+
return if relationships_names.empty?
|
70
|
+
|
71
|
+
relationships_names.each_with_object({}) do |name, rels|
|
72
|
+
rel_attribute = jat_class.attributes[name]
|
73
|
+
rel_object = rel_attribute.value(object, context)
|
74
|
+
|
75
|
+
rel_serializer = rel_attribute.serializer.call
|
76
|
+
rel_links = get_relationship_links(rel_serializer, rel_object)
|
77
|
+
rel_meta = get_relationship_meta(rel_serializer, rel_object)
|
78
|
+
rel_data =
|
79
|
+
if many?(rel_attribute, rel_object)
|
80
|
+
many_relationships_data(rel_serializer, rel_object)
|
81
|
+
else
|
82
|
+
one_relationship_data(rel_serializer, rel_object)
|
83
|
+
end
|
84
|
+
|
85
|
+
result = {}
|
86
|
+
result[:data] = rel_data
|
87
|
+
result[:links] = rel_links if rel_links.any?
|
88
|
+
result[:meta] = rel_meta if rel_meta.any?
|
89
|
+
rels[name] = result
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def many_relationships_data(rel_serializer, rel_objects)
|
94
|
+
return FROZEN_EMPTY_ARRAY if rel_objects.empty?
|
95
|
+
|
96
|
+
rel_objects.map { |rel_object| add_relationship_data(rel_serializer, rel_object) }
|
97
|
+
end
|
98
|
+
|
99
|
+
def one_relationship_data(rel_serializer, rel_object)
|
100
|
+
return unless rel_object
|
101
|
+
|
102
|
+
add_relationship_data(rel_serializer, rel_object)
|
103
|
+
end
|
104
|
+
|
105
|
+
def add_relationship_data(rel_serializer, rel_object)
|
106
|
+
rel_response_data = rel_serializer::ResponsePiece.new(rel_object, context, map, includes)
|
107
|
+
rel_uid = rel_response_data.uid
|
108
|
+
simple_uid = "#{rel_uid[:id]}-#{rel_uid[:type]}"
|
109
|
+
includes[simple_uid] ||= rel_response_data.to_h
|
110
|
+
rel_uid
|
111
|
+
end
|
112
|
+
|
113
|
+
def many?(attribute, object)
|
114
|
+
is_many = attribute.many?
|
115
|
+
|
116
|
+
# handle boolean
|
117
|
+
return is_many if (is_many == true) || (is_many == false)
|
118
|
+
|
119
|
+
# handle nil
|
120
|
+
object.is_a?(Enumerable)
|
121
|
+
end
|
122
|
+
|
123
|
+
def get_links
|
124
|
+
jat_class
|
125
|
+
.object_links
|
126
|
+
.transform_values { |attr| attr.value(object, context) }
|
127
|
+
.tap(&:compact!)
|
128
|
+
end
|
129
|
+
|
130
|
+
def get_meta
|
131
|
+
jat_class
|
132
|
+
.added_object_meta
|
133
|
+
.transform_values { |attr| attr.value(object, context) }
|
134
|
+
.tap(&:compact!)
|
135
|
+
end
|
136
|
+
|
137
|
+
def get_relationship_links(rel_serializer, rel_object)
|
138
|
+
links = rel_serializer.relationship_links
|
139
|
+
return FROZEN_EMPTY_HASH if links.empty?
|
140
|
+
|
141
|
+
context[:parent_object] = object
|
142
|
+
|
143
|
+
links
|
144
|
+
.transform_values { |attr| attr.value(rel_object, context) }
|
145
|
+
.tap(&:compact!)
|
146
|
+
.tap { context.delete(:parent_object) }
|
147
|
+
end
|
148
|
+
|
149
|
+
def get_relationship_meta(rel_serializer, rel_object)
|
150
|
+
meta = rel_serializer.added_relationship_meta
|
151
|
+
return FROZEN_EMPTY_HASH if meta.empty?
|
152
|
+
|
153
|
+
context[:parent_object] = object
|
154
|
+
|
155
|
+
meta
|
156
|
+
.transform_values { |attr| attr.value(rel_object, context) }
|
157
|
+
.tap(&:compact!)
|
158
|
+
.tap { context.delete(:parent_object) }
|
159
|
+
end
|
160
|
+
|
161
|
+
def type
|
162
|
+
@type ||= jat_class.get_type
|
163
|
+
end
|
164
|
+
|
165
|
+
def id
|
166
|
+
@id ||= jat_class.get_id.value(object, context)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
extend ClassMethods
|
171
|
+
include InstanceMethods
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module JsonApiActiverecord
|
6
|
+
def self.plugin_name
|
7
|
+
:json_api_activerecord
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.before_load(jat_class, **_opts)
|
11
|
+
return if jat_class.plugin_used?(:json_api)
|
12
|
+
raise Error, "Please load :json_api plugin first"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load(jat_class, **opts)
|
16
|
+
jat_class.plugin :json_api_preloads, **opts
|
17
|
+
jat_class.plugin :base_activerecord_preloads, **opts
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
register_plugin(JsonApiActiverecord.plugin_name, JsonApiActiverecord)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module JsonApiLowerCamelCase
|
6
|
+
def self.plugin_name
|
7
|
+
:json_api_lower_camel_case
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.before_load(jat_class, **_opts)
|
11
|
+
raise Error, "Please load :json_api plugin first" unless jat_class.plugin_used?(:json_api)
|
12
|
+
|
13
|
+
jat_class.plugin :base_lower_camel_case
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.load(jat_class, **_opts)
|
17
|
+
jat_class::Response.include(ResponseInstanceMethods)
|
18
|
+
end
|
19
|
+
|
20
|
+
module ResponseInstanceMethods
|
21
|
+
private
|
22
|
+
|
23
|
+
def context_attr_transform(*)
|
24
|
+
result = super
|
25
|
+
return result if result.empty?
|
26
|
+
|
27
|
+
result.transform_keys! { |key| Jat::LowerCamelCaseTransformation.call(key) }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
register_plugin(JsonApiLowerCamelCase.plugin_name, JsonApiLowerCamelCase)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module JsonApiMapsCache
|
6
|
+
def self.plugin_name
|
7
|
+
:json_api_maps_cache
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.before_load(jat_class, **opts)
|
11
|
+
return if jat_class.plugin_used?(:json_api)
|
12
|
+
raise Error, "Please load :json_api plugin first"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load(jat_class, **_opts)
|
16
|
+
jat_class::Map.extend(MapsCacheClassMethods)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.after_load(jat_class, **opts)
|
20
|
+
jat_class.config[:cached_maps_count] = opts[:cached_maps_count] || 100
|
21
|
+
end
|
22
|
+
|
23
|
+
module MapsCacheClassMethods
|
24
|
+
# Caches up to `:cached_maps_count` maps for each serializer.
|
25
|
+
# Removes earliest value if new value exceeds limit.
|
26
|
+
|
27
|
+
def maps_cache
|
28
|
+
@maps_cache ||= Hash.new do |cache, cache_key|
|
29
|
+
cache.shift if cache.length >= jat_class.config[:cached_maps_count] # protect from memory leak
|
30
|
+
cache[cache_key] = EnumDeepFreeze.call(yield)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def construct_map(exposed, fields, includes)
|
37
|
+
cache = maps_cache { super }
|
38
|
+
cache_key = cache_key(exposed, fields, includes)
|
39
|
+
cache[cache_key]
|
40
|
+
end
|
41
|
+
|
42
|
+
def cache_key(exposed, fields, includes)
|
43
|
+
key = "exposed:#{exposed}:"
|
44
|
+
key += "includes:#{includes}:" if includes
|
45
|
+
|
46
|
+
if fields
|
47
|
+
key += "fields:"
|
48
|
+
fields.each { |type, attrs| key += "#{type}:#{attrs}:" }
|
49
|
+
end
|
50
|
+
|
51
|
+
key
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
register_plugin(JsonApiMapsCache.plugin_name, JsonApiMapsCache)
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./lib/preloads"
|
4
|
+
|
5
|
+
class Jat
|
6
|
+
module Plugins
|
7
|
+
module JsonApiPreloads
|
8
|
+
def self.plugin_name
|
9
|
+
:json_api_preloads
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.before_load(jat_class, **opts)
|
13
|
+
raise Error, "Please load :json_api plugin first" unless jat_class.plugin_used?(:json_api)
|
14
|
+
|
15
|
+
jat_class.plugin :base_preloads, **opts
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.load(jat_class, **_opts)
|
19
|
+
jat_class.extend(ClassMethods)
|
20
|
+
jat_class.include(InstanceMethods)
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def preloads(context = {})
|
25
|
+
new(context).preloads
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
def preloads
|
31
|
+
@preloads ||= Preloads.call(self)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
register_plugin(JsonApiPreloads.plugin_name, JsonApiPreloads)
|
37
|
+
end
|
38
|
+
end
|