serega 0.18.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,11 +60,31 @@ class Serega
60
60
  :root
61
61
  end
62
62
 
63
+ # Checks requirements to load plugin
64
+ #
65
+ # @param serializer_class [Class<Serega>] Current serializer class
66
+ # @param opts [Hash] plugin options
67
+ #
68
+ # @return [void]
69
+ #
70
+ def self.before_load_plugin(serializer_class, **opts)
71
+ allowed_keys = %i[root root_one root_many]
72
+ opts.each_key do |key|
73
+ next if allowed_keys.include?(key)
74
+
75
+ raise SeregaError,
76
+ "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. Allowed options:\n" \
77
+ " - :root [String, Symbol, nil] Specifies common root keyword used when serializing one or multiple objects\n" \
78
+ " - :root_one [String, Symbol, nil] Specifies root keyword used when serializing one object\n" \
79
+ " - :root_many [String, Symbol, nil] Specifies root keyword used when serializing multiple objects" \
80
+ end
81
+ end
82
+
63
83
  #
64
84
  # Applies plugin code to specific serializer
65
85
  #
66
86
  # @param serializer_class [Class<Serega>] Current serializer class
67
- # @param _opts [Hash] Loaded plugins options
87
+ # @param _opts [Hash] Plugin options
68
88
  #
69
89
  # @return [void]
70
90
  #
@@ -78,7 +98,7 @@ class Serega
78
98
  # Adds config options and runs other callbacks after plugin was loaded
79
99
  #
80
100
  # @param serializer_class [Class<Serega>] Current serializer class
81
- # @param opts [Hash] loaded plugins opts
101
+ # @param opts [Hash] Plugin options
82
102
  #
83
103
  # @return [void]
84
104
  #
@@ -102,9 +122,9 @@ class Serega
102
122
  #
103
123
  # Configures response root key
104
124
  #
105
- # @param root [String, Symbol, nil] Specifies common root when serializing one or multiple objects
106
- # @param one [String, Symbol, nil] Specifies root when serializing one object
107
- # @param many [String, Symbol, nil] Specifies root when serializing multiple objects
125
+ # @param root [String, Symbol, nil] Specifies common root keyword used when serializing one or multiple objects
126
+ # @param one [String, Symbol, nil] Specifies root keyword used when serializing one object
127
+ # @param many [String, Symbol, nil] Specifies root keyword used when serializing multiple objects
108
128
  #
109
129
  # @return [Hash] Configured root names
110
130
  #
@@ -12,7 +12,7 @@ class Serega
12
12
  # Applies plugin code to specific serializer
13
13
  #
14
14
  # @param serializer_class [Class<Serega>] Current serializer class
15
- # @param _opts [Hash] Loaded plugins options
15
+ # @param _opts [Hash] Plugin options
16
16
  #
17
17
  # @return [void]
18
18
  #
@@ -27,7 +27,7 @@ class Serega
27
27
  validate(serializer_class, with) if with
28
28
  validate(serializer_class, except) if except
29
29
 
30
- raise_errors if any_error?
30
+ raise_errors(serializer_class) if any_error?
31
31
  end
32
32
 
33
33
  private
@@ -66,12 +66,19 @@ class Serega
66
66
  end
67
67
 
68
68
  def save_error(name)
69
- full_attribute_name = [*parents_names, name].join(".")
70
- error_attributes << full_attribute_name
69
+ error_attributes << build_full_attribute_name(*parents_names, name)
71
70
  end
72
71
 
73
- def raise_errors
74
- raise Serega::AttributeNotExist, "Not existing attributes: #{error_attributes.join(", ")}"
72
+ def build_full_attribute_name(*names)
73
+ head, *nested = *names
74
+ result = head.to_s # names are symbols, we need not frozen string
75
+ nested.each { |nested_name| result << "(" << nested_name.to_s }
76
+ nested.each { result << ")" }
77
+ result
78
+ end
79
+
80
+ def raise_errors(serializer_class)
81
+ raise Serega::AttributeNotExist.new("Not existing attributes: #{error_attributes.join(", ")}", serializer_class, error_attributes)
75
82
  end
76
83
 
77
84
  def any_error?
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.18.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-11 00:00:00.000000000 Z
11
+ date: 2023-12-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  JSON Serializer
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
142
  requirements: []
143
- rubygems_version: 3.4.22
143
+ rubygems_version: 3.5.3
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: JSON Serializer