ializer 0.5.0 → 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: 497afd4a62c4b255b976501c69adf8de2a569376c25c7aefb4a94b7ed94200bc
4
- data.tar.gz: '082946872d647c482b9044898b8e9069fec868339b33eac6f4c8996a446e47be'
3
+ metadata.gz: 94f2932d08df7f0236c889c447159ff88c2aebb3761f754cee5acaa31a49586d
4
+ data.tar.gz: a2b8e77a53cb3f38a619b616c48cdab480af0b7890952bb82632ea8fece8d1b3
5
5
  SHA512:
6
- metadata.gz: 0d079821ef910ac423409b0af192fdf971236fc7cd39efb1b994f0817edebbe16285302fc14d34634f452b16af85195d8c522fd3677ad17a0c5b1a9100f1c35f
7
- data.tar.gz: b2136f4515fbfe36d270f1c726dd45417f5759ea931f0f264a3709f324a88e1adcf82bf93db2173eb8df146d7ee52f35ed50ce3813e9bb989d00f78f200d0ab4
6
+ metadata.gz: 6fe88893e5ee61a1cef8b28f52add84a2d54da0a37e2556167dc8f1ee9ac9afd836104837a5c75cc4dc9a71208e0fe1d4f41664554ad8a93b816afd14f213c3b
7
+ data.tar.gz: 854229b905bedce44414a452e9fbf91b0cd44d88ce26ee6aebdc2fe809ed69056dcda0caa2b55766a02695a12e73855d742b5247f23e0abca1dc88b33dad321a
data/README.md CHANGED
@@ -13,6 +13,7 @@ A fast serializer/deserializer for Ruby Objects.
13
13
  * [Model Definitions](#model-definitions)
14
14
  * [Serializer Definitions](#serializer-definitions)
15
15
  * [DeSerializer Definitions](#deserializer-definitions)
16
+ * [De/Serializer Configuration](#deserializer-configuration)
16
17
  * [Object Serialization](#object-serialization)
17
18
  * [Object Deserialization](#object-deserialization)
18
19
  * [Attributes](#attributes)
@@ -153,6 +154,26 @@ class CustomerDeSer < De::Ser::Ializer
153
154
  end
154
155
  ```
155
156
 
157
+ ### De/Ser::Ializer Configuration
158
+
159
+ You can override the global config for a specific `Ser::Ializer` or `De::Ser::Ializer` by calling the setup command.
160
+
161
+ **Note:** `setup` must be called at the beginning of the definition otherwise the default config will be used.
162
+
163
+ ```ruby
164
+ class OrderDeSer < De::Ser::Ializer
165
+ setup do |config|
166
+ config.key_transform = :dasherize
167
+ end
168
+
169
+ integer :id
170
+ timestamp :created_at
171
+
172
+ nested :items, deser: OrderItemDeSer, model_class: OrderItem
173
+ nested :customer, deser: CustomerDeSer, model_class: Customer
174
+ end
175
+ ```
176
+
156
177
  ### Sample Object
157
178
 
158
179
  ```ruby
@@ -183,17 +204,17 @@ json_string = OrderDeser.serialize_json(order)
183
204
  ```json
184
205
  {
185
206
  "id": 4,
186
- "created-at": "2019-12-01T00:00:00.000-06:00",
207
+ "created_at": "2019-12-01T00:00:00.000-06:00",
187
208
  "items": [
188
209
  {
189
210
  "name": "Baseball",
190
211
  "decimal": "4.99",
191
- "in-stock": true
212
+ "in_stock": true
192
213
  },
193
214
  {
194
215
  "name": "Football",
195
216
  "decimal": "14.99",
196
- "in-stock": false
217
+ "in_stock": false
197
218
  }
198
219
  ],
199
220
  "customer": {
@@ -226,7 +247,7 @@ data = OrderDeSer.serialize([order, order2])
226
247
 
227
248
  ### Object Deserialization
228
249
 
229
- **Note:** Objects that are parsed must have a zero-argument initializer (ie: Object.new)
250
+ **Note:** Objects that are parsed must have a zero argument initializer (ie: Object.new)
230
251
 
231
252
  #### Parsing a hash
232
253
 
@@ -271,7 +292,7 @@ class OrderDeSer < De::Ser::Ializer
271
292
 
272
293
  nested :items, deser: OrderItemDeSer, model_class: OrderItem
273
294
  # OR
274
- property :items, deser: OrderItemDeSer, model_classx: OrderItem
295
+ property :items, deser: OrderItemDeSer, model_class: OrderItem
275
296
 
276
297
  nested :customer, model_class: Customer do
277
298
  string :name
@@ -437,6 +458,27 @@ end
437
458
  CustomerDeSer.serialize(order, current_user)
438
459
  ```
439
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
+
440
482
  ### Conditional Attributes
441
483
 
442
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
@@ -53,6 +53,8 @@ module De
53
53
  private
54
54
 
55
55
  def parse_field(object, field, value)
56
+ return if value.nil?
57
+
56
58
  parsed_value = field.parse(value)
57
59
 
58
60
  return if parsed_value.nil?
@@ -11,7 +11,11 @@ module Ializer
11
11
  end
12
12
 
13
13
  def self.parse(value)
14
+ return nil if value.nil?
15
+
14
16
  BigDecimal(value)
17
+ rescue ArgumentError
18
+ value
15
19
  end
16
20
  end
17
21
  end
@@ -10,7 +10,7 @@ module Ializer
10
10
  # :key_transform=: key_transform
11
11
  #
12
12
  # symbol of string transform to call on field keys
13
- # default is +:dasherize+.
13
+ # default is nil
14
14
  def key_transform=(key_transform)
15
15
  self.key_transformer = key_transform&.to_proc
16
16
  end
@@ -10,8 +10,8 @@ module Ializer
10
10
 
11
11
  def self.parse(value)
12
12
  Date.parse(value)
13
- rescue ArgumentError
14
- nil
13
+ rescue ArgumentError, TypeError
14
+ value
15
15
  end
16
16
  end
17
17
  end
@@ -9,6 +9,8 @@ module Ializer
9
9
  def self.parse(value)
10
10
  return value if value.is_a? Numeric
11
11
 
12
+ return nil if value.nil?
13
+
12
14
  value.to_i
13
15
  end
14
16
  end
@@ -23,6 +23,8 @@ module Ializer
23
23
 
24
24
  return Float::INFINITY if value == INFINITY_STRING
25
25
 
26
+ return nil if value.nil?
27
+
26
28
  value.to_f
27
29
  end
28
30
  end
@@ -9,7 +9,11 @@ module Ializer
9
9
  end
10
10
 
11
11
  def self.parse(value)
12
+ return nil if value.nil?
13
+
12
14
  Time.at(value / 1000.0)
15
+ rescue NoMethodError
16
+ value
13
17
  end
14
18
  end
15
19
  end
@@ -7,7 +7,11 @@ module Ializer
7
7
  end
8
8
 
9
9
  def self.parse(value)
10
+ return nil if value.nil?
11
+
10
12
  value.to_sym
13
+ rescue NoMethodError
14
+ value
11
15
  end
12
16
  end
13
17
  end
@@ -10,6 +10,8 @@ module Ializer
10
10
 
11
11
  def self.parse(value)
12
12
  DateTime.iso8601 value
13
+ rescue ArgumentError
14
+ value
13
15
  end
14
16
  end
15
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ializer
4
- VERSION = '0.5.0'
4
+ VERSION = '0.9.0'
5
5
  end
data/lib/ser/ializer.rb CHANGED
@@ -5,9 +5,24 @@ module Ser
5
5
  @@method_registry = {} # rubocop:disable Style/ClassVars
6
6
 
7
7
  class << self
8
+ def config
9
+ @config ||=
10
+ if equal? Ser::Ializer
11
+ ::Ializer.config
12
+ else
13
+ superclass.config
14
+ end
15
+ end
16
+
17
+ def setup
18
+ @config = config.dup
19
+
20
+ yield @config
21
+ end
22
+
8
23
  # Public DSL
9
24
  def property(name, options = {}, &block)
10
- return add_attribute(Field.new(name, options, &block)) if options[:deser]
25
+ return add_attribute(Field.new(name, options, config, &block)) if options[:deser]
11
26
 
12
27
  return default(name, options, &block) unless options[:type]
13
28
 
@@ -25,7 +40,7 @@ module Ser
25
40
  options[:deser] = deser
26
41
  end
27
42
 
28
- add_attribute(Field.new(name, options))
43
+ add_attribute(Field.new(name, options, config))
29
44
  end
30
45
 
31
46
  def with(deser)
@@ -52,7 +67,7 @@ module Ser
52
67
 
53
68
  define_singleton_method(method_name) do |name, options = {}, &block|
54
69
  options[:deser] = deser
55
- add_attribute Field.new(name, options, &block)
70
+ add_attribute Field.new(name, options, config, &block)
56
71
  end
57
72
 
58
73
  matchers.each do |matcher|
@@ -62,12 +77,12 @@ module Ser
62
77
 
63
78
  def register_default(deser)
64
79
  define_singleton_method('default') do |name, options = {}, &block|
65
- raise ArgumentError, warning_message(name) if ::Ializer.config.raise_on_default?
80
+ raise ArgumentError, warning_message(name) if config.raise_on_default?
66
81
 
67
- puts warning_message(name) if ::Ializer.config.warn_on_default?
82
+ puts warning_message(name) if config.warn_on_default?
68
83
 
69
84
  options[:deser] = deser
70
- add_attribute Field.new(name, options, &block)
85
+ add_attribute Field.new(name, options, config, &block)
71
86
  end
72
87
  end
73
88
 
@@ -137,7 +152,7 @@ module Ser
137
152
  end
138
153
 
139
154
  def serialize_one(object, context)
140
- _attributes.values.each_with_object({}) do |field, data|
155
+ fields_for_serialization(context).each_with_object({}) do |field, data|
141
156
  next unless field.valid_for_context?(object, context)
142
157
 
143
158
  value = public_send(field.name, object, context)
@@ -148,6 +163,26 @@ module Ser
148
163
  end
149
164
  end
150
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
+
151
186
  def valid_enumerable?(object)
152
187
  return true if object.is_a? Array
153
188
 
@@ -4,19 +4,19 @@ module Ser
4
4
  class Ializer
5
5
  class Field
6
6
  class << self
7
- def transform(key)
8
- return key unless ::Ializer.config.key_transformer
7
+ def transform(key, key_transformer)
8
+ return key unless key_transformer
9
9
 
10
- ::Ializer.config.key_transformer.call(key)
10
+ key_transformer.call(key)
11
11
  end
12
12
  end
13
13
 
14
14
  attr_reader :name, :setter, :key, :deser, :model_class, :if_condition, :block
15
15
 
16
- def initialize(name, options, &block)
16
+ def initialize(name, options, config, &block)
17
17
  @name = name
18
18
  @setter = options[:setter] || "#{name}="
19
- @key = options[:key] || Field.transform(name.to_s)
19
+ @key = options[:key] || Field.transform(name.to_s, config.key_transformer)
20
20
  @deser = options[:deser]
21
21
  @if_condition = options[:if]
22
22
  @model_class = options[:model_class]
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.5.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: 2020-03-24 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