serega 0.8.0 → 0.8.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2ebcd147c1714ebb2b4e923ee179c788158b225ac76791817229a82975926ec
4
- data.tar.gz: eef2621b3d6b9ac6ee1da3de29d3006ff99a0c2c91082cdc7795e82665ee92e5
3
+ metadata.gz: 0f1dd539e1a0b60a167cb67c5e8bfb509d5ce3be4d8ce8151a97a5f646f53378
4
+ data.tar.gz: 39ed8e53b95cc192a0819fbc4452d2cb6951b9208566d0597802cd98b828669e
5
5
  SHA512:
6
- metadata.gz: 7891e792121105b7ee566b926347a63d82fb9fae186b7cf97766d4450141538d81dbb3bf7ac571b2ff7ed12bf8567f35d3e769a68e8979e47aad5c1c99be0300
7
- data.tar.gz: 3dd972104c307eff809f5c41af005fb75fdd7caa6d6a494b90a8c63e89a0e25dc89de1547b762c6ad1f7ab3aa5e5f5811b470a6f20adaed55c7155c17fc00207
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 field in **GET** requests.
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
- - :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` (`attribute :name, many: true, batch: { ... }`).
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 via Symbol then batch loader must be defined using `config.batch_loaders.define(:loader_name) { ... }` method.
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.0
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
- class << self
14
- # Validates provided fields names are existing attributes
15
- #
16
- # @param serializer_class [Serega]
17
- # @param fields [Hash] validated fields
18
- #
19
- # @raise [Serega::AttributeNotExist] when modifier not exist as attribute
20
- #
21
- # @return [void]
22
- #
23
- def call(serializer_class, fields)
24
- return unless fields
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
- validate(serializer_class, fields, [])
27
- end
30
+ raise_errors if any_error?
31
+ end
28
32
 
29
- private
33
+ private
30
34
 
31
- def validate(serializer_class, fields, prev_names)
32
- fields.each do |name, nested_fields|
33
- attribute = serializer_class.attributes[name]
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
- raise_error(name, prev_names) unless attribute
36
- next if nested_fields.empty?
45
+ # Return when attribute has no nested fields
46
+ next if nested_fields.empty?
37
47
 
38
- raise_nested_error(name, prev_names, nested_fields) unless attribute.relation?
39
- nested_serializer = attribute.serializer
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
- def raise_error(name, prev_names)
45
- field_name = field_name(name, prev_names)
54
+ def parents_names
55
+ @parents_names ||= []
56
+ end
46
57
 
47
- raise Serega::AttributeNotExist, "Attribute #{field_name} not exists"
48
- end
58
+ def with_parent_name(name)
59
+ parents_names << name
60
+ yield
61
+ parents_names.pop
62
+ end
49
63
 
50
- def raise_nested_error(name, prev_names, nested_fields)
51
- field_name = field_name(name, prev_names)
52
- first_nested = nested_fields.keys.first
64
+ def error_attributes
65
+ @error_attributes ||= []
66
+ end
53
67
 
54
- raise Serega::AttributeNotExist, "Attribute #{field_name} has no :serializer option specified to add nested '#{first_nested}' attribute"
55
- end
68
+ def save_error(name)
69
+ full_attribute_name = [*parents_names, name].join(".")
70
+ error_attributes << full_attribute_name
71
+ end
56
72
 
57
- def field_name(name, prev_names)
58
- res = "'#{name}'"
59
- res += " ('#{prev_names.join(".")}.#{name}')" if prev_names.any?
60
- res
61
- end
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.0
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-08 00:00:00.000000000 Z
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: []