fast_serializer 1.1.4 → 1.1.5

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: a466a168d1e4a3e346fca8549c08eef80b4f524d5db975197129f23ce8614b16
4
- data.tar.gz: 07113eca188e090a2ab948fc9740ac71de8af3d74e76748a368a4e3acc9d3da1
3
+ metadata.gz: 43835187de2f5c9d212b524e690bff5b7c9200017db003cd86c7626d7614bbfd
4
+ data.tar.gz: 5b8fbbde43174cce1fedf517c260ce50dbf8a9543132cb04139247ac360db31e
5
5
  SHA512:
6
- metadata.gz: dce0f676f3cb5dc3591b9f28ef09ad662b8203839b96a27f30043fd000399fff0c03799ee642a085afecaba60d7221bc97ae5bc3dc044bdcca21d95bba3cbc97
7
- data.tar.gz: c4486052e66f392022e8524cb3fa201087248c82ed6444b9b08ff4ee3d1292795a9f6657324213e32791f572751745a05f58b7c896aba8550f0212a1589debd4
6
+ metadata.gz: aeb09ec697703487469d5cfeee8fb102aec96747cc391146fe602fb1f75169418353df1eea2b92fb841cb58df2937813193014d917d2fa40b73aaef9e86f93af
7
+ data.tar.gz: e002e4633acaacc799d77718b4ba66dcb1795f372d8e24cd6cce164c7a49b331729a7955018a7ddc4c4a04d79ff8fadf831558bf8e3afe8a484a7c79293316e7
data/CHANGELOG.md CHANGED
@@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.1.5
8
+
9
+ ### Fixed
10
+
11
+ - The `delegate: false` option on `serialize` is now honored; previously a delegate method was always defined.
12
+ - Redefining a serialized field (including in a subclass) now replaces the existing field instead of also appending a duplicate that was serialized twice.
13
+ - `SerializationContext` no longer clears its cache on every miss, so duplicate serializers within a context are properly reused.
14
+ - `ArraySerializer#cache` and `#cache_ttl` now return the configured cache and TTL instead of `true`, fixing a crash when passing the `:cache` option to an array serializer.
15
+ - `Cache::ActiveSupportCache#fetch_all` now passes the TTL to `fetch_multi`, so batch-cached entries expire as configured.
16
+ - Serializing non-Array enumerables (such as `Set`) containing values that need conversion no longer raises `NoMethodError`.
17
+
18
+ ### Changed
19
+
20
+ - Serialization no longer duplicates the options hash for fields without an associated serializer when `:include` or `:exclude` options are present.
21
+ - Minimum required Ruby version is now 2.6.
22
+
7
23
  ## 1.1.4
8
24
 
9
25
  ### Fixed
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.4
1
+ 1.1.5
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- spec.required_ruby_version = ">= 2.5"
29
+ spec.required_ruby_version = ">= 2.6"
30
30
 
31
31
  spec.add_development_dependency "bundler"
32
32
  end
@@ -34,10 +34,10 @@ module FastSerializer
34
34
  end
35
35
  end
36
36
 
37
- # @return [Numeric, Boolean]
37
+ # @return [Numeric, nil]
38
38
  def cache_ttl
39
39
  if option(:cache_ttl)
40
- true
40
+ option(:cache_ttl)
41
41
  elsif option(:serializer)
42
42
  option(:serializer).cache_ttl
43
43
  else
@@ -45,10 +45,10 @@ module FastSerializer
45
45
  end
46
46
  end
47
47
 
48
- # @return [FastSerializer::Cache, Boolean]
48
+ # @return [FastSerializer::Cache, nil]
49
49
  def cache
50
- if option(:cache)
51
- true
50
+ if @cache
51
+ @cache
52
52
  elsif option(:serializer)
53
53
  option(:serializer).cache
54
54
  else
@@ -56,11 +56,9 @@ module FastSerializer
56
56
  end
57
57
  end
58
58
 
59
- # @return [Hash]
59
+ # @return [Array, nil]
60
60
  def as_json(*args)
61
- if array.nil?
62
- nil
63
- elsif array.empty?
61
+ if array.empty?
64
62
  []
65
63
  else
66
64
  super[:array]
@@ -16,7 +16,7 @@ module FastSerializer
16
16
  end
17
17
 
18
18
  def fetch_all(serializers, ttl)
19
- results = @cache.fetch_multi(*serializers) { |serializer| yield(serializer) }
19
+ results = @cache.fetch_multi(*serializers, expires_in: ttl) { |serializer| yield(serializer) }
20
20
  if results.is_a?(Hash)
21
21
  serializers.collect { |serializer| results[serializer] }
22
22
  else
@@ -55,7 +55,7 @@ module FastSerializer
55
55
  unless serializer
56
56
  serializer = serializer_class.allocate
57
57
  serializer.send(:initialize, object, options)
58
- @cache = {}
58
+ @cache ||= {}
59
59
  @cache[key] = serializer
60
60
  end
61
61
 
@@ -3,7 +3,7 @@
3
3
  module FastSerializer
4
4
  # Data structure used internally for maintaining a field to be serialized.
5
5
  class SerializedField
6
- attr_reader :name, :condition
6
+ attr_reader :name, :condition, :serializer
7
7
 
8
8
  # Create a new serialized field.
9
9
  #
@@ -36,7 +36,6 @@ module FastSerializer
36
36
  # @return [Object] the serialized value
37
37
  def serialize(value, options = nil)
38
38
  if value && @serializer
39
- serializer = nil
40
39
  serializer = if @enumerable
41
40
  ArraySerializer.new(value, serializer: @serializer, serializer_options: serializer_options(options))
42
41
  else
@@ -109,7 +108,7 @@ module FastSerializer
109
108
  hash = nil
110
109
  value.each do |k, v|
111
110
  val = serialize_value(v)
112
- if val.object_id != v.object_id
111
+ unless val.equal?(v)
113
112
  hash ||= value.dup
114
113
  hash[k] = val
115
114
  end
@@ -121,9 +120,11 @@ module FastSerializer
121
120
  array = nil
122
121
  value.each_with_index do |v, i|
123
122
  val = serialize_value(v)
124
- if val.object_id != v.object_id
125
- array ||= value.dup
126
- array[i] = val
123
+ if array
124
+ array << val
125
+ elsif !val.equal?(v)
126
+ array = value.first(i)
127
+ array << val
127
128
  end
128
129
  end
129
130
  array || value
@@ -7,17 +7,19 @@ module FastSerializer
7
7
  # To define what fields to serialize on the wrapped object, the serializer class must call the +serialize+
8
8
  # class method:
9
9
  #
10
+ # @example Defining a basic serializer
10
11
  # class PersonSerializer
11
12
  # include FastSerializer::Serializer
12
13
  # serialize :id, :name
13
14
  # end
14
15
  #
15
- # This sample serializer will output an object as a hash with keys {:id, :name}. The values for each field
16
+ # This sample serializer will output an object as a hash with the keys +:id+ and +:name+. The values for each field
16
17
  # is gotten by calling the corresponding method on the serializer object. By default, each serialized field
17
18
  # will automatically define a method that simply delegates to the wrapped object. So if you need provide special
18
19
  # handling for a field or serialize a virtual field that doesn't exist on the parent object, you just need to
19
20
  # implement the method on the serializer.
20
21
  #
22
+ # @example Defining a virtual field
21
23
  # class PersonSerializer
22
24
  # include FastSerializer::Serializer
23
25
  # serialize :id, :name
@@ -29,6 +31,7 @@ module FastSerializer
29
31
  #
30
32
  # Serializers can implement their own options for controlling details about how to serialize the object.
31
33
  #
34
+ # @example Defining a serializer with custom options
32
35
  # class PersonSerializer
33
36
  # include FastSerializer::Serializer
34
37
  # serialize :id, :name
@@ -75,7 +78,7 @@ module FastSerializer
75
78
  # Several options can be specified to control how the field is serialized.
76
79
  #
77
80
  # * as: Name to call the field in the serialized hash. Defaults to the same as the field name
78
- # (withany ? stripped off the end for boolean fields). This option can only be specified
81
+ # (with any ? stripped off the end for boolean fields). This option can only be specified
79
82
  # for a single field.
80
83
  #
81
84
  # * optional: Boolean flag indicating if the field is optional in the serialized value (defaults to false).
@@ -115,7 +118,7 @@ module FastSerializer
115
118
  end
116
119
  as = options.delete(:as)
117
120
  optional = options.delete(:optional) || false
118
- delegate = options.delete(:delegate) || true
121
+ delegate = options.include?(:delegate) ? options.delete(:delegate) : true
119
122
  enumerable = options.delete(:enumerable) || false
120
123
  serializer = options.delete(:serializer)
121
124
  serializer_options = options.delete(:serializer_options)
@@ -205,7 +208,7 @@ module FastSerializer
205
208
  @cache_ttl = value
206
209
  end
207
210
 
208
- # Get the cache implemtation used to store cacheable serializers.
211
+ # Get the cache implementation used to store cacheable serializers.
209
212
  #
210
213
  # @return [FastSerializer::Cache]
211
214
  def cache
@@ -258,7 +261,7 @@ module FastSerializer
258
261
  def add_field(name, optional:, serializer:, serializer_options:, enumerable:, condition:)
259
262
  name = name.to_sym
260
263
  if condition.is_a?(Proc)
261
- include_method_name = "__include_#{name}?".to_sym
264
+ include_method_name = :"__include_#{name}?"
262
265
  define_method(include_method_name, condition)
263
266
  private include_method_name
264
267
  condition = include_method_name
@@ -271,6 +274,7 @@ module FastSerializer
271
274
  added = false
272
275
  serializable_fields.each do |existing_field|
273
276
  field_list << if existing_field.name == name
277
+ added = true
274
278
  field
275
279
  else
276
280
  existing_field
@@ -307,7 +311,7 @@ module FastSerializer
307
311
  @object = object
308
312
  @options = options
309
313
  @cache = options[:cache] if options
310
- if @cache && defined?(ActiveSupport::Cache::Store) && cache.is_a?(ActiveSupport::Cache::Store)
314
+ if @cache && defined?(ActiveSupport::Cache::Store) && @cache.is_a?(ActiveSupport::Cache::Store)
311
315
  @cache = Cache::ActiveSupportCache.new(@cache)
312
316
  end
313
317
  @_serialized = nil
@@ -388,7 +392,7 @@ module FastSerializer
388
392
  condition = field.condition
389
393
  next if condition && !send(condition)
390
394
 
391
- value = field.serialize(send(name), serializer_options(name))
395
+ value = field.serialize(send(name), field.serializer ? serializer_options(name) : nil)
392
396
  hash[name] = value
393
397
  end
394
398
  end
@@ -405,7 +409,7 @@ module FastSerializer
405
409
  include_options = include_options[name.to_sym]
406
410
  opts[:include] = include_options if include_options
407
411
  end
408
- exclude_options = options[:exclude]
412
+ exclude_options = opts[:exclude]
409
413
  if exclude_options.is_a?(Hash)
410
414
  exclude_options = exclude_options[name.to_sym]
411
415
  opts[:exclude] = exclude_options if exclude_options
@@ -42,7 +42,7 @@ module FastSerializer
42
42
  # Exception raised when there is a circular reference serializing a model dependent on itself.
43
43
  class CircularReferenceError < StandardError
44
44
  def initialize(model)
45
- super("Circular refernce on #{model.inspect}")
45
+ super("Circular reference on #{model.inspect}")
46
46
  end
47
47
  end
48
48
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-11-12 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bundler
@@ -24,7 +23,6 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
27
- description:
28
26
  email:
29
27
  - bbdurand@gmail.com
30
28
  executables: []
@@ -47,7 +45,6 @@ homepage: https://github.com/bdurand/fast_serializer
47
45
  licenses:
48
46
  - MIT
49
47
  metadata: {}
50
- post_install_message:
51
48
  rdoc_options: []
52
49
  require_paths:
53
50
  - lib
@@ -55,15 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
52
  requirements:
56
53
  - - ">="
57
54
  - !ruby/object:Gem::Version
58
- version: '2.5'
55
+ version: '2.6'
59
56
  required_rubygems_version: !ruby/object:Gem::Requirement
60
57
  requirements:
61
58
  - - ">="
62
59
  - !ruby/object:Gem::Version
63
60
  version: '0'
64
61
  requirements: []
65
- rubygems_version: 3.4.12
66
- signing_key:
62
+ rubygems_version: 4.0.3
67
63
  specification_version: 4
68
64
  summary: Super fast object serialization for API's combining a simple DSL with many
69
65
  optimizations under the hood.