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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a27b447fa68c06116dd6c47fee6b4b88d4a133b13918c3fef39128eb2b29bcd6
4
- data.tar.gz: 5287b42771194bc862c4822fcff6ebd2f4468832bfeffd49bac56508f2a16b87
3
+ metadata.gz: 94f2932d08df7f0236c889c447159ff88c2aebb3761f754cee5acaa31a49586d
4
+ data.tar.gz: a2b8e77a53cb3f38a619b616c48cdab480af0b7890952bb82632ea8fece8d1b3
5
5
  SHA512:
6
- metadata.gz: daa4636e280f256663d9cfb84be204b0b42a5e588022a1c0fd5a5905c3502597a6b0fd49f8aa8196426f4e2a03dbfd62e0ec2234633f7c196a795c275bee3153
7
- data.tar.gz: a4c0da31e4d8bac18f97382f514cd2cba48dfdded2d820e0926825d022fdda546019c6e3eda7d02f308a9a4bfdc28ee2af9d1c1a97a2a1babc48835ee2e1101f
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, model_classx: OrderItem
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ializer
4
- VERSION = '0.8.1'
4
+ VERSION = '0.9.0'
5
5
  end
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
- _attributes.values.each_with_object({}) do |field, data|
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.8.1
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: 2020-06-23 00:00:00.000000000 Z
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: '0'
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: '0'
110
+ version: 0.78.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rubocop-rspec
113
113
  requirement: !ruby/object:Gem::Requirement