jat 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +26 -5
  3. data/lib/jat/config.rb +4 -7
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
  6. data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
  7. data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
  8. data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
  9. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
  10. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
  11. data/lib/jat/plugins/cache/cache.rb +14 -6
  12. data/lib/jat/plugins/json_api/json_api.rb +150 -106
  13. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  14. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  15. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  16. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  17. data/lib/jat/plugins/json_api/lib/response.rb +81 -197
  18. data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
  19. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  20. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
  21. data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
  22. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  23. data/lib/jat/plugins/{_json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
  24. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
  25. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  26. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  27. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  28. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  29. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  30. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  31. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  32. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  33. data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
  34. data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
  35. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  36. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
  37. data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
  38. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
  39. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  40. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  41. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  42. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
  43. data/lib/jat/plugins/simple_api/simple_api.rb +87 -27
  44. data/lib/jat/plugins/to_str/to_str.rb +15 -5
  45. data/lib/jat/plugins/types/types.rb +54 -0
  46. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  47. data/lib/jat/plugins.rb +42 -30
  48. data/lib/jat.rb +40 -35
  49. data/test/lib/jat/attribute_test.rb +22 -4
  50. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  51. data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
  52. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
  53. data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
  54. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  55. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  56. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  57. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  58. data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
  59. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
  60. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
  61. data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
  62. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  63. data/test/lib/jat/plugins/json_api/lib/response_test.rb +38 -32
  64. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
  65. data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -0
  66. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  67. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
  68. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
  69. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
  70. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  71. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  72. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  73. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +87 -0
  74. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
  75. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  76. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +90 -76
  77. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
  78. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
  79. data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -0
  80. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  81. data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
  82. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
  83. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
  84. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  85. data/test/lib/jat/plugins/types/types_test.rb +84 -0
  86. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  87. data/test/lib/jat/plugins_test.rb +88 -0
  88. data/test/lib/jat_test.rb +47 -24
  89. data/test/lib/plugin_test.rb +3 -3
  90. data/test/test_helper.rb +7 -6
  91. data/test/test_plugin.rb +9 -12
  92. metadata +107 -88
  93. data/CHANGELOG.md +0 -7
  94. data/README.md +0 -21
  95. data/jat.gemspec +0 -37
  96. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
  97. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  98. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  99. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  100. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  101. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  102. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  103. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  104. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  105. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  106. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  107. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  108. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  109. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  110. data/lib/jat/presenter.rb +0 -51
  111. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
  112. data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
  113. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  114. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  115. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  116. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  117. data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
  118. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  119. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  120. data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
  121. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  122. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  123. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  124. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  125. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  126. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  127. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  128. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  129. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  130. data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
  131. 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
- attr_reader :jat, :jat_class, :object, :context
9
-
10
- def initialize(jat)
11
- @jat = jat
12
- @jat_class = jat.class
13
- @object = jat.object
14
- @context = jat.context
15
- end
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
- def document_links
71
- data = context[:links]&.transform_keys(&:to_sym) || {}
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
- def document_meta
84
- data = context[:meta]&.transform_keys(&:to_sym) || {}
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
- presenter = jat_class::DocumentMetaPresenter.new(object, context)
89
- document_meta.each_key do |key|
90
- data[key] = presenter.public_send(key) unless data.key?(key)
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
- class ResponseData
98
- attr_reader :jat_class, :object, :context, :full_map, :map, :includes, :presenter
99
-
100
- def initialize(jat_class, object, context, full_map, includes)
101
- @jat_class = jat_class
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
- result = uid
119
- result[:attributes] = attributes if attributes
120
- result[:relationships] = relationships if relationships
121
- result[:links] = links if links.any?
122
- result[:meta] = meta if meta.any?
123
- result
124
- end
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
- def get_attributes
133
- attributes_names = map[:attributes]
134
- return if attributes_names.empty?
48
+ private
135
49
 
136
- attributes_names.each_with_object({}) do |name, attrs|
137
- attribute = jat_class.attributes[name]
138
- attrs[name] = presenter.public_send(attribute.original_name)
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
- def get_relationships
143
- relationships_names = map[:relationships]
144
- return if relationships_names.empty?
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
- result = {}
161
- result[:data] = rel_data
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
- def many_relationships_data(rel_serializer, rel_objects)
169
- return [] if rel_objects.empty?
65
+ def many?(data, context)
66
+ many = context[:many]
67
+ many.nil? ? data.is_a?(Enumerable) : many
68
+ end
170
69
 
171
- rel_objects.map { |rel_object| add_relationship_data(rel_serializer, rel_object) }
172
- end
70
+ def jsonapi_data
71
+ combine(jat_class.jsonapi_data, context_jsonapi)
72
+ end
173
73
 
174
- def one_relationship_data(rel_serializer, rel_object)
175
- return unless rel_object
74
+ def document_links
75
+ combine(jat_class.document_links, context_links)
76
+ end
176
77
 
177
- add_relationship_data(rel_serializer, rel_object)
178
- end
78
+ def document_meta
79
+ combine(jat_class.added_document_meta, context_meta)
80
+ end
179
81
 
180
- def add_relationship_data(rel_serializer, rel_object)
181
- rel_response_data = self.class.new(rel_serializer, rel_object, context, full_map, includes)
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
- def many?(attribute, object)
188
- is_many = attribute.many?
85
+ data = context_data
189
86
 
190
- # handle boolean
191
- return is_many if (is_many == true) || (is_many == false)
87
+ attributes.each do |name, attribute|
88
+ next if data.key?(name)
192
89
 
193
- # handle nil
194
- object.is_a?(Enumerable)
195
- end
90
+ value = attribute.value(object, context)
196
91
 
197
- def get_links
198
- links = jat_class.object_links
199
- return links if links.empty?
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
- presenter = jat_class::LinksPresenter.new(object, context)
202
- result = links.each_key.each_with_object({}) { |key, data| data[key] = presenter.public_send(key) }
203
- result.compact!
204
- result
205
- end
98
+ data
99
+ end
206
100
 
207
- def get_meta
208
- meta = jat_class.added_object_meta
209
- return meta if meta.empty?
101
+ def context_jsonapi
102
+ context_attr_transform(:jsonapi)
103
+ end
210
104
 
211
- presenter = jat_class::MetaPresenter.new(object, context)
212
- result = meta.each_key.each_with_object({}) { |key, data| data[key] = presenter.public_send(key) }
213
- result.compact!
214
- result
215
- end
105
+ def context_links
106
+ context_attr_transform(:links)
107
+ end
216
108
 
217
- def get_relationship_links(rel_serializer, rel_object)
218
- links = rel_serializer.relationship_links
219
- return links if links.empty?
109
+ def context_meta
110
+ context_attr_transform(:meta)
111
+ end
220
112
 
221
- presenter = rel_serializer::RelationshipLinksPresenter.new(object, rel_object, context)
222
- result = links.each_key.each_with_object({}) { |key, data| data[key] = presenter.public_send(key) }
223
- result.compact!
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
- def get_relationship_meta(rel_serializer, rel_object)
228
- meta = rel_serializer.added_relationship_meta
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