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,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./include/parse"
4
+ require_relative "./include/validate"
5
+
6
+ class Jat
7
+ module Plugins
8
+ module JsonApi
9
+ module Params
10
+ class Include
11
+ class << self
12
+ # returns Hash { type => [attr1, attr2] }
13
+ def call(jat, includes_string)
14
+ return {} unless includes_string
15
+
16
+ jat_class = jat.class
17
+ includes = Parse.call(includes_string)
18
+ Validate.call(jat_class, includes)
19
+
20
+ typed_includes(jat_class, includes, {})
21
+ end
22
+
23
+ private
24
+
25
+ def typed_includes(jat_class, includes, result)
26
+ includes.each do |included_attr_name, nested_includes|
27
+ add_include(result, jat_class, included_attr_name)
28
+
29
+ nested_serializer = jat_class.attributes.fetch(included_attr_name).serializer.call
30
+ typed_includes(nested_serializer, nested_includes, result)
31
+ end
32
+
33
+ result
34
+ end
35
+
36
+ def add_include(result, serializer, included_attr_name)
37
+ type = serializer.type
38
+
39
+ includes = result[type] || []
40
+ includes |= [included_attr_name]
41
+
42
+ result[type] = includes
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Serializes to JSON-API format
4
+ class Jat
5
+ module Plugins
6
+ module JsonApi
7
+ class Response
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"
17
+ end
18
+
19
+ def call(object, context)
20
+ new(object, context).to_h
21
+ end
22
+ end
23
+
24
+ module InstanceMethods
25
+ attr_reader :jat_class, :object, :context
26
+
27
+ def initialize(object, context)
28
+ @object = object
29
+ @context = context
30
+ @jat_class = self.class.jat_class
31
+ end
32
+
33
+ def to_h
34
+ data, includes = data_with_includes
35
+ meta = document_meta
36
+ links = document_links
37
+ jsonapi = jsonapi_data
38
+
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
47
+
48
+ private
49
+
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]
55
+ end
56
+
57
+ def many(objects, includes, map)
58
+ objects.map { |object| one(object, includes, map) }
59
+ end
60
+
61
+ def one(object, includes, map)
62
+ jat_class::ResponsePiece.call(object, context, map, includes)
63
+ end
64
+
65
+ def many?(data, context)
66
+ many = context[:many]
67
+ many.nil? ? data.is_a?(Enumerable) : many
68
+ end
69
+
70
+ def jsonapi_data
71
+ combine(jat_class.jsonapi_data, context_jsonapi)
72
+ end
73
+
74
+ def document_links
75
+ combine(jat_class.document_links, context_links)
76
+ end
77
+
78
+ def document_meta
79
+ combine(jat_class.added_document_meta, context_meta)
80
+ end
81
+
82
+ def combine(attributes, context_data)
83
+ return context_data if attributes.empty?
84
+
85
+ data = context_data
86
+
87
+ attributes.each do |name, attribute|
88
+ next if data.key?(name)
89
+
90
+ value = attribute.value(object, context)
91
+
92
+ unless value.nil?
93
+ data = data.dup if data.equal?(FROZEN_EMPTY_HASH)
94
+ data[name] = value
95
+ end
96
+ end
97
+
98
+ data
99
+ end
100
+
101
+ def context_jsonapi
102
+ context_attr_transform(:jsonapi)
103
+ end
104
+
105
+ def context_links
106
+ context_attr_transform(:links)
107
+ end
108
+
109
+ def context_meta
110
+ context_attr_transform(:meta)
111
+ end
112
+
113
+ def context_attr_transform(key)
114
+ context[key]&.transform_keys(&:to_sym) || FROZEN_EMPTY_HASH
115
+ end
116
+ end
117
+
118
+ extend ClassMethods
119
+ include InstanceMethods
120
+ end
121
+ end
122
+ end
123
+ 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 unless links
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 unless meta
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 :_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 :_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 :_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
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ class Jat
6
+ module Plugins
7
+ module JsonApiPreloads
8
+ class Preloads
9
+ class << self
10
+ def call(jat)
11
+ new(jat.map).for(jat.class)
12
+ end
13
+ end
14
+
15
+ def initialize(current_map)
16
+ @used = Set.new
17
+ @current_map = current_map
18
+ end
19
+
20
+ def for(jat_class)
21
+ result = {}
22
+ append(result, jat_class)
23
+ result
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :current_map, :used
29
+
30
+ def append(result, jat_class)
31
+ attrs = current_map[jat_class.get_type]
32
+
33
+ add_attributes(result, jat_class, attrs[:attributes])
34
+ add_attributes(result, jat_class, attrs[:relationships])
35
+ end
36
+
37
+ def add_attributes(result, jat_class, attributes_names)
38
+ attributes_names.each do |name|
39
+ next unless used.add?([jat_class, name]) # Protection from recursive preloads
40
+
41
+ attribute = jat_class.attributes[name]
42
+ preloads = attribute.preloads
43
+ next unless preloads # we should not add preloads and nested preloads when nil provided
44
+
45
+ merge(result, deep_dup(preloads)) unless preloads.empty?
46
+ add_nested_preloads(result, attribute) if attribute.relation?
47
+ end
48
+ end
49
+
50
+ def add_nested_preloads(result, attribute)
51
+ path = attribute.preloads_path
52
+ nested_result = nested(result, path)
53
+ nested_serializer = attribute.serializer.call
54
+
55
+ append(nested_result, nested_serializer)
56
+ end
57
+
58
+ def merge(result, preloads)
59
+ result.merge!(preloads) do |_key, value_one, value_two|
60
+ merge(value_one, value_two)
61
+ end
62
+ end
63
+
64
+ def deep_dup(preloads)
65
+ preloads.dup.transform_values! do |nested_preloads|
66
+ deep_dup(nested_preloads)
67
+ end
68
+ end
69
+
70
+ def nested(result, path)
71
+ !path || path.empty? ? result : result.dig(*path)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./lib/params_error"
4
+ require_relative "./lib/validate_fields_param"
5
+ require_relative "./lib/validate_include_param"
6
+
7
+ class Jat
8
+ module Plugins
9
+ module JsonApiValidateParams
10
+ def self.plugin_name
11
+ :json_api_validate_params
12
+ end
13
+
14
+ def self.before_load(jat_class, **_opts)
15
+ return if jat_class.plugin_used?(:json_api)
16
+ raise Error, "Please load :json_api plugin first"
17
+ end
18
+
19
+ def self.load(jat_class, **_opts)
20
+ jat_class.extend(ClassMethods)
21
+ jat_class.include(InstanceMethods)
22
+
23
+ jat_class::FieldsParamParser.extend(FieldsParamParserClassMethods)
24
+ jat_class::IncludeParamParser.extend(IncludeParamParserClassMethods)
25
+ end
26
+
27
+ module InstanceMethods
28
+ def validate
29
+ @validate ||= self.class.validate(context)
30
+ end
31
+ end
32
+
33
+ module ClassMethods
34
+ def validate(context)
35
+ # Generate map for current context.
36
+ # Params are valid if no errors were raised
37
+ map(context)
38
+ true
39
+ end
40
+ end
41
+
42
+ module IncludeParamParserClassMethods
43
+ private
44
+
45
+ def parse_to_nested_hash(*)
46
+ super.tap { |result| ValidateIncludeParam.call(jat_class, result) }
47
+ end
48
+ end
49
+
50
+ module FieldsParamParserClassMethods
51
+ private
52
+
53
+ def parse_to_nested_hash(*)
54
+ super.tap { |result| ValidateFieldsParam.call(jat_class, result) }
55
+ end
56
+ end
57
+ end
58
+
59
+ register_plugin(JsonApiValidateParams.plugin_name, JsonApiValidateParams)
60
+ end
61
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ class JsonApiParamsError < Error
5
+ end
6
+ end