serega 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -10
- data/VERSION +1 -1
- data/lib/serega/validations/check_initiate_params.rb +1 -3
- data/lib/serega/validations/initiate/check_modifiers.rb +54 -37
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f1dd539e1a0b60a167cb67c5e8bfb509d5ce3be4d8ce8151a97a5f646f53378
|
4
|
+
data.tar.gz: 39ed8e53b95cc192a0819fbc4452d2cb6951b9208566d0597802cd98b828669e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b6519e8fb225d6bc132c5144b2268b8b7866e030a3c7a582febac6fc468a3cae449cf9298e05b5468c31c8a975629891e196ee0f1537cee401ceab394c573a2
|
7
|
+
data.tar.gz: bef92a58a8f730af6e97c3a36dbf6062aa7e3b99ad5c44484682447063018ec9dbcb345cc4715d1637a3d6c4c388f4b794a144b8250986db19efbe961b92c472
|
data/README.md
CHANGED
@@ -150,7 +150,7 @@ UserSerializer.as_json([user]) # => [{"username":"serega"}]
|
|
150
150
|
|
151
151
|
Modifiers can be provided as Hash, Array, String, Symbol or their combinations.
|
152
152
|
|
153
|
-
With plugin [string_modifiers][string_modifiers] we can provide modifiers as single `String` with attributes split by comma `,` and nested values inside brackets `()`, like: `username,enemies(username,email)`. This can be very useful to accept list of
|
153
|
+
With plugin [string_modifiers][string_modifiers] we can provide modifiers as single `String` with attributes split by comma `,` and nested values inside brackets `()`, like: `username,enemies(username,email)`. This can be very useful to accept list of fields in **GET** requests.
|
154
154
|
|
155
155
|
When provided non-existing attribute, `Serega::AttributeNotExist` error will be raised. This error can be muted with `check_initiate_params: false` parameter.
|
156
156
|
|
@@ -258,9 +258,9 @@ Allows to define `:preloads` to attributes and then allows to merge preloads
|
|
258
258
|
from serialized attributes and return single associations hash.
|
259
259
|
|
260
260
|
Plugin accepts options:
|
261
|
-
- `auto_preload_attributes_with_delegate` - default false
|
262
|
-
- `auto_preload_attributes_with_serializer` - default false
|
263
|
-
- `auto_hide_attributes_with_preload` - default false
|
261
|
+
- `auto_preload_attributes_with_delegate` - default `false`
|
262
|
+
- `auto_preload_attributes_with_serializer` - default `false`
|
263
|
+
- `auto_hide_attributes_with_preload` - default `false`
|
264
264
|
|
265
265
|
This options are very handy if you want to forget about finding preloads manually.
|
266
266
|
|
@@ -373,13 +373,12 @@ Added new `:batch` attribute option, example:
|
|
373
373
|
attribute :name, batch: { key: :id, loader: :name_loader, default: '' }
|
374
374
|
```
|
375
375
|
|
376
|
-
`:batch` option must be a hash with this keys:
|
377
|
-
-
|
378
|
-
-
|
379
|
-
-
|
376
|
+
Attribute `:batch` option must be a hash with this keys:
|
377
|
+
- `key` (required) [Symbol, Proc, callable] - Defines identifier of current object
|
378
|
+
- `loader` (required) [Symbol, Proc, callable] - Defines how to fetch values for batch of keys. Accepts 3 parameters: keys, context, point.
|
379
|
+
- `default` (optional) - Default value used when loader does not return value for current key. By default it is `nil` or `[]` when attribute has additional option `many: true` (ex: `attribute :name, many: true, batch: { ... }`).
|
380
380
|
|
381
|
-
If `:loader` was defined
|
382
|
-
Result of this block must be a Hash where keys are - provided keys, and values are - batch loaded values for according keys.
|
381
|
+
If `:loader` was defined using name (as Symbol) then batch loader must be defined using `config.batch_loaders.define(:loader_name) { ... }` method. Result of this block must be a Hash where keys are - provided keys, and values are - batch loaded values for according keys.
|
383
382
|
|
384
383
|
Batch loader works well with [`activerecord_preloads`][activerecord_preloads] plugin.
|
385
384
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.1
|
@@ -39,9 +39,7 @@ class Serega
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def check_modifiers
|
42
|
-
Initiate::CheckModifiers.call(serializer_class, opts[:only])
|
43
|
-
Initiate::CheckModifiers.call(serializer_class, opts[:except])
|
44
|
-
Initiate::CheckModifiers.call(serializer_class, opts[:with])
|
42
|
+
Initiate::CheckModifiers.new.call(serializer_class, opts[:only], opts[:with], opts[:except])
|
45
43
|
end
|
46
44
|
|
47
45
|
def serializer_class
|
@@ -10,55 +10,72 @@ class Serega
|
|
10
10
|
# Modifiers validation
|
11
11
|
#
|
12
12
|
class CheckModifiers
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
13
|
+
#
|
14
|
+
# Validates provided fields names are existing attributes
|
15
|
+
#
|
16
|
+
# @param serializer_class [Serega]
|
17
|
+
# @param only [Hash, nil] `only` modifier
|
18
|
+
# @param with [Hash, nil] `with` modifier
|
19
|
+
# @param except [Hash, nil] `except` modifier
|
20
|
+
#
|
21
|
+
# @raise [Serega::AttributeNotExist] when some checked modifier has not existing attribute
|
22
|
+
#
|
23
|
+
# @return [void]
|
24
|
+
#
|
25
|
+
def call(serializer_class, only, with, except)
|
26
|
+
validate(serializer_class, only) if only
|
27
|
+
validate(serializer_class, with) if with
|
28
|
+
validate(serializer_class, except) if except
|
25
29
|
|
26
|
-
|
27
|
-
|
30
|
+
raise_errors if any_error?
|
31
|
+
end
|
28
32
|
|
29
|
-
|
33
|
+
private
|
30
34
|
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
def validate(serializer_class, fields)
|
36
|
+
fields.each do |name, nested_fields|
|
37
|
+
attribute = serializer_class && serializer_class.attributes[name]
|
38
|
+
|
39
|
+
# Save error when no attribute with checked name exists
|
40
|
+
unless attribute
|
41
|
+
save_error(name)
|
42
|
+
next
|
43
|
+
end
|
34
44
|
|
35
|
-
|
36
|
-
|
45
|
+
# Return when attribute has no nested fields
|
46
|
+
next if nested_fields.empty?
|
37
47
|
|
38
|
-
|
39
|
-
|
40
|
-
validate(nested_serializer, nested_fields, prev_names + [name])
|
48
|
+
with_parent_name(name) do
|
49
|
+
validate(attribute.serializer, nested_fields)
|
41
50
|
end
|
42
51
|
end
|
52
|
+
end
|
43
53
|
|
44
|
-
|
45
|
-
|
54
|
+
def parents_names
|
55
|
+
@parents_names ||= []
|
56
|
+
end
|
46
57
|
|
47
|
-
|
48
|
-
|
58
|
+
def with_parent_name(name)
|
59
|
+
parents_names << name
|
60
|
+
yield
|
61
|
+
parents_names.pop
|
62
|
+
end
|
49
63
|
|
50
|
-
|
51
|
-
|
52
|
-
|
64
|
+
def error_attributes
|
65
|
+
@error_attributes ||= []
|
66
|
+
end
|
53
67
|
|
54
|
-
|
55
|
-
|
68
|
+
def save_error(name)
|
69
|
+
full_attribute_name = [*parents_names, name].join(".")
|
70
|
+
error_attributes << full_attribute_name
|
71
|
+
end
|
56
72
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
73
|
+
def raise_errors
|
74
|
+
raise Serega::AttributeNotExist, "Not existing attributes: #{error_attributes.join(", ")}"
|
75
|
+
end
|
76
|
+
|
77
|
+
def any_error?
|
78
|
+
defined?(@error_attributes)
|
62
79
|
end
|
63
80
|
end
|
64
81
|
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.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Glushkov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
JSON Serializer
|
@@ -93,8 +93,8 @@ homepage: https://github.com/aglushkov/serega
|
|
93
93
|
licenses:
|
94
94
|
- MIT
|
95
95
|
metadata:
|
96
|
-
homepage_uri: https://github.com/aglushkov/serega
|
97
96
|
source_code_uri: https://github.com/aglushkov/serega
|
97
|
+
documentation_uri: https://www.rubydoc.info/gems/serega
|
98
98
|
changelog_uri: https://github.com/aglushkov/serega/blob/master/CHANGELOG.md
|
99
99
|
post_install_message:
|
100
100
|
rdoc_options: []
|