jat 0.0.5 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +5 -16
  3. data/lib/jat/config.rb +4 -7
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads.rb → base/base_activerecord_preloads/base_activerecord_preloads.rb} +7 -2
  6. data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +16 -4
  7. data/lib/jat/plugins/{_lower_camel_case/_lower_camel_case.rb → base/base_lower_camel_case/base_lower_camel_case.rb} +6 -2
  8. data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +6 -2
  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 +5 -1
  12. data/lib/jat/plugins/json_api/json_api.rb +7 -3
  13. data/lib/jat/plugins/json_api/lib/map.rb +4 -5
  14. data/lib/jat/plugins/json_api/lib/response.rb +1 -5
  15. data/lib/jat/plugins/json_api/lib/response_piece.rb +19 -10
  16. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  17. data/lib/jat/plugins/{json_api_lower_camel_case → json_api/plugins/json_api_lower_camel_case}/json_api_lower_camel_case.rb +6 -2
  18. data/lib/jat/plugins/{json_api_maps_cache → json_api/plugins/json_api_maps_cache}/json_api_maps_cache.rb +5 -1
  19. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  20. data/lib/jat/plugins/{json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +1 -1
  21. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/json_api_validate_params.rb +5 -1
  22. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/lib/params_error.rb +0 -0
  23. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/lib/validate_fields_param.rb +0 -0
  24. data/lib/jat/plugins/{json_api_validate_params → json_api/plugins/json_api_validate_params}/lib/validate_include_param.rb +1 -3
  25. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  26. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  27. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  28. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +20 -36
  29. data/lib/jat/plugins/simple_api/lib/map.rb +4 -5
  30. data/lib/jat/plugins/simple_api/lib/response.rb +1 -5
  31. data/lib/jat/plugins/simple_api/lib/response_piece.rb +1 -5
  32. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  33. data/lib/jat/plugins/{simple_api_lower_camel_case → simple_api/plugins/simple_api_lower_camel_case}/simple_api_lower_camel_case.rb +6 -2
  34. data/lib/jat/plugins/{simple_api_maps_cache → simple_api/plugins/simple_api_maps_cache}/simple_api_maps_cache.rb +6 -4
  35. data/lib/jat/plugins/{simple_api_activerecord → simple_api/plugins/simple_api_preloads}/lib/preloads.rb +1 -1
  36. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  37. data/lib/jat/plugins/{simple_api_validate_params → simple_api/plugins/simple_api_validate_params}/lib/fields_error.rb +0 -0
  38. data/lib/jat/plugins/{simple_api_validate_params → simple_api/plugins/simple_api_validate_params}/lib/validate_fields_param.rb +0 -0
  39. data/lib/jat/plugins/{simple_api_validate_params → simple_api/plugins/simple_api_validate_params}/simple_api_validate_params.rb +6 -2
  40. data/lib/jat/plugins/simple_api/simple_api.rb +8 -7
  41. data/lib/jat/plugins/to_str/to_str.rb +5 -1
  42. data/lib/jat/plugins/types/types.rb +55 -0
  43. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  44. data/lib/jat/plugins.rb +41 -15
  45. data/lib/jat.rb +24 -17
  46. data/test/lib/jat/attribute_test.rb +8 -0
  47. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  48. data/test/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads_test.rb → base_activerecord_preloads/base_activerecord_preloads_test.rb} +3 -3
  49. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +2 -2
  50. data/test/lib/jat/plugins/{_camel_lower/_camel_lower_test.rb → base_lower_camel_case/base_lower_camel_case_test.rb} +2 -2
  51. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  52. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  53. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  54. data/test/lib/jat/plugins/json_api/json_api_test.rb +1 -9
  55. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +20 -12
  56. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +25 -11
  57. data/test/lib/jat/plugins/json_api/lib/map_test.rb +132 -124
  58. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  59. data/test/lib/jat/plugins/json_api/lib/response_test.rb +6 -1
  60. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +4 -18
  61. data/test/lib/jat/plugins/{json_api_camel_lower/json_api_camel_lower_test.rb → json_api_lower_camel_case/json_api_lower_camel_case_test.rb} +6 -0
  62. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
  63. data/test/lib/jat/plugins/{json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +8 -6
  64. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +30 -6
  65. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  66. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  67. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  68. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +57 -43
  69. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +90 -82
  70. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  71. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +18 -12
  72. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +33 -6
  73. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +4 -18
  74. data/test/lib/jat/plugins/{simple_api_camel_lower/simple_api_camel_lower_test.rb → simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb} +7 -1
  75. data/test/lib/jat/plugins/{simple_api_activerecord → simple_api_preloads}/lib/preloads_test.rb +8 -8
  76. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
  77. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +12 -0
  78. data/test/lib/jat/plugins/types/types_test.rb +83 -0
  79. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  80. data/test/lib/jat/plugins_test.rb +88 -0
  81. data/test/test_helper.rb +8 -4
  82. metadata +98 -82
  83. data/lib/jat/plugins/json_api_activerecord/json_api_activerecord.rb +0 -31
  84. data/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 405a0677976675d336278af6aa940fe83ac4bdd4f9d69263fb4728f5cb1c8b3e
4
- data.tar.gz: 9f7e0ef9238788984a50b677b68876d7301eae6e020df469ec7eba933566dbf0
3
+ metadata.gz: 0b59dbef3044d5b23c7b8178e23f931458b3acb5ced78b3750c5f1d7bb8ea01e
4
+ data.tar.gz: 7ebf0b1ec19a10872b8e659bd3ab9cd45a5859ace34f87c65070a83af713a2fd
5
5
  SHA512:
6
- metadata.gz: dd533f933d3504b8b549ac13f231ee70bfa7a8c00baafe88ab09a8973a6d278d8a727ddbd0e4e783abed34b0e8f22c3ccb314ced10699427de80877865a9ff0d
7
- data.tar.gz: 323559cf6470e9c569f919356f2c0c0033b46e51ccb0b9c1b79b9e0d5d397d68b1322a2401788560f1ed5a474cbf19a81720f38ed78291e1c75326ec6f9a7a07
6
+ metadata.gz: e7e4b943c4b6452937d2c025f47bc1e3e4484151748139682ad03724fa3bc5138f02c23400426ceb3fb6c948112dd91015c1beddfd4da2ee3a1cd3187e0a5d5c
7
+ data.tar.gz: de404f4d77ca9b68a50834b4e435f303f1ace3f66bb976ffa154e4159b01888fa27301795192c967a7cc3b2577c35a10012135521f7e91e9037833d7098f7816
data/lib/jat/attribute.rb CHANGED
@@ -9,6 +9,8 @@ class Jat
9
9
  attr_reader :params, :opts
10
10
 
11
11
  def initialize(name:, opts: {}, block: nil)
12
+ check_block_valid(block)
13
+
12
14
  @opts = EnumDeepDup.call(opts)
13
15
  @params = EnumDeepFreeze.call(name: name, opts: @opts, block: block)
14
16
  end
@@ -61,10 +63,10 @@ class Jat
61
63
  def block
62
64
  return @block if instance_variable_defined?(:@block)
63
65
 
64
- current_block = params.fetch(:block).tap { |bl| check_block_valid(bl) if bl }
66
+ current_block = params.fetch(:block)
65
67
  current_block ||= keyword_block
66
68
 
67
- @block = anonymized_block(current_block)
69
+ @block = current_block
68
70
  end
69
71
 
70
72
  def value(object, context)
@@ -78,24 +80,11 @@ class Jat
78
80
  proc { |object| object.public_send(key_method_name) }
79
81
  end
80
82
 
81
- def anonymized_block(block)
82
- Class.new do
83
- private
84
-
85
- define_method(:_doe) do |object, context|
86
- block.call(object, context)
87
- end
88
- end.new.method(:_doe)
89
- end
90
-
91
83
  def check_block_valid(block)
92
- raise Error, "Block must be a Proc (not lambda)" if block.lambda?
84
+ return unless block
93
85
 
94
86
  params = block.parameters
95
87
  raise Error, "Block can have 0-2 parameters" if params.count > 2
96
-
97
- valid_params_types = params.all? { |param| param[0] == :opt }
98
- raise Error, "Block parameters must be optional and no keyword parameters" unless valid_params_types
99
88
  end
100
89
  end
101
90
 
data/lib/jat/config.rb CHANGED
@@ -1,23 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "forwardable"
3
4
  require_relative "utils/enum_deep_dup"
4
5
 
5
6
  class Jat
6
7
  class Config
7
8
  module InstanceMethods
9
+ extend Forwardable
10
+
8
11
  attr_reader :opts
9
12
 
10
13
  def initialize(opts = {})
11
14
  @opts = EnumDeepDup.call(opts)
12
15
  end
13
16
 
14
- def []=(key, value)
15
- opts[key] = value
16
- end
17
-
18
- def [](key)
19
- opts[key]
20
- end
17
+ def_delegators :@opts, :[], :[]=, :fetch
21
18
  end
22
19
 
23
20
  module ClassMethods
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module Activerecord
6
+ def self.plugin_name
7
+ :activerecord
8
+ end
9
+
10
+ def self.load(jat_class, **opts)
11
+ if jat_class.plugin_used?(:json_api)
12
+ jat_class.plugin :json_api_activerecord, **opts
13
+ elsif jat_class.plugin_used?(:simple_api)
14
+ jat_class.plugin :simple_api_activerecord, **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(Activerecord.plugin_name, Activerecord)
22
+ end
23
+ end
@@ -4,7 +4,11 @@ require_relative "./lib/preloader"
4
4
 
5
5
  class Jat
6
6
  module Plugins
7
- module ActiverecordPreloads
7
+ module BaseActiverecordPreloads
8
+ def self.plugin_name
9
+ :base_activerecord_preloads
10
+ end
11
+
8
12
  def self.load(jat_class, **_opts)
9
13
  jat_class.include(InstanceMethods)
10
14
  end
@@ -20,6 +24,7 @@ class Jat
20
24
  def add_preloads(obj)
21
25
  return obj if obj.nil? || (obj.is_a?(Array) && obj.empty?)
22
26
 
27
+ # preloads() method comes from simple_api_activerecord or json_api_activerecord plugin
23
28
  preloads = preloads()
24
29
  return obj if preloads.empty?
25
30
 
@@ -28,6 +33,6 @@ class Jat
28
33
  end
29
34
  end
30
35
 
31
- register_plugin(:_activerecord_preloads, ActiverecordPreloads)
36
+ register_plugin(BaseActiverecordPreloads.plugin_name, BaseActiverecordPreloads)
32
37
  end
33
38
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module ActiverecordPreloads
5
+ module BaseActiverecordPreloads
6
6
  class Preloader
7
7
  module ClassMethods
8
8
  def preload(object, preloads)
@@ -20,6 +20,18 @@ class Jat
20
20
  extend ClassMethods
21
21
  end
22
22
 
23
+ class Loader
24
+ # :nocov: We can check only one version of activerecord
25
+ def self.call(records, associations)
26
+ if ActiveRecord::VERSION::MAJOR >= 7
27
+ ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
28
+ else
29
+ ActiveRecord::Associations::Preloader.new.preload(records, associations)
30
+ end
31
+ end
32
+ # :nocov:
33
+ end
34
+
23
35
  class ActiverecordObject
24
36
  module ClassMethods
25
37
  def fit?(object)
@@ -27,7 +39,7 @@ class Jat
27
39
  end
28
40
 
29
41
  def preload(object, preloads)
30
- ActiveRecord::Associations::Preloader.new.preload(object, preloads)
42
+ Loader.call([object], preloads)
31
43
  object
32
44
  end
33
45
  end
@@ -44,7 +56,7 @@ class Jat
44
56
  def preload(objects, preloads)
45
57
  if objects.loaded?
46
58
  array_objects = objects.to_a
47
- ActiverecordArray.preload(array_objects, preloads)
59
+ Loader.call(array_objects, preloads)
48
60
  objects
49
61
  else
50
62
  objects.preload(preloads).load
@@ -64,7 +76,7 @@ class Jat
64
76
  end
65
77
 
66
78
  def preload(objects, preloads)
67
- ActiveRecord::Associations::Preloader.new.preload(objects, preloads)
79
+ Loader.call(objects, preloads)
68
80
  objects
69
81
  end
70
82
 
@@ -2,7 +2,11 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module LowerCamelCase
5
+ module BaseLowerCamelCase
6
+ def self.plugin_name
7
+ :base_lower_camel_case
8
+ end
9
+
6
10
  def self.load(jat_class, **_opts)
7
11
  jat_class::Attribute.include(AttributeInstanceMethods)
8
12
  end
@@ -14,7 +18,7 @@ class Jat
14
18
  end
15
19
  end
16
20
 
17
- register_plugin(:_lower_camel_case, LowerCamelCase)
21
+ register_plugin(BaseLowerCamelCase.plugin_name, BaseLowerCamelCase)
18
22
  end
19
23
 
20
24
  class LowerCamelCaseTransformation
@@ -6,7 +6,11 @@ require_relative "./lib/preloads_with_path"
6
6
  # This plugin adds attribute methods #preloads, #preloads_path
7
7
  class Jat
8
8
  module Plugins
9
- module Preloads
9
+ module BasePreloads
10
+ def self.plugin_name
11
+ :base_preloads
12
+ end
13
+
10
14
  def self.load(jat_class, **_opts)
11
15
  jat_class::Attribute.include(AttributeMethods)
12
16
  end
@@ -54,6 +58,6 @@ class Jat
54
58
  end
55
59
  end
56
60
 
57
- register_plugin(:_preloads, Preloads)
61
+ register_plugin(BasePreloads.plugin_name, BasePreloads)
58
62
  end
59
63
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module Preloads
5
+ module BasePreloads
6
6
  class FormatUserPreloads
7
7
  METHODS = {
8
8
  Array => :array_to_hash,
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module Preloads
5
+ module BasePreloads
6
6
  class PreloadsWithPath
7
7
  module ClassMethods
8
8
  BANG = "!"
@@ -3,6 +3,10 @@
3
3
  class Jat
4
4
  module Plugins
5
5
  module Cache
6
+ def self.plugin_name
7
+ :cache
8
+ end
9
+
6
10
  def self.before_load(jat_class, **opts)
7
11
  jat_class.plugin :to_str, **opts
8
12
  end
@@ -38,6 +42,6 @@ class Jat
38
42
  end
39
43
  end
40
44
 
41
- register_plugin(:cache, Cache)
45
+ register_plugin(Cache.plugin_name, Cache)
42
46
  end
43
47
  end
@@ -10,6 +10,10 @@ require_relative "./lib/response_piece"
10
10
  class Jat
11
11
  module Plugins
12
12
  module JsonApi
13
+ def self.plugin_name
14
+ :json_api
15
+ end
16
+
13
17
  def self.before_load(jat_class, **_opts)
14
18
  response_plugin = jat_class.config[:response_plugin_loaded]
15
19
  return unless response_plugin
@@ -23,6 +27,8 @@ class Jat
23
27
  end
24
28
 
25
29
  def self.after_load(jat_class, **opts)
30
+ jat_class.config[:response_plugin_loaded] = plugin_name
31
+
26
32
  fields_parser_class = Class.new(FieldsParamParser)
27
33
  fields_parser_class.jat_class = jat_class
28
34
  jat_class.const_set(:FieldsParamParser, fields_parser_class)
@@ -43,8 +49,6 @@ class Jat
43
49
  response_piece_class.jat_class = jat_class
44
50
  jat_class.const_set(:ResponsePiece, response_piece_class)
45
51
 
46
- jat_class.config[:response_plugin_loaded] = :json_api
47
- jat_class.plugin(:json_api_activerecord, **opts) if opts[:activerecord]
48
52
  jat_class.id
49
53
  end
50
54
 
@@ -242,6 +246,6 @@ class Jat
242
246
  end
243
247
  end
244
248
 
245
- register_plugin(:json_api, JsonApi)
249
+ register_plugin(JsonApi.plugin_name, JsonApi)
246
250
  end
247
251
  end
@@ -103,11 +103,10 @@ class Jat
103
103
  end
104
104
 
105
105
  def attribute_exposed?(attribute)
106
- case exposed
107
- when :all then true
108
- when :none then false
109
- else attribute.exposed?
110
- end
106
+ return true if exposed == :all
107
+ return false if exposed == :none
108
+
109
+ attribute.exposed?
111
110
  end
112
111
  end
113
112
 
@@ -87,7 +87,7 @@ class Jat
87
87
  attributes.each do |name, attribute|
88
88
  next if data.key?(name)
89
89
 
90
- value = attribute_value(attribute)
90
+ value = attribute.value(object, context)
91
91
 
92
92
  unless value.nil?
93
93
  data = data.dup if data.equal?(FROZEN_EMPTY_HASH)
@@ -98,10 +98,6 @@ class Jat
98
98
  data
99
99
  end
100
100
 
101
- def attribute_value(attribute)
102
- attribute.block.call(object, context)
103
- end
104
-
105
101
  def context_jsonapi
106
102
  context_attr_transform(:jsonapi)
107
103
  end
@@ -49,7 +49,7 @@ class Jat
49
49
  end
50
50
 
51
51
  def uid
52
- {type: jat_class.get_type, id: jat_class.get_id.block.call(object, context)}
52
+ {type: type, id: id}
53
53
  end
54
54
 
55
55
  private
@@ -60,7 +60,7 @@ class Jat
60
60
 
61
61
  attributes_names.each_with_object({}) do |name, attrs|
62
62
  attribute = jat_class.attributes[name]
63
- attrs[name] = attribute.block.call(object, context)
63
+ attrs[name] = attribute.value(object, context)
64
64
  end
65
65
  end
66
66
 
@@ -70,7 +70,7 @@ class Jat
70
70
 
71
71
  relationships_names.each_with_object({}) do |name, rels|
72
72
  rel_attribute = jat_class.attributes[name]
73
- rel_object = rel_attribute.block.call(object, context)
73
+ rel_object = rel_attribute.value(object, context)
74
74
 
75
75
  rel_serializer = rel_attribute.serializer.call
76
76
  rel_links = get_relationship_links(rel_serializer, rel_object)
@@ -105,7 +105,8 @@ class Jat
105
105
  def add_relationship_data(rel_serializer, rel_object)
106
106
  rel_response_data = rel_serializer::ResponsePiece.new(rel_object, context, map, includes)
107
107
  rel_uid = rel_response_data.uid
108
- includes[rel_uid] ||= rel_response_data.to_h
108
+ simple_uid = "#{rel_uid[:id]}-#{rel_uid[:type]}"
109
+ includes[simple_uid] ||= rel_response_data.to_h
109
110
  rel_uid
110
111
  end
111
112
 
@@ -122,40 +123,48 @@ class Jat
122
123
  def get_links
123
124
  jat_class
124
125
  .object_links
125
- .transform_values { |attr| attr.block.call(object, context) }
126
+ .transform_values { |attr| attr.value(object, context) }
126
127
  .tap(&:compact!)
127
128
  end
128
129
 
129
130
  def get_meta
130
131
  jat_class
131
132
  .added_object_meta
132
- .transform_values { |attr| attr.block.call(object, context) }
133
+ .transform_values { |attr| attr.value(object, context) }
133
134
  .tap(&:compact!)
134
135
  end
135
136
 
136
137
  def get_relationship_links(rel_serializer, rel_object)
137
138
  links = rel_serializer.relationship_links
138
- return FROZEN_EMPTY_HASH unless links
139
+ return FROZEN_EMPTY_HASH if links.empty?
139
140
 
140
141
  context[:parent_object] = object
141
142
 
142
143
  links
143
- .transform_values { |attr| attr.block.call(rel_object, context) }
144
+ .transform_values { |attr| attr.value(rel_object, context) }
144
145
  .tap(&:compact!)
145
146
  .tap { context.delete(:parent_object) }
146
147
  end
147
148
 
148
149
  def get_relationship_meta(rel_serializer, rel_object)
149
150
  meta = rel_serializer.added_relationship_meta
150
- return FROZEN_EMPTY_HASH unless meta
151
+ return FROZEN_EMPTY_HASH if meta.empty?
151
152
 
152
153
  context[:parent_object] = object
153
154
 
154
155
  meta
155
- .transform_values { |attr| attr.block.call(rel_object, context) }
156
+ .transform_values { |attr| attr.value(rel_object, context) }
156
157
  .tap(&:compact!)
157
158
  .tap { context.delete(:parent_object) }
158
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
159
168
  end
160
169
 
161
170
  extend ClassMethods
@@ -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
@@ -3,10 +3,14 @@
3
3
  class Jat
4
4
  module Plugins
5
5
  module JsonApiLowerCamelCase
6
+ def self.plugin_name
7
+ :json_api_lower_camel_case
8
+ end
9
+
6
10
  def self.before_load(jat_class, **_opts)
7
11
  raise Error, "Please load :json_api plugin first" unless jat_class.plugin_used?(:json_api)
8
12
 
9
- jat_class.plugin :_lower_camel_case
13
+ jat_class.plugin :base_lower_camel_case
10
14
  end
11
15
 
12
16
  def self.load(jat_class, **_opts)
@@ -25,6 +29,6 @@ class Jat
25
29
  end
26
30
  end
27
31
 
28
- register_plugin(:json_api_lower_camel_case, JsonApiLowerCamelCase)
32
+ register_plugin(JsonApiLowerCamelCase.plugin_name, JsonApiLowerCamelCase)
29
33
  end
30
34
  end
@@ -3,6 +3,10 @@
3
3
  class Jat
4
4
  module Plugins
5
5
  module JsonApiMapsCache
6
+ def self.plugin_name
7
+ :json_api_maps_cache
8
+ end
9
+
6
10
  def self.before_load(jat_class, **opts)
7
11
  return if jat_class.plugin_used?(:json_api)
8
12
  raise Error, "Please load :json_api plugin first"
@@ -49,6 +53,6 @@ class Jat
49
53
  end
50
54
  end
51
55
 
52
- register_plugin(:json_api_maps_cache, JsonApiMapsCache)
56
+ register_plugin(JsonApiMapsCache.plugin_name, JsonApiMapsCache)
53
57
  end
54
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
@@ -4,7 +4,7 @@ require "set"
4
4
 
5
5
  class Jat
6
6
  module Plugins
7
- module JsonApiActiverecord
7
+ module JsonApiPreloads
8
8
  class Preloads
9
9
  class << self
10
10
  def call(jat)
@@ -7,6 +7,10 @@ require_relative "./lib/validate_include_param"
7
7
  class Jat
8
8
  module Plugins
9
9
  module JsonApiValidateParams
10
+ def self.plugin_name
11
+ :json_api_validate_params
12
+ end
13
+
10
14
  def self.before_load(jat_class, **_opts)
11
15
  return if jat_class.plugin_used?(:json_api)
12
16
  raise Error, "Please load :json_api plugin first"
@@ -52,6 +56,6 @@ class Jat
52
56
  end
53
57
  end
54
58
 
55
- register_plugin(:json_api_validate_params, JsonApiValidateParams)
59
+ register_plugin(JsonApiValidateParams.plugin_name, JsonApiValidateParams)
56
60
  end
57
61
  end
@@ -22,9 +22,7 @@ class Jat
22
22
  allowed_relationships = jat_class.attributes.each_value.select(&:relation?).map!(&:name)
23
23
  allowed_relationships = "'#{allowed_relationships.join("', '")}'"
24
24
 
25
- raise JsonApiParamsError,
26
- "Type '#{type}' has no included '#{name}' relationship. " \
27
- "Existing relationships are: #{allowed_relationships}"
25
+ raise JsonApiParamsError, "Type '#{type}' has no included '#{name}' relationship. Existing relationships are: #{allowed_relationships}"
28
26
  end
29
27
  end
30
28
  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