jat 0.0.1 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +107 -0
  3. data/lib/jat/config.rb +35 -0
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/cache/cache.rb +47 -0
  6. data/lib/jat/plugins/common/_activerecord_preloads/_activerecord_preloads.rb +38 -0
  7. data/lib/jat/plugins/common/_activerecord_preloads/lib/preloader.rb +93 -0
  8. data/lib/jat/plugins/common/_lower_camel_case/_lower_camel_case.rb +36 -0
  9. data/lib/jat/plugins/common/_preloads/_preloads.rb +63 -0
  10. data/lib/jat/plugins/common/_preloads/lib/format_user_preloads.rb +52 -0
  11. data/lib/jat/plugins/common/_preloads/lib/preloads_with_path.rb +78 -0
  12. data/lib/jat/plugins/json_api/json_api.rb +251 -0
  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 +119 -0
  16. data/lib/jat/plugins/json_api/lib/params/fields/parse.rb +27 -0
  17. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +58 -0
  18. data/lib/jat/plugins/json_api/lib/params/fields.rb +23 -0
  19. data/lib/jat/plugins/json_api/lib/params/include/parse.rb +55 -0
  20. data/lib/jat/plugins/json_api/lib/params/include/validate.rb +29 -0
  21. data/lib/jat/plugins/json_api/lib/params/include.rb +49 -0
  22. data/lib/jat/plugins/json_api/lib/response.rb +123 -0
  23. data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
  24. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  25. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
  26. data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
  27. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  28. data/lib/jat/plugins/json_api/plugins/json_api_preloads/lib/preloads.rb +76 -0
  29. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
  30. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  31. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  32. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  33. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  34. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  35. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  36. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  37. data/lib/jat/plugins/simple_api/lib/map.rb +99 -0
  38. data/lib/jat/plugins/simple_api/lib/response.rb +119 -0
  39. data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
  40. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  41. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
  42. data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
  43. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
  44. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  45. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  46. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  47. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
  48. data/lib/jat/plugins/simple_api/simple_api.rb +125 -0
  49. data/lib/jat/plugins/to_str/to_str.rb +54 -0
  50. data/lib/jat/plugins/types/types.rb +54 -0
  51. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  52. data/lib/jat/plugins.rb +39 -0
  53. data/lib/jat/utils/enum_deep_dup.rb +29 -0
  54. data/lib/jat/utils/enum_deep_freeze.rb +19 -0
  55. data/lib/jat.rb +66 -141
  56. data/test/lib/jat/attribute_test.rb +152 -0
  57. data/test/lib/jat/config_test.rb +57 -0
  58. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +59 -0
  59. data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +84 -0
  60. data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
  61. data/test/lib/jat/plugins/_preloads/_preloads_test.rb +68 -0
  62. data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +47 -0
  63. data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +33 -0
  64. data/test/lib/jat/plugins/cache/cache_test.rb +82 -0
  65. data/test/lib/jat/plugins/json_api/json_api_test.rb +162 -0
  66. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +38 -0
  67. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +41 -0
  68. data/test/lib/jat/plugins/json_api/lib/map_test.rb +188 -0
  69. data/test/lib/jat/plugins/json_api/lib/response_test.rb +489 -0
  70. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
  71. data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -0
  72. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  73. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +37 -0
  74. data/test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb +197 -0
  75. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +84 -0
  76. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +77 -0
  77. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +133 -0
  78. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +348 -0
  79. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +139 -0
  80. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
  81. data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -0
  82. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  83. data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
  84. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +37 -0
  85. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +89 -0
  86. data/test/lib/jat/plugins/to_str/to_str_test.rb +52 -0
  87. data/test/lib/jat/plugins/types/types_test.rb +79 -0
  88. data/test/lib/jat/utils/enum_deep_dup_test.rb +31 -0
  89. data/test/lib/jat/utils/enum_deep_freeze_test.rb +28 -0
  90. data/test/lib/jat_test.rb +143 -0
  91. data/test/lib/plugin_test.rb +49 -0
  92. data/test/support/activerecord.rb +24 -0
  93. data/test/test_helper.rb +13 -0
  94. data/test/test_plugin.rb +56 -0
  95. metadata +243 -11
@@ -0,0 +1,251 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./lib/fields_param_parser"
4
+ require_relative "./lib/include_param_parser"
5
+ require_relative "./lib/map"
6
+ require_relative "./lib/response"
7
+ require_relative "./lib/response_piece"
8
+
9
+ # Serializes to JSON-API format
10
+ class Jat
11
+ module Plugins
12
+ module JsonApi
13
+ def self.plugin_name
14
+ :json_api
15
+ end
16
+
17
+ def self.before_load(jat_class, **_opts)
18
+ response_plugin = jat_class.config[:response_plugin_loaded]
19
+ return unless response_plugin
20
+
21
+ raise Error, "Response plugin `#{response_plugin}` was already loaded before"
22
+ end
23
+
24
+ def self.load(jat_class, **_opts)
25
+ jat_class.include(InstanceMethods)
26
+ jat_class.extend(ClassMethods)
27
+ end
28
+
29
+ def self.after_load(jat_class, **opts)
30
+ jat_class.config[:response_plugin_loaded] = plugin_name
31
+
32
+ fields_parser_class = Class.new(FieldsParamParser)
33
+ fields_parser_class.jat_class = jat_class
34
+ jat_class.const_set(:FieldsParamParser, fields_parser_class)
35
+
36
+ includes_parser_class = Class.new(IncludeParamParser)
37
+ includes_parser_class.jat_class = jat_class
38
+ jat_class.const_set(:IncludeParamParser, includes_parser_class)
39
+
40
+ map_class = Class.new(Map)
41
+ map_class.jat_class = jat_class
42
+ jat_class.const_set(:Map, map_class)
43
+
44
+ response_class = Class.new(Response)
45
+ response_class.jat_class = jat_class
46
+ jat_class.const_set(:Response, response_class)
47
+
48
+ response_piece_class = Class.new(ResponsePiece)
49
+ response_piece_class.jat_class = jat_class
50
+ jat_class.const_set(:ResponsePiece, response_piece_class)
51
+
52
+ jat_class.id
53
+ end
54
+
55
+ module InstanceMethods
56
+ def to_h(object)
57
+ self.class::Response.call(object, context)
58
+ end
59
+
60
+ def map
61
+ @map ||= self.class.map(context)
62
+ end
63
+ end
64
+
65
+ module ClassMethods
66
+ def inherited(subclass)
67
+ super
68
+
69
+ fields_parser_class = Class.new(self::FieldsParamParser)
70
+ fields_parser_class.jat_class = subclass
71
+ subclass.const_set(:FieldsParamParser, fields_parser_class)
72
+
73
+ includes_parser_class = Class.new(self::IncludeParamParser)
74
+ includes_parser_class.jat_class = subclass
75
+ subclass.const_set(:IncludeParamParser, includes_parser_class)
76
+
77
+ map_class = Class.new(self::Map)
78
+ map_class.jat_class = subclass
79
+ subclass.const_set(:Map, map_class)
80
+
81
+ response_class = Class.new(self::Response)
82
+ response_class.jat_class = subclass
83
+ subclass.const_set(:Response, response_class)
84
+
85
+ response_piece_class = Class.new(self::ResponsePiece)
86
+ response_piece_class.jat_class = subclass
87
+ subclass.const_set(:ResponsePiece, response_piece_class)
88
+
89
+ subclass.type(@type) if defined?(@type)
90
+ subclass.id(&get_id.params[:block])
91
+
92
+ # Assign same jsonapi_data
93
+ jsonapi_data.each_value do |attribute|
94
+ params = attribute.params
95
+ subclass.jsonapi(params[:name], **params[:opts], &params[:block])
96
+ end
97
+
98
+ # Assign same object_links
99
+ object_links.each_value do |attribute|
100
+ params = attribute.params
101
+ subclass.object_link(params[:name], **params[:opts], &params[:block])
102
+ end
103
+
104
+ # Assign same document_links
105
+ document_links.each_value do |attribute|
106
+ params = attribute.params
107
+ subclass.document_link(params[:name], **params[:opts], &params[:block])
108
+ end
109
+
110
+ # Assign same relationship_links
111
+ relationship_links.each_value do |attribute|
112
+ params = attribute.params
113
+ subclass.relationship_link(params[:name], **params[:opts], &params[:block])
114
+ end
115
+
116
+ # Assign same added_object_meta
117
+ added_object_meta.each_value do |attribute|
118
+ params = attribute.params
119
+ subclass.object_meta(params[:name], **params[:opts], &params[:block])
120
+ end
121
+
122
+ # Assign same added_document_meta
123
+ added_document_meta.each_value do |attribute|
124
+ params = attribute.params
125
+ subclass.document_meta(params[:name], **params[:opts], &params[:block])
126
+ end
127
+
128
+ # Assign same added_relationship_meta
129
+ added_relationship_meta.each_value do |attribute|
130
+ params = attribute.params
131
+ subclass.relationship_meta(params[:name], **params[:opts], &params[:block])
132
+ end
133
+ end
134
+
135
+ def get_type
136
+ (defined?(@type) && @type) || raise(Error, "#{self} has no defined type")
137
+ end
138
+
139
+ def type(new_type)
140
+ @type = new_type.to_sym
141
+ end
142
+
143
+ def get_id
144
+ @id
145
+ end
146
+
147
+ def id(**opts, &block)
148
+ @id = self::Attribute.new(name: :id, opts: opts, block: block)
149
+ end
150
+
151
+ # JSON API block values
152
+ #
153
+ # https://jsonapi.org/format/#document-jsonapi-object
154
+ def jsonapi_data(_value = nil)
155
+ @jsonapi_data ||= {}
156
+ end
157
+
158
+ def jsonapi(name, **opts, &block)
159
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
160
+ jsonapi_data[new_attr.name] = new_attr
161
+ end
162
+
163
+ # Links related to the resource
164
+ #
165
+ # https://jsonapi.org/format/#document-resource-object-links
166
+ def object_links
167
+ @object_links ||= {}
168
+ end
169
+
170
+ def object_link(name, **opts, &block)
171
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
172
+ object_links[new_attr.name] = new_attr
173
+ end
174
+
175
+ # Top-level document links
176
+ #
177
+ # https://jsonapi.org/format/#document-top-level
178
+ def document_links
179
+ @document_links ||= {}
180
+ end
181
+
182
+ def document_link(name, **opts, &block)
183
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
184
+ document_links[new_attr.name] = new_attr
185
+ end
186
+
187
+ # Relationship links
188
+ #
189
+ # https://jsonapi.org/format/#document-resource-object-linkage
190
+ def relationship_links
191
+ @relationship_links ||= {}
192
+ end
193
+
194
+ def relationship_link(name, **opts, &block)
195
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
196
+ relationship_links[new_attr.name] = new_attr
197
+ end
198
+
199
+ # Object meta
200
+ #
201
+ # https://jsonapi.org/format/#document-resource-objects
202
+ def added_object_meta
203
+ @added_object_meta ||= {}
204
+ end
205
+
206
+ def object_meta(name, **opts, &block)
207
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
208
+ added_object_meta[new_attr.name] = new_attr
209
+ end
210
+
211
+ # Top-level document meta
212
+ #
213
+ # https://jsonapi.org/format/#document-meta
214
+ def added_document_meta
215
+ @added_document_meta ||= {}
216
+ end
217
+
218
+ def document_meta(name, **opts, &block)
219
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
220
+ added_document_meta[new_attr.name] = new_attr
221
+ end
222
+
223
+ # Relationship meta
224
+ #
225
+ # https://jsonapi.org/format/#document-resource-object-relationships
226
+ def added_relationship_meta
227
+ @added_relationship_meta ||= {}
228
+ end
229
+
230
+ def relationship_meta(name, **opts, &block)
231
+ new_attr = self::Attribute.new(name: name, opts: opts, block: block)
232
+ added_relationship_meta[new_attr.name] = new_attr
233
+ end
234
+
235
+ def map(context)
236
+ self::Map.call(context)
237
+ end
238
+
239
+ def map_full
240
+ @map_full ||= self::Map.call(exposed: :all)
241
+ end
242
+
243
+ def map_exposed
244
+ @map_exposed ||= self::Map.call(exposed: :default)
245
+ end
246
+ end
247
+ end
248
+
249
+ register_plugin(JsonApi.plugin_name, JsonApi)
250
+ end
251
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ class FieldsParamParser
7
+ module ClassMethods
8
+ COMMA = ","
9
+
10
+ # Returns the Jat class that this FieldsParamParser class is namespaced under.
11
+ attr_accessor :jat_class
12
+
13
+ # Since FieldsParamParser is anonymously subclassed when Jat is subclassed,
14
+ # and then assigned to a constant of the Jat subclass, make inspect
15
+ # reflect the likely name for the class.
16
+ def inspect
17
+ "#{jat_class.inspect}::FieldsParamParser"
18
+ end
19
+
20
+ def parse(fields)
21
+ return FROZEN_EMPTY_HASH unless fields
22
+
23
+ parse_to_nested_hash(fields)
24
+ end
25
+
26
+ private
27
+
28
+ def parse_to_nested_hash(fields)
29
+ fields.each_with_object({}) do |(type, attrs_string), obj|
30
+ attrs = attrs_string.split(COMMA).map!(&:to_sym)
31
+ obj[type.to_sym] = attrs
32
+ end
33
+ end
34
+ end
35
+
36
+ extend ClassMethods
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ class IncludeParamParser
7
+ module ClassMethods
8
+ COMMA = ","
9
+ DOT = "."
10
+
11
+ # Returns the Jat class that this IncludeParamParser class is namespaced under.
12
+ attr_accessor :jat_class
13
+
14
+ # Since IncludeParamParser is anonymously subclassed when Jat is subclassed,
15
+ # and then assigned to a constant of the Jat subclass, make inspect
16
+ # reflect the likely name for the class.
17
+ def inspect
18
+ "#{jat_class.inspect}::IncludeParamParser"
19
+ end
20
+
21
+ def parse(includes_string_param)
22
+ return {} unless includes_string_param
23
+
24
+ includes_hash = parse_to_nested_hash(includes_string_param)
25
+ typed_includes(jat_class, includes_hash, {})
26
+ end
27
+
28
+ private
29
+
30
+ def parse_to_nested_hash(includes_string_param)
31
+ includes_string_param.split(COMMA).each_with_object({}) do |part, obj|
32
+ includes = parse_part(part)
33
+ deep_merge!(obj, includes)
34
+ end
35
+ end
36
+
37
+ def typed_includes(jat_class, includes, result)
38
+ includes.each do |included_attr_name, nested_includes|
39
+ add_typed_include(result, jat_class, included_attr_name)
40
+
41
+ nested_serializer = jat_class.attributes.fetch(included_attr_name).serializer.call
42
+ typed_includes(nested_serializer, nested_includes, result)
43
+ end
44
+
45
+ result
46
+ end
47
+
48
+ def add_typed_include(result, serializer, included_attr_name)
49
+ type = serializer.get_type
50
+
51
+ includes = result[type] || []
52
+ includes |= [included_attr_name]
53
+
54
+ result[type] = includes
55
+ end
56
+
57
+ def parse_part(part)
58
+ val = {}
59
+
60
+ part.split(DOT).reverse_each do |inc|
61
+ val = {inc.to_sym => val}
62
+ end
63
+
64
+ val
65
+ end
66
+
67
+ def deep_merge!(this_hash, other_hash)
68
+ this_hash.merge!(other_hash) do |_key, this_val, other_val|
69
+ deep_merge(this_val, other_val)
70
+ end
71
+ end
72
+
73
+ def deep_merge(this_hash, other_hash)
74
+ this_hash.merge(other_hash) do |_key, this_val, other_val|
75
+ deep_merge(this_val, other_val)
76
+ end
77
+ end
78
+ end
79
+
80
+ extend ClassMethods
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ class Map
7
+ module ClassMethods
8
+ # Returns the Jat class that this Map class is namespaced under.
9
+ attr_accessor :jat_class
10
+
11
+ # Since Map 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}::Map"
16
+ end
17
+
18
+ # Returns structure like
19
+ # {
20
+ # type1 => {
21
+ # attributes: [attr1, attr2, ...],
22
+ # relationships: [rel1, rel2, ...]
23
+ # },
24
+ # type2 => { ... }
25
+ # }
26
+ def call(context)
27
+ exposed = context[:exposed]&.to_sym || :default
28
+ fields = context[:fields]
29
+ includes = context[:include]
30
+
31
+ construct_map(exposed, fields, includes)
32
+ end
33
+
34
+ private
35
+
36
+ def construct_map(exposed, fields, includes)
37
+ fields = jat_class::FieldsParamParser.parse(fields) if fields
38
+ includes = jat_class::IncludeParamParser.parse(includes) if includes
39
+
40
+ new(exposed, fields, includes).to_h
41
+ end
42
+ end
43
+
44
+ module InstanceMethods
45
+ attr_reader :jat_class, :exposed, :includes, :fields
46
+
47
+ EXPOSED_TYPES = {all: :all, default: :default, none: :none}.freeze
48
+
49
+ def initialize(exposed, fields, includes)
50
+ @jat_class = self.class.jat_class
51
+ @exposed = EXPOSED_TYPES.fetch(exposed)
52
+ @fields = fields
53
+ @includes = includes
54
+ end
55
+
56
+ def to_h
57
+ map = {}
58
+ append_map(map, jat_class)
59
+ map
60
+ end
61
+
62
+ private
63
+
64
+ def append_map(map, jat_class)
65
+ type = jat_class.get_type
66
+ return map if map.key?(type)
67
+
68
+ type_map = {serializer: jat_class}
69
+ map[type] = type_map
70
+
71
+ fill_type_map(map, type_map, type, jat_class)
72
+
73
+ type_map[:attributes] ||= FROZEN_EMPTY_ARRAY
74
+ type_map[:relationships] ||= FROZEN_EMPTY_ARRAY
75
+ end
76
+
77
+ def fill_type_map(map, type_map, type, jat_class)
78
+ jat_class.attributes.each_value do |attribute|
79
+ next unless expose?(type, attribute)
80
+
81
+ fill_attr(map, type_map, attribute)
82
+ end
83
+ end
84
+
85
+ def fill_attr(map, type_map, attribute)
86
+ name = attribute.name
87
+
88
+ if attribute.relation?
89
+ (type_map[:relationships] ||= []) << name
90
+ append_map(map, attribute.serializer.call)
91
+ else
92
+ (type_map[:attributes] ||= []) << name
93
+ end
94
+ end
95
+
96
+ def expose?(type, attribute)
97
+ attribute_name = attribute.name
98
+ fields_attribute_names = fields && fields[type]
99
+ return fields_attribute_names.include?(attribute_name) if fields_attribute_names
100
+
101
+ includes_attribute_names = includes && includes[type]
102
+ includes_attribute_names&.include?(attribute_name) || attribute_exposed?(attribute)
103
+ end
104
+
105
+ def attribute_exposed?(attribute)
106
+ case exposed
107
+ when :all then true
108
+ when :none then false
109
+ else attribute.exposed?
110
+ end
111
+ end
112
+ end
113
+
114
+ extend ClassMethods
115
+ include InstanceMethods
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ module Params
7
+ class Fields
8
+ class Parse
9
+ COMMA = ","
10
+
11
+ class << self
12
+ # returns Hash { type => [attr1, attr2] }
13
+ def call(fields)
14
+ return {} unless fields
15
+
16
+ fields.each_with_object({}) do |(type, attrs_string), obj|
17
+ attrs = attrs_string.split(COMMA).map!(&:to_sym)
18
+ obj[type.to_sym] = attrs
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ module Params
7
+ class Fields
8
+ class Validate
9
+ class << self
10
+ def call(jat, fields)
11
+ full_map = jat.maps.full
12
+
13
+ fields.each do |type, attributes_names|
14
+ new(jat, type, full_map).validate(attributes_names)
15
+ end
16
+ end
17
+ end
18
+
19
+ attr_reader :jat, :type, :full_map
20
+
21
+ def initialize(jat, type, full_map)
22
+ @jat = jat
23
+ @type = type
24
+ @full_map = full_map
25
+ end
26
+
27
+ def validate(attributes_names)
28
+ check_fields_type
29
+ check_attributes_names(attributes_names)
30
+ end
31
+
32
+ private
33
+
34
+ def check_fields_type
35
+ return if full_map.key?(type)
36
+
37
+ raise Error, "#{jat.class} and its children have no requested type `#{type}`"
38
+ end
39
+
40
+ def check_attributes_names(attributes_names)
41
+ attributes_names.each do |attribute_name|
42
+ check_attribute_name(attribute_name)
43
+ end
44
+ end
45
+
46
+ def check_attribute_name(attribute_name)
47
+ type_data = full_map.fetch(type)
48
+ type_serializer = type_data.fetch(:serializer)
49
+ return if type_serializer.attributes.key?(attribute_name)
50
+
51
+ raise Error, "#{type_serializer} has no requested attribute or relationship `#{attribute_name}`"
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./fields/parse"
4
+ require_relative "./fields/validate"
5
+
6
+ class Jat
7
+ module Plugins
8
+ module JsonApi
9
+ module Params
10
+ class Fields
11
+ class << self
12
+ # returns Hash { type => [key1, key2] }
13
+ def call(jat, fields)
14
+ parsed_fields = Parse.call(fields)
15
+ Validate.call(jat, parsed_fields)
16
+ parsed_fields
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ module Params
7
+ class Include
8
+ class Parse
9
+ COMMA = ","
10
+ DOT = "."
11
+
12
+ class << self
13
+ def call(includes_string)
14
+ return {} unless includes_string
15
+
16
+ string_to_hash(includes_string)
17
+ end
18
+
19
+ private
20
+
21
+ def string_to_hash(includes_string)
22
+ includes_string.split(COMMA).each_with_object({}) do |part, obj|
23
+ includes = parse_part(part)
24
+ deep_merge!(obj, includes)
25
+ end
26
+ end
27
+
28
+ def parse_part(part)
29
+ val = {}
30
+
31
+ part.split(DOT).reverse_each do |inc|
32
+ val = {inc.to_sym => val}
33
+ end
34
+
35
+ val
36
+ end
37
+
38
+ def deep_merge!(this_hash, other_hash)
39
+ this_hash.merge!(other_hash) do |_key, this_val, other_val|
40
+ deep_merge(this_val, other_val)
41
+ end
42
+ end
43
+
44
+ def deep_merge(this_hash, other_hash)
45
+ this_hash.merge(other_hash) do |_key, this_val, other_val|
46
+ deep_merge(this_val, other_val)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApi
6
+ module Params
7
+ class Include
8
+ class Validate
9
+ class << self
10
+ def call(jat_class, includes)
11
+ includes.each do |name, nested_includes|
12
+ attribute = jat_class.attributes[name]
13
+ raise_error(jat_class, name) if !attribute || !attribute.relation?
14
+
15
+ nested_serializer = attribute.serializer.call
16
+ call(nested_serializer, nested_includes)
17
+ end
18
+ end
19
+
20
+ def raise_error(jat_class, name)
21
+ raise Error, "#{jat_class} has no `#{name}` relationship"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end