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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/serega/attribute.rb +17 -4
  4. data/lib/serega/config.rb +5 -5
  5. data/lib/serega/convert.rb +7 -7
  6. data/lib/serega/convert_item.rb +4 -4
  7. data/lib/serega/helpers/serializer_class_helper.rb +1 -1
  8. data/lib/serega/map.rb +2 -2
  9. data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +3 -3
  10. data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +2 -2
  11. data/lib/serega/plugins/context_metadata/context_metadata.rb +9 -13
  12. data/lib/serega/plugins/formatters/formatters.rb +11 -3
  13. data/lib/serega/plugins/hide_nil/hide_nil.rb +6 -6
  14. data/lib/serega/plugins/metadata/meta_attribute.rb +4 -4
  15. data/lib/serega/plugins/metadata/metadata.rb +3 -3
  16. data/lib/serega/plugins/metadata/validations/check_block.rb +4 -4
  17. data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +3 -3
  18. data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +3 -3
  19. data/lib/serega/plugins/metadata/validations/check_opts.rb +3 -3
  20. data/lib/serega/plugins/metadata/validations/check_path.rb +3 -3
  21. data/lib/serega/plugins/preloads/lib/enum_deep_freeze.rb +1 -1
  22. data/lib/serega/plugins/preloads/lib/format_user_preloads.rb +1 -1
  23. data/lib/serega/plugins/preloads/lib/main_preload_path.rb +1 -1
  24. data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +2 -2
  25. data/lib/serega/plugins/preloads/preloads.rb +26 -4
  26. data/lib/serega/plugins/preloads/validations/check_opt_preload.rb +17 -0
  27. data/lib/serega/plugins/preloads/validations/check_opt_preload_path.rb +4 -4
  28. data/lib/serega/plugins/presenter/presenter.rb +6 -6
  29. data/lib/serega/plugins/root/root.rb +3 -3
  30. data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +1 -1
  31. data/lib/serega/plugins/string_modifiers/string_modifiers.rb +1 -1
  32. data/lib/serega/plugins/validate_modifiers/validate.rb +6 -3
  33. data/lib/serega/plugins/validate_modifiers/validate_modifiers.rb +1 -1
  34. data/lib/serega/plugins.rb +7 -7
  35. data/lib/serega/utils/as_json.rb +1 -1
  36. data/lib/serega/utils/enum_deep_dup.rb +1 -1
  37. data/lib/serega/utils/to_hash.rb +2 -2
  38. data/lib/serega/utils/to_json.rb +1 -1
  39. data/lib/serega/validations/attribute/check_block.rb +3 -3
  40. data/lib/serega/validations/attribute/check_name.rb +3 -3
  41. data/lib/serega/validations/attribute/check_opt_const.rb +5 -5
  42. data/lib/serega/validations/attribute/check_opt_delegate.rb +57 -0
  43. data/lib/serega/validations/attribute/check_opt_hide.rb +3 -3
  44. data/lib/serega/validations/attribute/check_opt_key.rb +6 -6
  45. data/lib/serega/validations/attribute/check_opt_many.rb +3 -3
  46. data/lib/serega/validations/attribute/check_opt_serializer.rb +3 -3
  47. data/lib/serega/validations/attribute/check_opt_value.rb +7 -7
  48. data/lib/serega/validations/check_attribute_params.rb +4 -3
  49. data/lib/serega/validations/check_initiate_params.rb +3 -3
  50. data/lib/serega/validations/check_serialize_params.rb +5 -5
  51. data/lib/serega/validations/utils/check_allowed_keys.rb +3 -3
  52. data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -3
  53. data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -3
  54. data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -3
  55. data/lib/serega.rb +30 -43
  56. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0677ac2e319673d37e712b840c9485abf9ec2cd578a66d0a4d667e6134b4b3e
4
- data.tar.gz: 7711764ae779bd31ed3c203fff925279ca19c9c74acb94cc58e4a4a302da35a9
3
+ metadata.gz: 88e403fa3d489c2c708aca3f9df3734bcfc64f8e61eed4b96b9635cdf1a16622
4
+ data.tar.gz: 1c7f92f7defa3c2415257e69021ed70f0c07b43b0f468969589a1ed18148507b
5
5
  SHA512:
6
- metadata.gz: e5f26401f3388020f8051543f19a03f223b424d55d9ce91a6c11c0ad7646e398d68d746227746297f5fb9dc387c7adaa84f6fdaa2d0829b8f2d3e13b516c28ec
7
- data.tar.gz: 0fbb40978adcaa91756bd59f6ed70b5a905eb25f0b6e60d2bb22f85bd4e9f7c4a51f37c987d035cc761bb0cab52f400472718ea992b576c89a39d582267c6b9a
6
+ metadata.gz: 50aec430bd1d42cccc89c82635700734569ffde5de0780782af0454feae87fbb11fc273b91bb1c936fb3001c26d24db72726928fc8f7d93ccf4b4b846a43147f
7
+ data.tar.gz: 883c4e0e945663939b75c62e237b0268b9d8fd60ff2f31f7066a72021a9f90c045ca404c725ae787bc7268cf5f5cbd208f626c8f97ec22ebbc4208f0208e8233
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
@@ -4,7 +4,7 @@ class Serega
4
4
  #
5
5
  # Stores Attribute data
6
6
  #
7
- class Attribute
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 = Utils::EnumDeepDup.call(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::Helpers::SerializerClassHelper
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 Config
10
- module ConfigInstanceMethods
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 = Utils::EnumDeepDup.call(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 ConfigInstanceMethods
46
- extend Serega::Helpers::SerializerClassHelper
45
+ include SeregaConfigInstanceMethods
46
+ extend Serega::SeregaHelpers::SerializerClassHelper
47
47
  end
48
48
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- class Convert
5
- module ConvertClassMethods
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 ConvertInstanceMethods
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::ConvertItem.call(object, opts[:context], opts[:map])
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::Helpers::SerializerClassHelper
42
- extend ConvertClassMethods
43
- include ConvertInstanceMethods
41
+ extend Serega::SeregaHelpers::SerializerClassHelper
42
+ extend SeregaConvertClassMethods
43
+ include SeregaConvertInstanceMethods
44
44
  end
45
45
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- class ConvertItem
5
- module ConvertItemClassMethods
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::Helpers::SerializerClassHelper
35
- extend ConvertItemClassMethods
34
+ extend Serega::SeregaHelpers::SerializerClassHelper
35
+ extend SeregaConvertItemClassMethods
36
36
  end
37
37
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Helpers
4
+ module SeregaHelpers
5
5
  # Stores link to current serializer class
6
6
  module SerializerClassHelper
7
7
  # @return [Class<Serega>] Serializer class that current class is namespaced under.
data/lib/serega/map.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- class Map
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::Helpers::SerializerClassHelper
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 Plugins
4
+ module SeregaPlugins
5
5
  #
6
6
  # Plugin that checks used plugins and loads correct Preloader for selected response type
7
- # @see Serega::Plugins::JsonApiActiverecordPreloader
8
- # @see Serega::Plugins::SimpleApiActiverecordPreloader
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 Plugins
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 Error, "Can't preload #{preloads.inspect} to #{object.inspect}" unless preload_handler
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 Plugins
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(InstanceMethods)
18
- serializer_class::Convert.include(ConvertInstanceMethods)
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 InstanceMethods
29
- def to_h(object, **opts)
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 ConvertInstanceMethods
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 Plugins
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::Attribute.include(AttributeInstanceMethods)
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
- @value_block = formatted_block(formatter, original_block)
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 Plugins
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::Attribute.include(AttributeMethods)
24
+ serializer_class::SeregaAttribute.include(AttributeMethods)
25
25
  serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
26
- serializer_class::ConvertItem.extend(ConvertItemClassMethods)
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::Error] Error that option has invalid value
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 Error, "Invalid option :hide_nil => #{value.inspect}. Must have a boolean value"
65
+ raise SeregaError, "Invalid option :hide_nil => #{value.inspect}. Must have a boolean value"
66
66
  end
67
67
  end
68
68
 
69
- module ConvertItemClassMethods
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 Plugins
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 = Utils::EnumDeepDup.call(path)
40
- @opts = Utils::EnumDeepDup.call(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::Helpers::SerializerClassHelper
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 Plugins
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::Convert.include(ConvertInstanceMethods)
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 ConvertInstanceMethods
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 Plugins
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 [Error] Error that block has invalid arguments
28
+ # @raise [SeregaError] SeregaError that block has invalid arguments
29
29
  #
30
30
  # @return [void]
31
31
  #
32
32
  def call(block)
33
- raise Error, "Block must be provided when defining meta attribute" unless block
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 Error, "Block can have maximum 2 regular parameters (no **keyword or *array args)"
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 Plugins
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 [Error] Error that option has invalid value
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 Error, "Invalid option :hide_empty => #{value.inspect}. Must be true"
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 Plugins
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 [Error] Error that option has invalid value
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 Error, "Invalid option :hide_nil => #{value.inspect}. Must be true"
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 Plugins
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 [Error] when attribute has invalid options
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 Error, "Invalid option #{key.inspect}. Allowed options are: #{attribute_keys.map(&:inspect).join(", ")}"
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 Plugins
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 [Error] when metadata attribute name has invalid format
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 Error, message(name)
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 Plugins
4
+ module SeregaPlugins
5
5
  module Preloads
6
6
  # Freezes nested enumerable data
7
7
  class EnumDeepFreeze
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Plugins
4
+ module SeregaPlugins
5
5
  module Preloads
6
6
  # Transforms user provided preloads to hash
7
7
  class FormatUserPreloads
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Plugins
4
+ module SeregaPlugins
5
5
  module Preloads
6
6
  class MainPreloadPath
7
7
  module ClassMethods
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Plugins
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 = Utils::EnumDeepDup.call(current_preloads) if has_nested
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 Plugins
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::Attribute.include(AttributeMethods)
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] = {auto_preload_relations: opts.fetch(:auto_preload_relations, true)}
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][:auto_preload_relations]
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