serega 0.8.3 → 0.10.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.
@@ -1,106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- module SeregaPlugins
5
- #
6
- # Plugin adds `:hide_nil` option to attributes to delete them from final result
7
- # if value is nil
8
- #
9
- module HideNil
10
- # @return [Symbol] Plugin name
11
- def self.plugin_name
12
- :hide_nil
13
- end
14
-
15
- #
16
- # Applies plugin code to specific serializer
17
- #
18
- # @param serializer_class [Class<Serega>] Current serializer class
19
- # @param _opts [Hash] Loaded plugins options
20
- #
21
- # @return [void]
22
- #
23
- def self.load_plugin(serializer_class, **_opts)
24
- serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
25
- serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
26
- serializer_class::SeregaObjectSerializer.include(SeregaObjectSerializerInstanceMethods)
27
- end
28
-
29
- #
30
- # Adds config options and runs other callbacks after plugin was loaded
31
- #
32
- # @param serializer_class [Class<Serega>] Current serializer class
33
- # @param opts [Hash] loaded plugins opts
34
- #
35
- # @return [void]
36
- #
37
- def self.after_load_plugin(serializer_class, **opts)
38
- serializer_class.config.attribute_keys << :hide_nil
39
- end
40
-
41
- #
42
- # Serega::SeregaAttribute additional/patched instance methods
43
- #
44
- # @see Serega::SeregaValidations::CheckAttributeParams
45
- #
46
- module AttributeInstanceMethods
47
- # Check hide_nil is specified
48
- def hide_nil?
49
- !!opts[:hide_nil]
50
- end
51
- end
52
-
53
- #
54
- # Serega::SeregaValidations::CheckAttributeParams additional/patched class methods
55
- #
56
- # @see Serega::SeregaValidations::CheckAttributeParams
57
- #
58
- module CheckAttributeParamsInstanceMethods
59
- private
60
-
61
- def check_opts
62
- super
63
- CheckOptHideNil.call(opts)
64
- end
65
- end
66
-
67
- #
68
- # Validator class for :hide_nil attribute option
69
- #
70
- class CheckOptHideNil
71
- #
72
- # Checks attribute :hide_nil option
73
- #
74
- # @param opts [Hash] Attribute options
75
- #
76
- # @raise [Serega::SeregaError] SeregaError that option has invalid value
77
- #
78
- # @return [void]
79
- #
80
- def self.call(opts)
81
- return unless opts.key?(:hide_nil)
82
-
83
- value = opts[:hide_nil]
84
- return if (value == true) || (value == false)
85
-
86
- raise SeregaError, "Invalid option :hide_nil => #{value.inspect}. Must have a boolean value"
87
- end
88
- end
89
-
90
- #
91
- # SeregaObjectSerializer additional/patched class methods
92
- #
93
- # @see Serega::SeregaObjectSerializer
94
- #
95
- module SeregaObjectSerializerInstanceMethods
96
- private
97
-
98
- def attach_final_value(final_value, *)
99
- super unless final_value.nil?
100
- end
101
- end
102
- end
103
-
104
- register_plugin(HideNil.plugin_name, HideNil)
105
- end
106
- end