serega 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/serega/attribute.rb +17 -4
- data/lib/serega/config.rb +5 -5
- data/lib/serega/convert.rb +7 -7
- data/lib/serega/convert_item.rb +4 -4
- data/lib/serega/helpers/serializer_class_helper.rb +1 -1
- data/lib/serega/map.rb +2 -2
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +3 -3
- data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +2 -2
- data/lib/serega/plugins/context_metadata/context_metadata.rb +9 -13
- data/lib/serega/plugins/formatters/formatters.rb +11 -3
- data/lib/serega/plugins/hide_nil/hide_nil.rb +6 -6
- data/lib/serega/plugins/metadata/meta_attribute.rb +4 -4
- data/lib/serega/plugins/metadata/metadata.rb +3 -3
- data/lib/serega/plugins/metadata/validations/check_block.rb +4 -4
- data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +3 -3
- data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +3 -3
- data/lib/serega/plugins/metadata/validations/check_opts.rb +3 -3
- data/lib/serega/plugins/metadata/validations/check_path.rb +3 -3
- data/lib/serega/plugins/preloads/lib/enum_deep_freeze.rb +1 -1
- data/lib/serega/plugins/preloads/lib/format_user_preloads.rb +1 -1
- data/lib/serega/plugins/preloads/lib/main_preload_path.rb +1 -1
- data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +2 -2
- data/lib/serega/plugins/preloads/preloads.rb +26 -4
- data/lib/serega/plugins/preloads/validations/check_opt_preload.rb +17 -0
- data/lib/serega/plugins/preloads/validations/check_opt_preload_path.rb +4 -4
- data/lib/serega/plugins/presenter/presenter.rb +6 -6
- data/lib/serega/plugins/root/root.rb +3 -3
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +1 -1
- data/lib/serega/plugins/string_modifiers/string_modifiers.rb +1 -1
- data/lib/serega/plugins/validate_modifiers/validate.rb +6 -3
- data/lib/serega/plugins/validate_modifiers/validate_modifiers.rb +1 -1
- data/lib/serega/plugins.rb +7 -7
- data/lib/serega/utils/as_json.rb +1 -1
- data/lib/serega/utils/enum_deep_dup.rb +1 -1
- data/lib/serega/utils/to_hash.rb +2 -2
- data/lib/serega/utils/to_json.rb +1 -1
- data/lib/serega/validations/attribute/check_block.rb +3 -3
- data/lib/serega/validations/attribute/check_name.rb +3 -3
- data/lib/serega/validations/attribute/check_opt_const.rb +5 -5
- data/lib/serega/validations/attribute/check_opt_delegate.rb +57 -0
- data/lib/serega/validations/attribute/check_opt_hide.rb +3 -3
- data/lib/serega/validations/attribute/check_opt_key.rb +6 -6
- data/lib/serega/validations/attribute/check_opt_many.rb +3 -3
- data/lib/serega/validations/attribute/check_opt_serializer.rb +3 -3
- data/lib/serega/validations/attribute/check_opt_value.rb +7 -7
- data/lib/serega/validations/check_attribute_params.rb +4 -3
- data/lib/serega/validations/check_initiate_params.rb +3 -3
- data/lib/serega/validations/check_serialize_params.rb +5 -5
- data/lib/serega/validations/utils/check_allowed_keys.rb +3 -3
- data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -3
- data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -3
- data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -3
- data/lib/serega.rb +30 -43
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88e403fa3d489c2c708aca3f9df3734bcfc64f8e61eed4b96b9635cdf1a16622
|
4
|
+
data.tar.gz: 1c7f92f7defa3c2415257e69021ed70f0c07b43b0f468969589a1ed18148507b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50aec430bd1d42cccc89c82635700734569ffde5de0780782af0454feae87fbb11fc273b91bb1c936fb3001c26d24db72726928fc8f7d93ccf4b4b846a43147f
|
7
|
+
data.tar.gz: 883c4e0e945663939b75c62e237b0268b9d8fd60ff2f31f7066a72021a9f90c045ca404c725ae787bc7268cf5f5cbd208f626c8f97ec22ebbc4208f0208e8233
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/serega/attribute.rb
CHANGED
@@ -4,7 +4,7 @@ class Serega
|
|
4
4
|
#
|
5
5
|
# Stores Attribute data
|
6
6
|
#
|
7
|
-
class
|
7
|
+
class SeregaAttribute
|
8
8
|
#
|
9
9
|
# Stores Attribute instance methods
|
10
10
|
#
|
@@ -36,7 +36,7 @@ class Serega
|
|
36
36
|
self.class.serializer_class::CheckAttributeParams.new(name, opts, block).validate
|
37
37
|
|
38
38
|
@name = name.to_sym
|
39
|
-
@opts =
|
39
|
+
@opts = SeregaUtils::EnumDeepDup.call(opts)
|
40
40
|
@block = block
|
41
41
|
end
|
42
42
|
|
@@ -77,7 +77,7 @@ class Serega
|
|
77
77
|
def value_block
|
78
78
|
return @value_block if instance_variable_defined?(:@value_block)
|
79
79
|
|
80
|
-
@value_block = block || opts[:value] || const_block || keyword_block
|
80
|
+
@value_block = block || opts[:value] || const_block || delegate_block || keyword_block
|
81
81
|
end
|
82
82
|
|
83
83
|
#
|
@@ -123,9 +123,22 @@ class Serega
|
|
123
123
|
key_method_name = key
|
124
124
|
proc { |object| object.public_send(key_method_name) }
|
125
125
|
end
|
126
|
+
|
127
|
+
def delegate_block
|
128
|
+
return unless opts.key?(:delegate)
|
129
|
+
|
130
|
+
key_method_name = key
|
131
|
+
delegate_to = opts[:delegate][:to]
|
132
|
+
|
133
|
+
if opts[:delegate][:allow_nil]
|
134
|
+
proc { |object| object.public_send(delegate_to)&.public_send(key_method_name) }
|
135
|
+
else
|
136
|
+
proc { |object| object.public_send(delegate_to).public_send(key_method_name) }
|
137
|
+
end
|
138
|
+
end
|
126
139
|
end
|
127
140
|
|
128
|
-
extend Serega::
|
141
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
129
142
|
include AttributeInstanceMethods
|
130
143
|
end
|
131
144
|
end
|
data/lib/serega/config.rb
CHANGED
@@ -6,8 +6,8 @@ class Serega
|
|
6
6
|
#
|
7
7
|
# Core class that stores serializer configuration
|
8
8
|
#
|
9
|
-
class
|
10
|
-
module
|
9
|
+
class SeregaConfig
|
10
|
+
module SeregaConfigInstanceMethods
|
11
11
|
extend Forwardable
|
12
12
|
|
13
13
|
# @return [Hash] Current config data
|
@@ -20,7 +20,7 @@ class Serega
|
|
20
20
|
# @param opts [Hash] Initial config options
|
21
21
|
#
|
22
22
|
def initialize(opts = {})
|
23
|
-
@opts =
|
23
|
+
@opts = SeregaUtils::EnumDeepDup.call(opts)
|
24
24
|
end
|
25
25
|
|
26
26
|
#
|
@@ -42,7 +42,7 @@ class Serega
|
|
42
42
|
def_delegators :opts, :[], :[]=, :fetch, :keys, :has_key?
|
43
43
|
end
|
44
44
|
|
45
|
-
include
|
46
|
-
extend Serega::
|
45
|
+
include SeregaConfigInstanceMethods
|
46
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
47
47
|
end
|
48
48
|
end
|
data/lib/serega/convert.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
class
|
5
|
-
module
|
4
|
+
class SeregaConvert
|
5
|
+
module SeregaConvertClassMethods
|
6
6
|
def call(object, **opts)
|
7
7
|
new(object, **opts).to_h
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
module
|
11
|
+
module SeregaConvertInstanceMethods
|
12
12
|
attr_reader :object, :opts
|
13
13
|
|
14
14
|
def initialize(object, **opts)
|
@@ -27,7 +27,7 @@ class Serega
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def one(object)
|
30
|
-
self.class.serializer_class::
|
30
|
+
self.class.serializer_class::SeregaConvertItem.call(object, opts[:context], opts[:map])
|
31
31
|
end
|
32
32
|
|
33
33
|
def many?
|
@@ -38,8 +38,8 @@ class Serega
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
extend Serega::
|
42
|
-
extend
|
43
|
-
include
|
41
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
42
|
+
extend SeregaConvertClassMethods
|
43
|
+
include SeregaConvertInstanceMethods
|
44
44
|
end
|
45
45
|
end
|
data/lib/serega/convert_item.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
class
|
5
|
-
module
|
4
|
+
class SeregaConvertItem
|
5
|
+
module SeregaConvertItemClassMethods
|
6
6
|
def call(object, context, map)
|
7
7
|
return unless object
|
8
8
|
|
@@ -31,7 +31,7 @@ class Serega
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
extend Serega::
|
35
|
-
extend
|
34
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
35
|
+
extend SeregaConvertItemClassMethods
|
36
36
|
end
|
37
37
|
end
|
data/lib/serega/map.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
class
|
4
|
+
class SeregaMap
|
5
5
|
module ClassMethods
|
6
6
|
def call(opts)
|
7
7
|
@cache ||= {}
|
@@ -44,6 +44,6 @@ class Serega
|
|
44
44
|
end
|
45
45
|
|
46
46
|
extend ClassMethods
|
47
|
-
extend Serega::
|
47
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
48
48
|
end
|
49
49
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
#
|
6
6
|
# Plugin that checks used plugins and loads correct Preloader for selected response type
|
7
|
-
# @see Serega::
|
8
|
-
# @see Serega::
|
7
|
+
# @see Serega::SeregaPlugins::JsonApiActiverecordPreloader
|
8
|
+
# @see Serega::SeregaPlugins::SimpleApiActiverecordPreloader
|
9
9
|
#
|
10
10
|
module ActiverecordPreloads
|
11
11
|
# @return [Symbol] plugin name
|
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module ActiverecordPreloads
|
6
6
|
class Preloader
|
7
7
|
module ClassMethods
|
8
8
|
def preload(object, preloads)
|
9
9
|
preload_handler = handlers.find { |handler| handler.fit?(object) }
|
10
|
-
raise
|
10
|
+
raise SeregaError, "Can't preload #{preloads.inspect} to #{object.inspect}" unless preload_handler
|
11
11
|
|
12
12
|
preload_handler.preload(object, preloads)
|
13
13
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module ContextMetadata
|
6
6
|
DEFAULT_CONTEXT_METADATA_KEY = :meta
|
7
7
|
|
@@ -14,8 +14,8 @@ class Serega
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.load_plugin(serializer_class, **_opts)
|
17
|
-
serializer_class.include(
|
18
|
-
serializer_class::
|
17
|
+
serializer_class::SeregaConvert.include(SeregaConvertInstanceMethods)
|
18
|
+
serializer_class::CheckSerializeParams.extend(CheckSerializeParamsClassMethods)
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.after_load_plugin(serializer_class, **opts)
|
@@ -25,20 +25,16 @@ class Serega
|
|
25
25
|
config[:serialize_keys] << meta_key
|
26
26
|
end
|
27
27
|
|
28
|
-
module
|
29
|
-
def
|
30
|
-
meta_key = self.class.config[:context_metadata][:key]
|
31
|
-
meta = opts[meta_key]
|
32
|
-
|
33
|
-
if meta && !meta.is_a?(Hash)
|
34
|
-
raise Serega::Error, "Option :#{meta_key} must be a Hash, but #{meta.class} was given"
|
35
|
-
end
|
36
|
-
|
28
|
+
module CheckSerializeParamsClassMethods
|
29
|
+
def check_opts(opts)
|
37
30
|
super
|
31
|
+
|
32
|
+
meta_key = serializer_class.config[:context_metadata][:key]
|
33
|
+
SeregaValidations::SeregaUtils::CheckOptIsHash.call(opts, meta_key)
|
38
34
|
end
|
39
35
|
end
|
40
36
|
|
41
|
-
module
|
37
|
+
module SeregaConvertInstanceMethods
|
42
38
|
def to_h
|
43
39
|
super.tap do |hash|
|
44
40
|
add_context_metadata(hash)
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Formatters
|
6
6
|
def self.plugin_name
|
7
7
|
:formatters
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.load_plugin(serializer_class, **_opts)
|
11
|
-
serializer_class::
|
11
|
+
serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.after_load_plugin(serializer_class, **_opts)
|
@@ -25,7 +25,15 @@ class Serega
|
|
25
25
|
formatter = opts[:format]
|
26
26
|
return original_block unless formatter
|
27
27
|
|
28
|
-
|
28
|
+
new_value_block = formatted_block(formatter, original_block)
|
29
|
+
|
30
|
+
# Detect formatted :const value in advance
|
31
|
+
if opts.key?(:const)
|
32
|
+
const_value = new_value_block.call
|
33
|
+
new_value_block = proc { const_value }
|
34
|
+
end
|
35
|
+
|
36
|
+
@value_block = new_value_block
|
29
37
|
end
|
30
38
|
|
31
39
|
private
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
#
|
6
6
|
# Plugin adds `:hide_nil` option to attributes to delete them from final result
|
7
7
|
# if value is nil
|
@@ -21,9 +21,9 @@ class Serega
|
|
21
21
|
# @return [void]
|
22
22
|
#
|
23
23
|
def self.load_plugin(serializer_class, **_opts)
|
24
|
-
serializer_class::
|
24
|
+
serializer_class::SeregaAttribute.include(AttributeMethods)
|
25
25
|
serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
|
26
|
-
serializer_class::
|
26
|
+
serializer_class::SeregaConvertItem.extend(SeregaConvertItemClassMethods)
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.after_load_plugin(serializer_class, **opts)
|
@@ -52,7 +52,7 @@ class Serega
|
|
52
52
|
#
|
53
53
|
# @param opts [Hash] Attribute options
|
54
54
|
#
|
55
|
-
# @raise [Serega::
|
55
|
+
# @raise [Serega::SeregaError] SeregaError that option has invalid value
|
56
56
|
#
|
57
57
|
# @return [void]
|
58
58
|
#
|
@@ -62,11 +62,11 @@ class Serega
|
|
62
62
|
value = opts[:hide_nil]
|
63
63
|
return if (value == true) || (value == false)
|
64
64
|
|
65
|
-
raise
|
65
|
+
raise SeregaError, "Invalid option :hide_nil => #{value.inspect}. Must have a boolean value"
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
module
|
69
|
+
module SeregaConvertItemClassMethods
|
70
70
|
private
|
71
71
|
|
72
72
|
def attach_value(value, *args)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
#
|
7
7
|
# Stores Attribute data
|
@@ -36,8 +36,8 @@ class Serega
|
|
36
36
|
check(path, opts, block)
|
37
37
|
|
38
38
|
@name = path.join(".").to_sym
|
39
|
-
@path =
|
40
|
-
@opts =
|
39
|
+
@path = SeregaUtils::EnumDeepDup.call(path)
|
40
|
+
@opts = SeregaUtils::EnumDeepDup.call(opts)
|
41
41
|
@block = block
|
42
42
|
end
|
43
43
|
|
@@ -66,7 +66,7 @@ class Serega
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
extend Serega::
|
69
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
70
70
|
include InstanceMethods
|
71
71
|
end
|
72
72
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
def self.plugin_name
|
7
7
|
:metadata
|
@@ -13,7 +13,7 @@ class Serega
|
|
13
13
|
|
14
14
|
def self.load_plugin(serializer_class, **_opts)
|
15
15
|
serializer_class.extend(ClassMethods)
|
16
|
-
serializer_class::
|
16
|
+
serializer_class::SeregaConvert.include(SeregaConvertInstanceMethods)
|
17
17
|
|
18
18
|
require_relative "./meta_attribute"
|
19
19
|
require_relative "./validations/check_block"
|
@@ -79,7 +79,7 @@ class Serega
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
module
|
82
|
+
module SeregaConvertInstanceMethods
|
83
83
|
def to_h
|
84
84
|
hash = super
|
85
85
|
add_metadata(hash)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
class MetaAttribute
|
7
7
|
class CheckBlock
|
@@ -25,17 +25,17 @@ class Serega
|
|
25
25
|
#
|
26
26
|
# @param block [Proc] Block that returns serialized meta attribute value
|
27
27
|
#
|
28
|
-
# @raise [
|
28
|
+
# @raise [SeregaError] SeregaError that block has invalid arguments
|
29
29
|
#
|
30
30
|
# @return [void]
|
31
31
|
#
|
32
32
|
def call(block)
|
33
|
-
raise
|
33
|
+
raise SeregaError, "Block must be provided when defining meta attribute" unless block
|
34
34
|
|
35
35
|
params = block.parameters
|
36
36
|
return if (params.count <= 2) && params.all? { |par| ALLOWED_PARAM_TYPES.include?(par[0]) }
|
37
37
|
|
38
|
-
raise
|
38
|
+
raise SeregaError, "Block can have maximum 2 regular parameters (no **keyword or *array args)"
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
class MetaAttribute
|
7
7
|
class CheckOptHideEmpty
|
@@ -11,7 +11,7 @@ class Serega
|
|
11
11
|
#
|
12
12
|
# @param opts [Hash] Attribute options
|
13
13
|
#
|
14
|
-
# @raise [
|
14
|
+
# @raise [SeregaError] SeregaError that option has invalid value
|
15
15
|
#
|
16
16
|
# @return [void]
|
17
17
|
#
|
@@ -21,7 +21,7 @@ class Serega
|
|
21
21
|
value = opts[:hide_empty]
|
22
22
|
return if value == true
|
23
23
|
|
24
|
-
raise
|
24
|
+
raise SeregaError, "Invalid option :hide_empty => #{value.inspect}. Must be true"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
class MetaAttribute
|
7
7
|
class CheckOptHideNil
|
@@ -11,7 +11,7 @@ class Serega
|
|
11
11
|
#
|
12
12
|
# @param opts [Hash] Attribute options
|
13
13
|
#
|
14
|
-
# @raise [
|
14
|
+
# @raise [SeregaError] SeregaError that option has invalid value
|
15
15
|
#
|
16
16
|
# @return [void]
|
17
17
|
#
|
@@ -21,7 +21,7 @@ class Serega
|
|
21
21
|
value = opts[:hide_nil]
|
22
22
|
return if value == true
|
23
23
|
|
24
|
-
raise
|
24
|
+
raise SeregaError, "Invalid option :hide_nil => #{value.inspect}. Must be true"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
class MetaAttribute
|
7
7
|
class CheckOpts
|
@@ -13,7 +13,7 @@ class Serega
|
|
13
13
|
# @param opts [Hash] Attribute options
|
14
14
|
# @param attribute_keys [Array<Symbol>] Allowed options keys
|
15
15
|
#
|
16
|
-
# @raise [
|
16
|
+
# @raise [SeregaError] when attribute has invalid options
|
17
17
|
#
|
18
18
|
# @return [void]
|
19
19
|
#
|
@@ -21,7 +21,7 @@ class Serega
|
|
21
21
|
opts.each_key do |key|
|
22
22
|
next if attribute_keys.include?(key.to_sym)
|
23
23
|
|
24
|
-
raise
|
24
|
+
raise SeregaError, "Invalid option #{key.inspect}. Allowed options are: #{attribute_keys.map(&:inspect).join(", ")}"
|
25
25
|
end
|
26
26
|
|
27
27
|
check_each_opt(opts)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Metadata
|
6
6
|
class MetaAttribute
|
7
7
|
class CheckPath
|
@@ -18,7 +18,7 @@ class Serega
|
|
18
18
|
#
|
19
19
|
# @param path [Array<String, Symbol>] Metadata attribute path names
|
20
20
|
#
|
21
|
-
# @raise [
|
21
|
+
# @raise [SeregaError] when metadata attribute name has invalid format
|
22
22
|
# @return [void]
|
23
23
|
#
|
24
24
|
def call(path)
|
@@ -39,7 +39,7 @@ class Serega
|
|
39
39
|
|
40
40
|
return if valid
|
41
41
|
|
42
|
-
raise
|
42
|
+
raise SeregaError, message(name)
|
43
43
|
end
|
44
44
|
|
45
45
|
def message(name)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
module Preloads
|
6
6
|
#
|
7
7
|
# Finds relations to preload for provided serializer
|
@@ -29,7 +29,7 @@ class Serega
|
|
29
29
|
next unless current_preloads
|
30
30
|
|
31
31
|
has_nested = nested_map.any?
|
32
|
-
current_preloads =
|
32
|
+
current_preloads = SeregaUtils::EnumDeepDup.call(current_preloads) if has_nested
|
33
33
|
append_current(preloads, current_preloads)
|
34
34
|
next unless has_nested
|
35
35
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
4
|
+
module SeregaPlugins
|
5
5
|
#
|
6
6
|
# Plugin adds `.preloads` method to find relations that must be preloaded
|
7
7
|
#
|
@@ -21,7 +21,7 @@ class Serega
|
|
21
21
|
#
|
22
22
|
def self.load_plugin(serializer_class, **_opts)
|
23
23
|
serializer_class.include(InstanceMethods)
|
24
|
-
serializer_class::
|
24
|
+
serializer_class::SeregaAttribute.include(AttributeMethods)
|
25
25
|
|
26
26
|
serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
|
27
27
|
|
@@ -29,13 +29,18 @@ class Serega
|
|
29
29
|
require_relative "./lib/format_user_preloads"
|
30
30
|
require_relative "./lib/main_preload_path"
|
31
31
|
require_relative "./lib/preloads_constructor"
|
32
|
+
require_relative "./validations/check_opt_preload"
|
32
33
|
require_relative "./validations/check_opt_preload_path"
|
33
34
|
end
|
34
35
|
|
35
36
|
def self.after_load_plugin(serializer_class, **opts)
|
36
37
|
config = serializer_class.config
|
37
38
|
config[:attribute_keys] += [:preload, :preload_path]
|
38
|
-
config[:preloads] = {
|
39
|
+
config[:preloads] = {
|
40
|
+
auto_preload_attributes_with_delegate: opts.fetch(:auto_preload_attributes_with_delegate, false),
|
41
|
+
auto_preload_attributes_with_serializer: opts.fetch(:auto_preload_attributes_with_serializer, false),
|
42
|
+
auto_hide_attributes_with_preload: opts.fetch(:auto_hide_attributes_with_preload, false)
|
43
|
+
}
|
39
44
|
end
|
40
45
|
|
41
46
|
# Adds #preloads instance method
|
@@ -60,15 +65,31 @@ class Serega
|
|
60
65
|
@preloads_path = get_preloads_path
|
61
66
|
end
|
62
67
|
|
68
|
+
def hide
|
69
|
+
res = super
|
70
|
+
return res unless res.nil?
|
71
|
+
|
72
|
+
auto_hide_attribute_with_preloads? || nil
|
73
|
+
end
|
74
|
+
|
63
75
|
private
|
64
76
|
|
77
|
+
def auto_hide_attribute_with_preloads?
|
78
|
+
return @auto_hide_attribute_with_preloads if defined?(@auto_hide_attribute_with_preloads)
|
79
|
+
|
80
|
+
auto = self.class.serializer_class.config[:preloads][:auto_hide_attributes_with_preload]
|
81
|
+
@auto_hide_attribute_with_preloads = auto && !preloads.nil? && (preloads != false) && (preloads != {})
|
82
|
+
end
|
83
|
+
|
65
84
|
def get_preloads
|
66
85
|
preloads_provided = opts.key?(:preload)
|
67
86
|
preloads =
|
68
87
|
if preloads_provided
|
69
88
|
opts[:preload]
|
70
|
-
elsif relation? && self.class.serializer_class.config[:preloads][:
|
89
|
+
elsif relation? && self.class.serializer_class.config[:preloads][:auto_preload_attributes_with_serializer]
|
71
90
|
key
|
91
|
+
elsif opts.key?(:delegate) && self.class.serializer_class.config[:preloads][:auto_preload_attributes_with_delegate]
|
92
|
+
opts[:delegate].fetch(:to)
|
72
93
|
end
|
73
94
|
|
74
95
|
# Nil and empty hash differs as we can preload nested results to
|
@@ -90,6 +111,7 @@ class Serega
|
|
90
111
|
|
91
112
|
def check_opts
|
92
113
|
super
|
114
|
+
CheckOptPreload.call(opts)
|
93
115
|
CheckOptPreloadPath.call(opts)
|
94
116
|
end
|
95
117
|
end
|