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
@@ -1,60 +1,52 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "set"
4
+
3
5
  class Jat
4
6
  module Plugins
5
- module JsonApiActiverecord
7
+ module JsonApiPreloads
6
8
  class Preloads
7
9
  class << self
8
10
  def call(jat)
9
- new(jat.traversal_map.current).for(jat.class)
11
+ new(jat.map).for(jat.class)
10
12
  end
11
13
  end
12
14
 
13
- attr_reader :initial_result
14
-
15
15
  def initialize(current_map)
16
+ @used = Set.new
16
17
  @current_map = current_map
17
- @initial_result = {}
18
18
  end
19
19
 
20
20
  def for(jat_class)
21
- @initial_result = {}
22
- append(initial_result, jat_class)
23
- initial_result
24
- rescue SystemStackError
25
- raise Error, "Stack level too deep, recursive preloads detected: #{initial_result}"
21
+ result = {}
22
+ append(result, jat_class)
23
+ result
26
24
  end
27
25
 
28
26
  private
29
27
 
30
- attr_reader :current_map
28
+ attr_reader :current_map, :used
31
29
 
32
30
  def append(result, jat_class)
33
- attrs = current_map[jat_class.type]
34
- attributes_names = attrs[:attributes] + attrs[:relationships]
31
+ attrs = current_map[jat_class.get_type]
35
32
 
36
- add_attributes(result, jat_class, attributes_names)
33
+ add_attributes(result, jat_class, attrs[:attributes])
34
+ add_attributes(result, jat_class, attrs[:relationships])
37
35
  end
38
36
 
39
37
  def add_attributes(result, jat_class, attributes_names)
40
38
  attributes_names.each do |name|
39
+ next unless used.add?([jat_class, name]) # Protection from recursive preloads
40
+
41
41
  attribute = jat_class.attributes[name]
42
42
  preloads = attribute.preloads
43
- next unless preloads # we should not addd preloads and nested preloads when nil provided
43
+ next unless preloads # we should not add preloads and nested preloads when nil provided
44
44
 
45
- add_preloads(result, preloads, attribute)
45
+ merge(result, deep_dup(preloads)) unless preloads.empty?
46
+ add_nested_preloads(result, attribute) if attribute.relation?
46
47
  end
47
48
  end
48
49
 
49
- def add_preloads(result, preloads, attribute)
50
- unless preloads.empty?
51
- preloads = deep_dup(preloads)
52
- merge(result, preloads)
53
- end
54
-
55
- add_nested_preloads(result, attribute) if attribute.relation?
56
- end
57
-
58
50
  def add_nested_preloads(result, attribute)
59
51
  path = attribute.preloads_path
60
52
  nested_result = nested(result, path)
@@ -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
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApiValidateParams
6
+ class ValidateFieldsParam
7
+ def self.call(jat_class, fields)
8
+ full_map = jat_class.map_full
9
+
10
+ fields.each do |type, attributes_names|
11
+ new(type, full_map).validate(attributes_names)
12
+ end
13
+ end
14
+
15
+ attr_reader :type, :full_map
16
+
17
+ def initialize(type, full_map)
18
+ @type = type
19
+ @full_map = full_map
20
+ end
21
+
22
+ def validate(attributes_names)
23
+ check_fields_type
24
+ check_attributes_names(attributes_names)
25
+ end
26
+
27
+ private
28
+
29
+ def check_fields_type
30
+ return if full_map.key?(type)
31
+
32
+ allowed_types = "'#{full_map.keys.join("', '")}'"
33
+
34
+ raise JsonApiParamsError, <<~ERROR.strip
35
+ Response does not have resources with type '#{type}'. Existing types are: #{allowed_types}
36
+ ERROR
37
+ end
38
+
39
+ def check_attributes_names(attributes_names)
40
+ attributes_names.each do |attribute_name|
41
+ check_attribute_name(attribute_name)
42
+ end
43
+ end
44
+
45
+ def check_attribute_name(attribute_name)
46
+ type_data = full_map.fetch(type)
47
+ type_serializer = type_data.fetch(:serializer)
48
+ return if type_serializer.attributes.key?(attribute_name)
49
+
50
+ allowed_attributes = "'#{type_serializer.attributes.keys.join("', '")}'"
51
+
52
+ raise JsonApiParamsError, <<~ERROR.strip
53
+ No attribute '#{attribute_name}' in resource type '#{type}'. Existing attributes are: #{allowed_attributes}
54
+ ERROR
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module JsonApiValidateParams
6
+ class ValidateIncludeParam
7
+ class << self
8
+ def call(jat_class, includes)
9
+ includes.each do |name, nested_includes|
10
+ attribute = jat_class.attributes[name]
11
+ raise_error(jat_class, name) if !attribute || !attribute.relation?
12
+
13
+ nested_serializer = attribute.serializer.call
14
+ call(nested_serializer, nested_includes)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def raise_error(jat_class, name)
21
+ type = jat_class.get_type
22
+ allowed_relationships = jat_class.attributes.each_value.select(&:relation?).map!(&:name)
23
+ allowed_relationships = "'#{allowed_relationships.join("', '")}'"
24
+
25
+ raise JsonApiParamsError,
26
+ "Type '#{type}' has no included '#{name}' relationship. " \
27
+ "Existing relationships are: #{allowed_relationships}"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module LowerCamelCase
6
+ def self.plugin_name
7
+ :lower_camel_case
8
+ end
9
+
10
+ def self.load(jat_class, **opts)
11
+ if jat_class.plugin_used?(:json_api)
12
+ jat_class.plugin :json_api_lower_camel_case, **opts
13
+ elsif jat_class.plugin_used?(:simple_api)
14
+ jat_class.plugin :simple_api_lower_camel_case, **opts
15
+ else
16
+ raise Error, "Please load :json_api or :simple_api plugin first"
17
+ end
18
+ end
19
+ end
20
+
21
+ register_plugin(LowerCamelCase.plugin_name, LowerCamelCase)
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module MapsCache
6
+ def self.plugin_name
7
+ :maps_cache
8
+ end
9
+
10
+ def self.load(jat_class, **opts)
11
+ if jat_class.plugin_used?(:json_api)
12
+ jat_class.plugin :json_api_maps_cache, **opts
13
+ elsif jat_class.plugin_used?(:simple_api)
14
+ jat_class.plugin :simple_api_maps_cache, **opts
15
+ else
16
+ raise Error, "Please load :json_api or :simple_api plugin first"
17
+ end
18
+ end
19
+ end
20
+
21
+ register_plugin(MapsCache.plugin_name, MapsCache)
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module Preloads
6
+ def self.plugin_name
7
+ :preloads
8
+ end
9
+
10
+ def self.load(jat_class, **opts)
11
+ if jat_class.plugin_used?(:json_api)
12
+ jat_class.plugin :json_api_preloads, **opts
13
+ elsif jat_class.plugin_used?(:simple_api)
14
+ jat_class.plugin :simple_api_preloads, **opts
15
+ else
16
+ raise Error, "Please load :json_api or :simple_api plugin first"
17
+ end
18
+ end
19
+ end
20
+
21
+ register_plugin(Preloads.plugin_name, Preloads)
22
+ end
23
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module SimpleApi
6
+ class FieldsParamParser
7
+ module ClassMethods
8
+ # Returns the Jat class that this FieldsParamParser class is namespaced under.
9
+ attr_accessor :jat_class
10
+
11
+ # Since FieldsParamParser 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}::FieldsParamParser"
16
+ end
17
+
18
+ def parse(fields)
19
+ return FROZEN_EMPTY_HASH unless fields
20
+
21
+ new(fields).parse
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ COMMA = ","
27
+ OPEN_BRACKET = "("
28
+ CLOSE_BRACKET = ")"
29
+
30
+ def initialize(fields)
31
+ @fields = fields
32
+ end
33
+
34
+ # user => { user: {} }
35
+ # user(id) => { user: { id: {} } }
36
+ # user(id,name) => { user: { id: {}, name: {} } }
37
+ # user,comments => { user: {}, comments: {} }
38
+ # user(comments(text)) => { user: { comments: { text: {} } } }
39
+ def parse
40
+ current_attr = nil
41
+
42
+ fields.each_char do |char|
43
+ case char
44
+ when COMMA
45
+ next unless current_attr
46
+
47
+ add_attribute(current_attr)
48
+ current_attr = nil
49
+ when CLOSE_BRACKET
50
+ if current_attr
51
+ add_attribute(current_attr)
52
+ current_attr = nil
53
+ end
54
+
55
+ route.pop
56
+ when OPEN_BRACKET
57
+ next unless current_attr
58
+
59
+ attribute_name = add_attribute(current_attr, {})
60
+ route << attribute_name
61
+ current_attr = nil
62
+ else
63
+ current_attr = current_attr ? current_attr.insert(-1, char) : char
64
+ end
65
+ end
66
+
67
+ add_attribute(current_attr) if current_attr
68
+
69
+ res
70
+ end
71
+
72
+ private
73
+
74
+ attr_reader :fields
75
+
76
+ def add_attribute(current_attr, nested_attrs = FROZEN_EMPTY_HASH)
77
+ current_resource = route.empty? ? res : res.dig(*route)
78
+ attribute_name = current_attr.strip.to_sym
79
+ current_resource[attribute_name] = nested_attrs
80
+ attribute_name
81
+ end
82
+
83
+ def res
84
+ @res ||= {}
85
+ end
86
+
87
+ def route
88
+ @route ||= []
89
+ end
90
+ end
91
+
92
+ extend ClassMethods
93
+ include InstanceMethods
94
+ end
95
+ end
96
+ end
97
+ end
@@ -1,28 +1,98 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "./params/parse"
4
- require_relative "./construct_traversal_map"
5
-
6
3
  class Jat
7
4
  module Plugins
8
5
  module SimpleApi
9
6
  class Map
10
- class << self
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
+
11
18
  # Returns structure like
12
19
  # {
13
20
  # key1 => { key11 => {}, key12 => { ... } },
14
21
  # key2 => { key21 => {}, key22 => { ... } },
15
22
  # }
16
- def call(jat)
17
- params = jat.context[:params]
18
- fields = params && (params[:fields] || params["fields"])
19
- exposed = jat.context[:exposed] || :default
23
+ def call(context)
24
+ exposed = context[:exposed]&.to_sym || :default
25
+ fields = context[:fields]
26
+
27
+ construct_map(exposed, fields)
28
+ end
29
+
30
+ private
31
+
32
+ def construct_map(exposed, fields)
33
+ fields = jat_class::FieldsParamParser.parse(fields) if fields
34
+ new(exposed, fields).to_h
35
+ end
36
+ end
37
+
38
+ module InstanceMethods
39
+ attr_reader :exposed, :fields
40
+
41
+ EXPOSED_TYPES = {all: :all, default: :default, none: :none}.freeze
42
+
43
+ def initialize(exposed, fields)
44
+ @exposed = EXPOSED_TYPES.fetch(exposed)
45
+ @fields = fields
46
+ end
47
+
48
+ def to_h
49
+ map_for(self.class.jat_class, fields)
50
+ end
51
+
52
+ def map_for(serializer, fields, stack = [])
53
+ serializer.attributes.each_with_object({}) do |name_attr, result|
54
+ name = name_attr[0]
55
+ attribute = name_attr[1]
56
+ next unless expose?(attribute, fields)
20
57
 
21
- manually_exposed = Params::Parse.new(fields).parse
58
+ raise Error, recursive_error_message(stack, name) if stack.any?(name_attr)
59
+ stack << name_attr
22
60
 
23
- ConstructTraversalMap.new(jat.class, exposed, manually_exposed: manually_exposed).to_h
61
+ result[name] =
62
+ if attribute.relation?
63
+ nested_serializer = attribute.serializer.call
64
+ nested_fields = fields&.[](name)
65
+ map_for(nested_serializer, nested_fields, stack)
66
+ else
67
+ FROZEN_EMPTY_HASH
68
+ end
69
+
70
+ stack.pop
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def expose?(attribute, fields)
77
+ case exposed
78
+ when :all then true
79
+ when :none then manually_exposed?(attribute, fields)
80
+ else attribute.exposed? || manually_exposed?(attribute, fields)
81
+ end
82
+ end
83
+
84
+ def manually_exposed?(attribute, fields)
85
+ fields&.include?(attribute.name)
86
+ end
87
+
88
+ def recursive_error_message(stack, name)
89
+ recursion = (stack.map!(&:first) << name).join(" -> ")
90
+ "Recursive serialization: #{recursion}"
24
91
  end
25
92
  end
93
+
94
+ extend ClassMethods
95
+ include InstanceMethods
26
96
  end
27
97
  end
28
98
  end