serega 0.12.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -0
- data/VERSION +1 -1
- data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +66 -0
- data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +35 -0
- data/lib/serega/plugins/preloads/lib/preloads_config.rb +2 -2
- data/lib/serega/validations/attribute/check_opt_many.rb +28 -11
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d780c3d344370b36d9e6db64dfdfa08878bfe32885469610ac2314638c878d87
|
4
|
+
data.tar.gz: d282c6466f7b7205a7b524999b85a3e928d79c00ee11445abd26582e0146842c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2a161487df001b0074d1864770a7dfecd77ef788e187de6e460cd3d2856162cba8777eb807a65a8d4d49471bb50380fc49ffd17f36c16eb52a2ab8b84ed7991
|
7
|
+
data.tar.gz: 4a672090d0893ef8d3945b2528fc9861d245833ec4b21074b73481ab7acbf8fa5207c786cad8496bf022070f3341339522bbf4970c7a554cfa543f002f51f504
|
data/README.md
CHANGED
@@ -888,6 +888,30 @@ Look at [select serialized fields](#selecting-fields) for `:hide` usage examples
|
|
888
888
|
end
|
889
889
|
```
|
890
890
|
|
891
|
+
### Plugin :explicit_many_option
|
892
|
+
|
893
|
+
Plugin requires to add :many option when adding relationships
|
894
|
+
(relationships are attributes with :serializer option specified)
|
895
|
+
|
896
|
+
Adding this plugin makes it clearer to find if relationship returns array or single
|
897
|
+
object
|
898
|
+
|
899
|
+
```ruby
|
900
|
+
class BaseSerializer < Serega
|
901
|
+
plugin :explicit_many_option
|
902
|
+
end
|
903
|
+
|
904
|
+
class UserSerializer < BaseSerializer
|
905
|
+
attribute :name
|
906
|
+
end
|
907
|
+
|
908
|
+
class PostSerializer < BaseSerializer
|
909
|
+
attribute :text
|
910
|
+
attribute :user, serializer: UserSerializer, many: false
|
911
|
+
attribute :comments, serializer: PostSerializer, many: true
|
912
|
+
end
|
913
|
+
```
|
914
|
+
|
891
915
|
## Errors
|
892
916
|
|
893
917
|
- `Serega::SeregaError` is a base error raised by this gem.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.15.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaPlugins
|
5
|
+
#
|
6
|
+
# Plugin :explicit_many_option
|
7
|
+
#
|
8
|
+
# Plugin requires to add :many option when adding relationships
|
9
|
+
# (relationships are attributes with :serializer option specified)
|
10
|
+
#
|
11
|
+
# Adding this plugin makes it clearer to find if relationship returns array or single object
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# class BaseSerializer < Serega
|
15
|
+
# plugin :explicit_many_option
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# class UserSerializer < BaseSerializer
|
19
|
+
# attribute :name
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# class PostSerializer < BaseSerializer
|
23
|
+
# attribute :text
|
24
|
+
# attribute :user, serializer: UserSerializer, many: false
|
25
|
+
# attribute :comments, serializer: PostSerializer, many: true
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
module ExplicitManyOption
|
29
|
+
# @return [Symbol] Plugin name
|
30
|
+
def self.plugin_name
|
31
|
+
:explicit_many_option
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Applies plugin code to specific serializer
|
36
|
+
#
|
37
|
+
# @param serializer_class [Class<Serega>] Current serializer class
|
38
|
+
# @param _opts [Hash] Loaded plugins options
|
39
|
+
#
|
40
|
+
# @return [void]
|
41
|
+
#
|
42
|
+
def self.load_plugin(serializer_class, **_opts)
|
43
|
+
require_relative "./validations/check_opt_many"
|
44
|
+
|
45
|
+
serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Serega::SeregaValidations::CheckAttributeParams additional/patched class methods
|
50
|
+
#
|
51
|
+
# @see Serega::SeregaValidations::CheckAttributeParams
|
52
|
+
#
|
53
|
+
module CheckAttributeParamsInstanceMethods
|
54
|
+
private
|
55
|
+
|
56
|
+
def check_opts
|
57
|
+
super
|
58
|
+
|
59
|
+
CheckOptMany.call(opts)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
register_plugin(ExplicitManyOption.plugin_name, ExplicitManyOption)
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaPlugins
|
5
|
+
module ExplicitManyOption
|
6
|
+
#
|
7
|
+
# Validator for attribute :many option
|
8
|
+
#
|
9
|
+
class CheckOptMany
|
10
|
+
class << self
|
11
|
+
#
|
12
|
+
# Checks attribute :many option must be provided with relations
|
13
|
+
#
|
14
|
+
# @param opts [Hash] Attribute options
|
15
|
+
#
|
16
|
+
# @raise [SeregaError] Attribute validation error
|
17
|
+
#
|
18
|
+
# @return [void]
|
19
|
+
#
|
20
|
+
def call(opts)
|
21
|
+
serializer = opts[:serializer]
|
22
|
+
return unless serializer
|
23
|
+
|
24
|
+
many_option_exists = opts.key?(:many)
|
25
|
+
return if many_option_exists
|
26
|
+
|
27
|
+
raise SeregaError,
|
28
|
+
"Attribute option :many [Boolean] must be provided" \
|
29
|
+
" for attributes with :serializer option"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -11,11 +11,11 @@ class Serega
|
|
11
11
|
attr_reader :opts
|
12
12
|
|
13
13
|
#
|
14
|
-
# Initializes
|
14
|
+
# Initializes PreloadsConfig object
|
15
15
|
#
|
16
16
|
# @param opts [Hash] options
|
17
17
|
#
|
18
|
-
# @return [Serega::SeregaPlugins::
|
18
|
+
# @return [Serega::SeregaPlugins::Preloads::PreloadsConfig]
|
19
19
|
#
|
20
20
|
def initialize(opts)
|
21
21
|
@opts = opts
|
@@ -7,17 +7,34 @@ class Serega
|
|
7
7
|
# Attribute `:many` option validator
|
8
8
|
#
|
9
9
|
class CheckOptMany
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
10
|
+
class << self
|
11
|
+
#
|
12
|
+
# Checks attribute :many option
|
13
|
+
#
|
14
|
+
# @param opts [Hash] Attribute options
|
15
|
+
#
|
16
|
+
# @raise [SeregaError] SeregaError that option has invalid value
|
17
|
+
#
|
18
|
+
# @return [void]
|
19
|
+
#
|
20
|
+
def call(opts)
|
21
|
+
return unless opts.key?(:many)
|
22
|
+
|
23
|
+
check_many_option_makes_sence(opts)
|
24
|
+
Utils::CheckOptIsBool.call(opts, :many)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def check_many_option_makes_sence(opts)
|
30
|
+
return if many_option_makes_sence?(opts)
|
31
|
+
|
32
|
+
raise SeregaError, "Option :many can be provided only together with :serializer or :batch option"
|
33
|
+
end
|
34
|
+
|
35
|
+
def many_option_makes_sence?(opts)
|
36
|
+
opts[:serializer] || opts[:batch]
|
37
|
+
end
|
21
38
|
end
|
22
39
|
end
|
23
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serega
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Glushkov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
JSON Serializer
|
@@ -59,6 +59,8 @@ files:
|
|
59
59
|
- lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb
|
60
60
|
- lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
|
61
61
|
- lib/serega/plugins/context_metadata/context_metadata.rb
|
62
|
+
- lib/serega/plugins/explicit_many_option/explicit_many_option.rb
|
63
|
+
- lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb
|
62
64
|
- lib/serega/plugins/formatters/formatters.rb
|
63
65
|
- lib/serega/plugins/if/if.rb
|
64
66
|
- lib/serega/plugins/if/validations/check_opt_if.rb
|
@@ -117,7 +119,7 @@ metadata:
|
|
117
119
|
source_code_uri: https://github.com/aglushkov/serega
|
118
120
|
documentation_uri: https://www.rubydoc.info/gems/serega
|
119
121
|
changelog_uri: https://github.com/aglushkov/serega/blob/master/CHANGELOG.md
|
120
|
-
post_install_message:
|
122
|
+
post_install_message:
|
121
123
|
rdoc_options: []
|
122
124
|
require_paths:
|
123
125
|
- lib
|
@@ -132,8 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
134
|
- !ruby/object:Gem::Version
|
133
135
|
version: '0'
|
134
136
|
requirements: []
|
135
|
-
rubygems_version: 3.4.
|
136
|
-
signing_key:
|
137
|
+
rubygems_version: 3.4.6
|
138
|
+
signing_key:
|
137
139
|
specification_version: 4
|
138
140
|
summary: JSON Serializer
|
139
141
|
test_files: []
|