lib/simply_serializable/serializer.rb in simply_serializable-1.3.0 vs lib/simply_serializable/serializer.rb in simply_serializable-1.4.0
- old
+ new
@@ -1,26 +1,30 @@
# frozen_string_literal: true
module SimplySerializable
class Serializer
attr_reader :attributes,
+ :do_not_serialize_if_class_is,
:except,
:object,
:only
def initialize(
attributes: [],
cache: {},
+ do_not_serialize_if_class_is: [],
except: nil,
include_readable_instance_variables: true,
method_prefix: :serialize,
object:,
only: nil
)
@object = object
@id = id
@attributes = attributes
+ @do_not_serialize_if_class_is = do_not_serialize_if_class_is || []
+ @do_not_serialize_if_class_is = [@do_not_serialize_if_class_is] unless @do_not_serialize_if_class_is.is_a?(Array)
@include_readable_instance_variables = include_readable_instance_variables
@except = except&.map(&:to_sym)
@only = only&.map(&:to_sym)
@method_prefix = method_prefix
@local_cache = cache
@@ -95,10 +99,12 @@
end]
end
def deep_serialize(obj)
case obj
+ when *do_not_serialize_if_class_is
+ obj
when FalseClass, Float, nil, Integer, String, Symbol, TrueClass
obj
when Array
obj.map { |v| deep_serialize(v) }
when Hash
@@ -158,20 +164,21 @@
use_object_cache_key = cache_key_for(use_object)
if @local_cache.key?(use_object_cache_key)
@nestable = false
return reference_to(use_object_cache_key)
end
- serializer = unless use_object.respond_to?(:serialize)
+
+ serializer = if !use_object.respond_to?(:serialize)
raise "#{use_object.class.name} does not respond to serialize. Did you mean to include Serializable in this class?" unless @include_readable_instance_variables
Serializer.new(
cache: @local_cache,
object: use_object
)
else
use_object.serializer(cache: @local_cache)
end
- serializer
+
@local_cache.merge!(serializer.cache)
reference_to(use_object_cache_key)
end
def reference_to(key)