ializer 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/ializer.gemspec +1 -1
- data/lib/ializer/version.rb +1 -1
- data/lib/ser/ializer.rb +21 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94f2932d08df7f0236c889c447159ff88c2aebb3761f754cee5acaa31a49586d
|
4
|
+
data.tar.gz: a2b8e77a53cb3f38a619b616c48cdab480af0b7890952bb82632ea8fece8d1b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fe88893e5ee61a1cef8b28f52add84a2d54da0a37e2556167dc8f1ee9ac9afd836104837a5c75cc4dc9a71208e0fe1d4f41664554ad8a93b816afd14f213c3b
|
7
|
+
data.tar.gz: 854229b905bedce44414a452e9fbf91b0cd44d88ce26ee6aebdc2fe809ed69056dcda0caa2b55766a02695a12e73855d742b5247f23e0abca1dc88b33dad321a
|
data/README.md
CHANGED
@@ -292,7 +292,7 @@ class OrderDeSer < De::Ser::Ializer
|
|
292
292
|
|
293
293
|
nested :items, deser: OrderItemDeSer, model_class: OrderItem
|
294
294
|
# OR
|
295
|
-
property :items, deser: OrderItemDeSer,
|
295
|
+
property :items, deser: OrderItemDeSer, model_class: OrderItem
|
296
296
|
|
297
297
|
nested :customer, model_class: Customer do
|
298
298
|
string :name
|
@@ -458,6 +458,27 @@ end
|
|
458
458
|
CustomerDeSer.serialize(order, current_user)
|
459
459
|
```
|
460
460
|
|
461
|
+
#### Using the context to serialize a subset of attributes
|
462
|
+
|
463
|
+
There are special keywords/method names on the Serialization Context that can be used to limit the attributes that are serialized. This is different from conditional attributes below. The conditions would still apply to the subset.
|
464
|
+
|
465
|
+
If your serialization context is a `Hash`, you can use the hash keys `:attributes` or `:include` to define the limited subset of attributes for serialization.
|
466
|
+
|
467
|
+
```ruby
|
468
|
+
CustomerDeSer.serialize(order, attributes: [:name])
|
469
|
+
```
|
470
|
+
|
471
|
+
If your serialization context is a ruby object, a method named `attributes` that returns an array of attribute names can be used.
|
472
|
+
|
473
|
+
```ruby
|
474
|
+
class AttributeSubsetContext
|
475
|
+
attr_accessor :attributes
|
476
|
+
end
|
477
|
+
|
478
|
+
context = AttributeSubsetContext.new(attributes: [:name])
|
479
|
+
CustomerDeSer.serialize(order, context)
|
480
|
+
```
|
481
|
+
|
461
482
|
### Conditional Attributes
|
462
483
|
|
463
484
|
Conditional attributes can be defined by passing a Proc to the `if` key on the `property` method. Return `truthy` if the attribute should be serialized, and `falsey` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
|
data/ializer.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_development_dependency 'pry'
|
29
29
|
spec.add_development_dependency 'rake'
|
30
30
|
spec.add_development_dependency 'rspec'
|
31
|
-
spec.add_development_dependency 'rubocop'
|
31
|
+
spec.add_development_dependency 'rubocop', '~> 0.78.0'
|
32
32
|
spec.add_development_dependency 'rubocop-rspec'
|
33
33
|
spec.add_development_dependency 'simplecov'
|
34
34
|
end
|
data/lib/ializer/version.rb
CHANGED
data/lib/ser/ializer.rb
CHANGED
@@ -152,7 +152,7 @@ module Ser
|
|
152
152
|
end
|
153
153
|
|
154
154
|
def serialize_one(object, context)
|
155
|
-
|
155
|
+
fields_for_serialization(context).each_with_object({}) do |field, data|
|
156
156
|
next unless field.valid_for_context?(object, context)
|
157
157
|
|
158
158
|
value = public_send(field.name, object, context)
|
@@ -163,6 +163,26 @@ module Ser
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
+
def fields_for_serialization(context)
|
167
|
+
field_names = fields_names_for_serialization(context)
|
168
|
+
|
169
|
+
return _attributes.values unless field_names
|
170
|
+
|
171
|
+
_attributes.values_at(*field_names)
|
172
|
+
end
|
173
|
+
|
174
|
+
def fields_names_for_serialization(context) # rubocop:disable Metrics/CyclomaticComplexity
|
175
|
+
return nil unless context
|
176
|
+
|
177
|
+
if context.is_a?(Hash)
|
178
|
+
return context[:attributes] || context[:include] || context['attributes'] || context['include']
|
179
|
+
end
|
180
|
+
|
181
|
+
return context.attributes if context.respond_to?(:attributes)
|
182
|
+
|
183
|
+
nil
|
184
|
+
end
|
185
|
+
|
166
186
|
def valid_enumerable?(object)
|
167
187
|
return true if object.is_a? Array
|
168
188
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Steinberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -98,16 +98,16 @@ dependencies:
|
|
98
98
|
name: rubocop
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - "
|
101
|
+
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
103
|
+
version: 0.78.0
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - "
|
108
|
+
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: 0.78.0
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rubocop-rspec
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|